我有兩個圖像,我想設置一個在另一個。合併後的圖像質量下降,Android
但合併後我得到低質量的文件。
這是我原來的topImage
(96×96)像素:
這是我bottomIamge
(74x74)像素:
你可以看到,質量是相當不錯的。
當我提到下運行代碼我得到合併圖像(74x74):
現在你可以看到topImage失去了他的品質。
下面是相關代碼:
// load bottom image from assets:
InputStream is;
Bitmap bottomImage;
try {
is = context.getAssets().open("images/avatar1.png");
} catch (IOException e1) {
int resID = context.getResources().getIdentifier("unknown_item", "drawable", context.getPackageName());
is = context.getResources().openRawResource(resID);
}
BitmapFactory.Options options = new BitmapFactory.Options();
options.inMutable = true;
bottomImage = BitmapFactory.decodeStream(is,null,options);
try {
is.close();
is = null;
} catch (IOException e) {
}
Bitmap topImage = null;
String base64Img = null;
// get byte array from String (base64).
byte[] backToBytes = Base64.decode(base64Img, Base64.DEFAULT);
// here I verified that image I got from byte array still has good quality
//writeToStorage(backToBytes, "test.png");
// create Bitmap
topImage = BitmapFactory.decodeByteArray(backToBytes, 0, backToBytes.length, null);
// scale the image
topImage = Bitmap.createScaledBitmap(topImage, 74, 74, false); // set fixed size 74x74 image
topImage = Bitmap.createBitmap(topImage, topImage.getWidth()/4, 0, topImage.getWidth()/2, topImage.getHeight());
// shift it:
Canvas comboImage = new Canvas(bottomImage);
// Then draw the second on top of that
comboImage.drawBitmap(topImage, 0f, 0f, null);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bottomImage.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
String base64String = Base64.encodeToString(byteArray, Base64.DEFAULT);
如果我就畫base64String
我得到合併圖像
難道我錯過了什麼?
Bitmap.createScaledBitmap(topImage, 37, 74, false);
是否縮放到37x74像素?
感謝,
的想法是,'topImage'可能是不同的大小,120×120,56x56 ...但我希望把它在固定74x74的'bottomImage'上。謝謝, – snaggs
那麼正如我所解釋的。如果您不按大廳號進行縮放,則會出現像素化問題。 –
對不起,但這個答案不滿足我,因爲從你的話我不能做我想要的。無論如何,我發現了幾個解決方案,保證不同質量,但接近。再次感謝你 – snaggs