Android:在不损失质量的情况下调整位图大小

问题描述 投票:0回答:4

在发布之前我已经搜索了整个网络。我的问题是我无法在不损失图像质量的情况下调整位图大小(质量非常差并且像素化)。

我从相机获取位图,然后我必须缩小它,这样我就可以更快地将其上传到服务器。

这是进行采样的函数

public Bitmap resizeBitmap(Bitmap bitmap){
         Canvas canvas = new Canvas();
         Bitmap resizedBitmap = null;
         if (bitmap !=null) {
                int h = bitmap.getHeight();
                int w = bitmap.getWidth();
                int newWidth=0;
                int newHeight=0;

                if(h>w){
                    newWidth = 600;
                    newHeight = 800;
                }

                if(w>h){
                    newWidth = 800;
                    newHeight = 600;
                    }

                float scaleWidth = ((float) newWidth) / w;
                float scaleHeight = ((float) newHeight) / h;



                Matrix matrix = new Matrix();
                // resize the bit map
                matrix.preScale(scaleWidth, scaleHeight);

                resizedBitmap  = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);



                Paint paint = new Paint();
                paint.setAntiAlias(true);
                paint.setFilterBitmap(true);
                paint.setDither(true);

                canvas.drawBitmap(resizedBitmap, matrix, paint);



            }


        return resizedBitmap;   

这就是我从活动结果中获取图像的方式

 protected void onActivityResult(int requestCode, int resultCode,
             Intent data) {
         if(resultCode != RESULT_CANCELED){

         if (requestCode == 0) {
             if (resultCode == RESULT_OK) {


                    getContentResolver().notifyChange(mImageUri, null);
                    ContentResolver cr = this.getContentResolver();

                    try
                    {
                        b = android.provider.MediaStore.Images.Media.getBitmap(cr, mImageUri);
                        Log.d("foto", Integer.toString(b.getWidth()));
                        Log.d("foto", Integer.toString(b.getHeight()));
                        addPhoto.setImageBitmap(b);

                    }
                    catch (Exception e)
                    {
                        Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT).show();
                        Log.d("TAG", "Failed to load", e);
                    }



             }
         }

我开始认为获得小图片尺寸的最佳方法是设置相机分辨率。还有人可以帮忙吗?

java android bitmap resize
4个回答
10
投票

一个好的缩小算法(不像最近邻算法)仅包含 2 个步骤(加上输入/输出图像裁剪的精确矩形的计算):

  1. 使用 BitmapFactory.Options::inSampleSize->BitmapFactory.decodeResource() 进行缩小尽可能接近您需要的分辨率,但不能低于它
  2. 使用 Canvas::drawBitmap()
  3. 稍微缩小尺寸即可获得准确的分辨率

这里有索尼移动如何解决这个任务的详细解释

这里是SonyMobile秤实用程序的源代码


2
投票

尝试下面提到的代码来调整位图大小。

 public Bitmap get_Resized_Bitmap(Bitmap bmp, int newHeight, int newWidth) {
        int width = bmp.getWidth();
        int height = bmp.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 newBitmap = Bitmap.createBitmap(bmp, 0, 0, width, height, matrix, false);
        return newBitmap ;
    }

我使用这段代码来缩小我的位图,它的质量,呃……,是可以接受的。

希望这有帮助。


1
投票

尝试Bitmap.createScaledBitmap。 它还具有过滤源的选项。


0
投票

我遇到过的最好的缩放方法 该方法使用

createScaledBitmap
,根据位图的高度和宽度以及比例来计算高度和宽度,因此质量不会损失

 public Bitmap resize(Bitmap imaged, int maxWidth, int maxHeight) {
        Bitmap image = imaged;

        if (maxHeight > 0 && maxWidth > 0) {
            int width = image.getWidth();
            int height = image.getHeight();
            float ratioBitmap = (float) width / (float) height;
            float ratioMax = (float) maxWidth / (float) maxHeight;
            int finalWidth = maxWidth;
            int finalHeight = maxHeight;
            if (ratioMax > 1) {
                finalWidth = Math.round(((float) maxHeight * ratioBitmap));
            } else {
                finalHeight = Math.round(((float) maxWidth / ratioBitmap));
            }
            return image = Bitmap.createScaledBitmap(image, finalWidth, finalHeight, false);
        }
        return image;
    }

使用方法:

Bitmap resizedBitmap = resize(scrBitMap,640,640);
© www.soinside.com 2019 - 2024. All rights reserved.