简体   繁体   English

安卓图像质量

[英]image quality android

I need to solve OutOfMemory error that throws by bitmap size. 我需要解决由位图大小引发的OutOfMemory错误。

i used this code : 我用这个代码:

public static Bitmap decodeSampledBitmapFromSDcard(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);
    }

    public static int calculateInSampleSize(BitmapFactory.Options options,
            int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {
            if (width > height) {
                inSampleSize = Math.round((float) height / (float) reqHeight);
            } else {
                inSampleSize = Math.round((float) width / (float) reqWidth);
            }
        }
        return inSampleSize;
    }

but the quality of the image is bad. 但是图像质量不好。

i need another way to resize the image to make its bitmap object is small withoud destroy the quality. 我需要另一种方法来调整图像的大小,以使其位图对象很小而不会破坏质量。

int newWidth = 640;
int newHeight = 480;
loat scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
Matrix matrix = new Matrix();
                                        matrix.postScale(scaleWidth, scaleHeight);

Bitmap nResizedBitmap = Bitmap.createBitmap(b, 0, 0, width, height, matrix,true);
                                                                                FileOutputStream out = new FileOutputStream(...outfile...);                                         nResizedBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM