【问题标题】:Reduce size of Bitmap to some specified pixel in Android将位图的大小减小到 Android 中的某个指定像素
【发布时间】:2013-03-23 10:58:22
【问题描述】:

我想将我的位图图像大小减小到最大 640 像素。例如,我有大小为 1200 x 1200 像素的位图图像。我怎样才能将其缩小到 640 像素。

【问题讨论】:

    标签: android bitmap compression


    【解决方案1】:

    如果你传递位图widthheight 然后使用:

    public Bitmap getResizedBitmap(Bitmap image, int bitmapWidth, int bitmapHeight) {
        return Bitmap.createScaledBitmap(image, bitmapWidth, bitmapHeight, true);
    }
    

    如果您想保持位图比例相同,但将其减小到最大边长,请使用:

    public Bitmap getResizedBitmap(Bitmap image, int maxSize) {
            int width = image.getWidth();
            int height = image.getHeight();
    
            float bitmapRatio = (float) width / (float) height;
            if (bitmapRatio > 1) {
                width = maxSize;
                height = (int) (width / bitmapRatio);
            } else {
                height = maxSize;
                width = (int) (height * bitmapRatio);
            }
    
            return Bitmap.createScaledBitmap(image, width, height, true);
    }
    

    【讨论】:

    • 感谢这个 sn-p。有一个错误tough,你必须检查“if (bitmapRatio > 1)”不是0。即使高度更大,你也不会有负比率。
    • 我到处都能看到这个解决方案。但为什么它会裁剪我的位图?我只有左上部分,它也移动到屏幕的右侧(
    【解决方案2】:

    使用这个方法

     /** getResizedBitmap method is used to Resized the Image according to custom width and height 
      * @param image
      * @param newHeight (new desired height)
      * @param newWidth (new desired Width)
      * @return image (new resized image)
      * */
    public static Bitmap getResizedBitmap(Bitmap image, int newHeight, int newWidth) {
        int width = image.getWidth();
        int height = image.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(image, 0, 0, width, height,
                matrix, false);
        return resizedBitmap;
    }
    

    【讨论】:

      【解决方案3】:

      或者你可以这样做:

      Bitmap.createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, boolean filter);
      

      通过 filter = false 将产生块状的像素化图像。

      通过 filter = true 将使您的边缘更平滑。

      【讨论】:

      • 这个方法怎么用?该方法不存在!
      • 你投反对票了吗?你说它不存在是什么意思? Bitmap.createScaledBitmap 自 api lvl 1 起存在
      【解决方案4】:

      这是将位图图像分辨率(像素)降低到所需值的工作代码...

      import android.graphics.Bitmap;
      import android.graphics.BitmapFactory;
      import android.os.AsyncTask;
      import android.os.Bundle;
      import android.support.v7.app.AppCompatActivity;
      import android.widget.ImageView;
      import android.widget.Toast;
      
      public class ImageProcessActivity extends AppCompatActivity {
      
      
          private static final String TAG = "ImageProcessActivity";
          private static final String IMAGE_PATH = "/sdcard/DCIM/my_image.jpg";
      
          public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
      
              final int height = options.outHeight;
              final int width = options.outWidth;
              int inSampleSize = 1;
      
              if (height > reqHeight || width > reqWidth) {
      
                  final int halfHeight = height / 2;
                  final int halfWidth = width / 2;
      
                  // Calculate the largest inSampleSize value that is a power of 2 and keeps both
                  // height and width larger than the requested height and width.
                  while ((halfHeight / inSampleSize) >= reqHeight && (halfWidth / inSampleSize) >= reqWidth) {
      
                      inSampleSize *= 2;
                  }
              }
      
              return inSampleSize;
          }
      
          public static Bitmap decodeSampleDrawableFromFile(String file, int reqWidth, int reqHeight) {
      
              // First decode with inJustDecodeBounds=true to check dimensions
              final BitmapFactory.Options options = new BitmapFactory.Options();
              options.inJustDecodeBounds = true;
              BitmapFactory.decodeFile(file, options);
      
              // Calculate inSampleSize
              options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
      
              // Decode bitmap with inSampleSize set
              options.inJustDecodeBounds = false;
              return BitmapFactory.decodeFile(file, options);
          }
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_image_process);
      
              new AsyncTask<Object, Object, Bitmap>() {
      
                  @Override
                  protected Bitmap doInBackground(Object... objects) {
      
                      try {
      
                          return decodeSampleDrawableFromFile(IMAGE_PATH, 640, 640);
      
                      } catch (Exception e) {
      
                          e.printStackTrace();
      
                      }
                      return null;
                  }
      
                  @Override
                  protected void onPostExecute(Bitmap bitmap) {
                      super.onPostExecute(bitmap);
      
                      ((ImageView) findViewById(R.id.img)).setImageBitmap(bitmap);
                  }
              }.execute();
          }
      }
      

      步骤:

      1. 获取Bitmap.Options(图片信息)。

      2. 将大小采样到所需的样本大小。

      3. 使用给定选项(所需分辨率)从图像文件加载到Bitmapbitmap 对象。但在后台线程中执行此操作。

      4. 在 UI 线程 (onPostExecute()) 上将 Bitmap 图像加载到 ImageView

      【讨论】:

        猜你喜欢
        • 2013-06-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-18
        • 2012-06-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多