Android:调整位图大小而不会降低质量

Vah*_*n84 6 java android resize bitmap

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

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

这是进行采样的功能

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;   
Run Code Online (Sandbox Code Playgroud)

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

 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);
                    }



             }
         }
Run Code Online (Sandbox Code Playgroud)

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

goR*_*Gon 7

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

  1. 使用BitmapFactory.Options :: inSampleSize-> BitmapFactory.decodeResource()的缩减尽可能接近您需要但不低于它的分辨率
  2. 通过使用Canvas :: drawBitmap()降低一点点来获得精确的分辨率

以下是SonyMobile如何解决此任务的详细说明:http://developer.sonymobile.com/2011/06/27/how-to-scale-images-for-your-android-application/

以下是SonyMobile scale utils的源代码:http://developer.sonymobile.com/downloads/code-example-module/image-scaling-code-example-for-android/

  • 这应该是公认的答案.刚刚实现了链接文章中描述的内容,结果明显优于使用简单的createScaledBitmap,并且在缩减时使用的内容也少得多.结果仍然不如缩小版,​​例如在Photoshop中. (2认同)
  • https://web.archive.org/web/20171011183652/http://developer.sonymobile.com/2011/06/27/how-to-scale-images-for-your-android-application/ (2认同)

Men*_*ena 1

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

  • 谢谢...但我已经尝试过: resizedBitmap = Bitmap.createScaledBitmap(bitmap, newWidth, newHeight, true); 但它是一样的......仍然是块状和像素化的 (4认同)