如何在安卓中高效地调整位图大小而不失去质量

16

我有一个大小为320x480Bitmap,我需要将其在不同的设备屏幕上拉伸,我尝试使用了这个方法:

Rect dstRect = new Rect();
canvas.getClipBounds(dstRect);
canvas.drawBitmap(frameBuffer, null, dstRect, null);

它可以工作,图像填满了整个屏幕,就像我想要的那样,但图像很模糊,看起来很糟糕。然后我尝试了:

float scaleWidth = (float) newWidth / width;
float scaleHeight = (float) newHeight / height;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap resizedBitmap = Bitmap.createBitmap(frameBuffer, 0, 0,
                width, height, matrix, true);
canvas.drawBitmap(resizedBitmap, 0, 0, null);

这次看起来非常完美、流畅,但是这段代码必须在我的主游戏循环中运行,并且每次迭代都创建Bitmap使其变得非常缓慢。我如何调整图像大小,以便它不会变得模糊并且能够快速完成?

找到了解决方案:

Paint paint = new Paint();
paint.setFilterBitmap();
canvas.drawBitmap(bitmap, matrix, paint);

2
如果您找到了自己的解决方案,应将其作为答案添加到您的问题中并自行接受,以便他人不会向您的问题添加不必要的答案。 - Kuffs
2个回答

36

调整位图大小:

public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth)
{
    int width = bm.getWidth();
    int height = bm.getHeight();
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;
    // create a matrix for the manipulation
    Matrix matrix = new Matrix();
    // resize the bit map
    matrix.postScale(scaleWidth, scaleHeight);
    // recreate the new Bitmap
    Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
    return resizedBitmap;
}

简单来说,只需输入原始Bitmap对象和所需的Bitmap尺寸,此方法将返回新调整大小的Bitmap!也许对您有用。


@user924941 这种一次性的调整大小比每帧调整大小更好。 - Steve Blackwell
4
为了获得更高质量的缩放图像,Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true); - Boris Karloff
@BorisKarloff:当进行上采样时,设置过滤器参数没有任何效果,而这正是OP感兴趣的。 - Ε Г И І И О
你如何调用这个函数? - Si8
7
为什么不使用以下代码来调整位图大小:Bitmap resizedBitmap = Bitmap.createScaledBitmap(originalBitmap, newWidth, newHeight, false); - slott
我只想调整图片的宽度,怎么做呢? - Vasant

0
我正在使用上述解决方案来调整位图大小。但是它会导致图像的一部分丢失。
以下是我的代码。
 BitmapFactory.Options bmFactoryOptions = new BitmapFactory.Options();
            bmFactoryOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;
            bmFactoryOptions.inMutable = true;
            bmFactoryOptions.inSampleSize = 2;
            Bitmap originalCameraBitmap = BitmapFactory.decodeByteArray(pData, 0, pData.length, bmFactoryOptions);
            rotatedBitmap = getResizedBitmap(originalCameraBitmap, cameraPreviewLayout.getHeight(), cameraPreviewLayout.getWidth() - preSizePriviewHight(), (int) rotationDegrees);

 public Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight, int angle) {
        int width = bm.getWidth();
        int height = bm.getHeight();
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;
        Matrix matrix = new Matrix();
        matrix.postRotate(angle);
        matrix.postScale(scaleWidth, scaleHeight);
        Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
        DeliverItApplication.getInstance().setImageCaptured(true);
        return resizedBitmap;
    }

以下是图片的高度和宽度: 预览表面大小:352:288 调整大小前的位图宽度:320 高度:240 CameraPreviewLayout 宽度:1080 高度:1362 调整大小后的位图宽度:1022 高度:1307


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接