合并图像后质量下降,Android

时间:2014-07-02 18:43:54

标签: android

我有两个图像,我想要一个接一个。

然而,在合并之后我得到了低质量的文件。

这是我原来的topImage(96x96)像素:

enter image description here

这是我的bottomIamge(74x74)像素:

enter image description here

你可以看到质量非常好。

当我在提到的代码下运行时,我得到了合并的Image(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像素?

谢谢,

2 个答案:

答案 0 :(得分: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

质量不一样但似乎更好

答案 1 :(得分:0)

按照大厅编号(2,3,4,5)进行缩放时,无法保持质量。在您的情况下,您将缩放96像素到74。

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

另请注意这个video解释什么是反aliacing ,以及它如何为您提供帮助。