在Android应用中更改位图分辨率

n00*_*mer 3 android resolution bitmap

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

谢谢!

Cün*_*eyt 6

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


Bha*_*vin 5

您可以设置其宽度和高度

Bitmap bm = ShrinkBitmap(imagefile, 150, 150);
Run Code Online (Sandbox Code Playgroud)

通话功能

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

}

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