如何在Android中修复调整大小图像的质量?

时间:2015-01-12 06:14:13

标签: java android image image-processing

我有4608x3456图像,我想调整大小。我想将图像的分辨率调整为640x480,但是当我这样做时,图像的质量会下降。有没有办法降低图像分辨率,仍然有更好的图像质量?

调整大小的图像640x480:

enter image description here

     Bitmap temobitmpa = loadScaledBitmapFromUri(path,640,480);

     return getImageUri(getApplicationContext(), temobitmpa);



     public Bitmap loadScaledBitmapFromUri(String filePath, int width, int height) {

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;

        // calc aspect ratio
        int[] retval = calculateAspectRatio(options.outWidth, options.outHeight);

        options.inJustDecodeBounds = false;

        options.inSampleSize = calculateSampleSize(options.outWidth,
                options.outHeight, width, height);

        Log.i("test", "sample size::" + options.inSampleSize);
        Bitmap unscaledBitmap = BitmapFactory.decodeFile(filePath, options);
        return Bitmap.createScaledBitmap(unscaledBitmap, retval[0], retval[1],
                true);

    }


        private int[] calculateAspectRatio(int origWidth, int origHeight) {
            int newWidth = 640;
            int newHeight = 480;

            // If no new width or height were specified return the original bitmap
            if (newWidth <= 0 && newHeight <= 0) {
                newWidth = origWidth;
                newHeight = origHeight;
            }
            // Only the width was specified
            else if (newWidth > 0 && newHeight <= 0) {
                newHeight = (newWidth * origHeight) / origWidth;
            }
            // only the height was specified
            else if (newWidth <= 0 && newHeight > 0) {
                newWidth = (newHeight * origWidth) / origHeight;
            }
            // If the user specified both a positive width and height
            // (potentially different aspect ratio) then the width or height is
            // scaled so that the image fits while maintaining aspect ratio.
            // Alternatively, the specified width and height could have been
            // kept and Bitmap.SCALE_TO_FIT specified when scaling, but this
            // would result in whitespace in the new image.
            else {
                double newRatio = newWidth / (double) newHeight;
                double origRatio = origWidth / (double) origHeight;

                if (origRatio > newRatio) {
                    newHeight = (newWidth * origHeight) / origWidth;
                } else if (origRatio < newRatio) {
                    newWidth = (newHeight * origWidth) / origHeight;
                }
            }

            int[] retval = new int[2];
            retval[0] = newWidth;
            retval[1] = newHeight;
            return retval;
        }


        private int calculateSampleSize(int srcWidth, int srcHeight, int dstWidth,
                int dstHeight) {
            final float srcAspect = (float) srcWidth / (float) srcHeight;
            final float dstAspect = (float) dstWidth / (float) dstHeight;

            if (srcAspect > dstAspect) {
                return srcWidth / dstWidth;
            } else {
                return srcHeight / dstHeight;
            }
        }

1 个答案:

答案 0 :(得分:0)

您可以尝试使用此图像缩放库,它可以提供非常好的图像质量。

https://github.com/thebuzzmedia/imgscalr