2014-07-02 30 views
1

我有兩個圖像,我想設置一個在另一個。合併後的圖像質量下降,Android

但合併後我得到低質量的文件。

這是我原來的topImage(96×96)像素:

enter image description here

這是我bottomIamge(74x74)像素:

enter image description here

你可以看到,質量是相當不錯的。

當我提到下運行代碼我得到合併圖像(74x74):

enter image description here

現在你可以看到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像素?

感謝,

回答

0

通過未堂號(2,3,4,5)縮放時不能保持質量。在你的情況下,你正在將96像素縮放到74.

您可以將比例更改爲48像素(x2)或保持96像素。

另外看這個video,它解釋什麼是反混淆,以及它如何幫助你。

+0

的想法是,'topImage'可能是不同的大小,120×120,56x56 ...但我希望把它在固定74x74的'bottomImage'上。謝謝, – snaggs

+0

那麼正如我所解釋的。如果您不按大廳號進行縮放,則會出現像素化問題。 –

+0

對不起,但這個答案不滿足我,因爲從你的話我不能做我想要的。無論如何,我發現了幾個解決方案,保證不同質量,但接近。再次感謝你 – snaggs

1

現在我用這種方式(基於THIS答案):

// this method should replace Bitmap.createScaledBitmap 
private Bitmap scaleBitmap(Bitmap bitmap, int newWidth, int newHeight){ 
     Bitmap scaledBitmap = Bitmap.createBitmap(newWidth, newHeight, Config.ARGB_8888); 

     float ratioX = newWidth/(float) bitmap.getWidth(); 
     float ratioY = newHeight/(float) bitmap.getHeight(); 
     float middleX = newWidth/2.0f; 
     float middleY = newHeight/2.0f; 

     Matrix scaleMatrix = new Matrix(); 
     scaleMatrix.setScale(ratioX, ratioY, middleX, middleY); 

     Canvas canvas = new Canvas(scaledBitmap); 
     canvas.setMatrix(scaleMatrix); 
     canvas.drawBitmap(bitmap, middleX - bitmap.getWidth()/2, middleY - bitmap.getHeight()/2, new Paint(Paint.FILTER_BITMAP_FLAG)); 

     return scaledBitmap; 
    } 

現在我可以代替:

topImage = Bitmap.createScaledBitmap(topImage, 74, 74, false); 

有:

topImage = scaleBitmap(topImage, 74, 74); 

是:

enter image description here

現在:

enter image description here

它不一樣的質量,但似乎更

+0

如果將最後一個參數更改爲true,您將獲得與createScaledBitmap相同的效果。你真的應該看我推薦給你的視頻。 –

+0

@Ilya_Gazman是的,模糊效果相同 – snaggs