change bitmap resolution in Android app

2020-03-25 11:24发布

I'm writing an application that uses the phone's camera to take a picture, and then use it in my app. The thing is, the app runs out of memory, and it is probably because of the bitmap's high resolution. Is there a way to keep the bitmap at the same size, but lower the resolution?

Thanks!

3条回答
欢心
2楼-- · 2020-03-25 11:31

from jeet.chanchawat' s answer: 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;
    }
查看更多
够拽才男人
3楼-- · 2020-03-25 11:31

this can be done using Options.inSampleSize, when creating the bitmap

查看更多
Summer. ? 凉城
4楼-- · 2020-03-25 11:38

You can Set Its Width and Height

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

Function to Call

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

}

This are two more links which might Help You. Link 1 & Link 2

查看更多
登录 后发表回答