【问题标题】:change bitmap resolution in Android app在 Android 应用中更改位图分辨率
【发布时间】:2012-04-15 01:07:34
【问题描述】:

我正在编写一个应用程序,它使用手机的相机拍照,然后在我的应用程序中使用它。问题是,应用程序内存不足,这可能是因为位图的高分辨率。有没有办法让位图保持相同的大小,但降低分辨率?

谢谢!

【问题讨论】:

    标签: android bitmap resolution


    【解决方案1】:

    来自 jeet.chanchawat 的回答:https://stackoverflow.com/a/10703256/3027225

      public Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight) {
            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;
        }
    

    【讨论】:

      【解决方案2】:

      这可以在创建位图时使用 Options.inSampleSize 来完成

      【讨论】:

      • 不是因为尺寸吗?如果您只想操纵分辨率而不更改大小怎么办?
      【解决方案3】:

      你可以设置它的宽度和高度

      Bitmap bm = ShrinkBitmap(imagefile, 150, 150);
      

      调用函数

      Bitmap ShrinkBitmap(String file, int width, int height){
      
       BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
          bmpFactoryOptions.inJustDecodeBounds = true;
          Bitmap bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);
      
          int heightRatio = (int)Math.ceil(bmpFactoryOptions.outHeight/(float)height);
          int widthRatio = (int)Math.ceil(bmpFactoryOptions.outWidth/(float)width);
      
          if (heightRatio > 1 || widthRatio > 1)
          {
           if (heightRatio > widthRatio)
           {
            bmpFactoryOptions.inSampleSize = heightRatio;
           } else {
            bmpFactoryOptions.inSampleSize = widthRatio; 
           }
          }
      
          bmpFactoryOptions.inJustDecodeBounds = false;
          bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);
       return bitmap;
      }
      

      }

      这是另外两个可能对您有所帮助的链接。 Link 1 & Link 2

      【讨论】:

      • 这在我使用 decodeResource 选项来获取我的位图时有效。但是,当从获取图像意图工作时,我得到一个 Uri 并执行以下操作: Uri selectedImageUri = data.getData();位图位图 = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImageUri);以这种方式获得位图后,有没有办法调整大小?
      猜你喜欢
      • 2014-01-16
      • 2015-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多