Java 如何在Android中获得更好的图像质量?

Java 如何在Android中获得更好的图像质量?,java,android,image,image-processing,Java,Android,Image,Image Processing,我有4608x3456图像,我想调整其大小。我想将图像的分辨率调整为640x480,但当我这样做时,图像的质量会下降。有没有一种方法可以降低图像分辨率,并且仍然具有更好的图像质量 已调整大小的图像640x480: Bitmap temobitmpa = loadScaledBitmapFromUri(path,640,480); return getImageUri(getApplicationContext(), temobitmpa); public B

我有4608x3456图像,我想调整其大小。我想将图像的分辨率调整为640x480,但当我这样做时,图像的质量会下降。有没有一种方法可以降低图像分辨率,并且仍然具有更好的图像质量

已调整大小的图像640x480:

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

Bitmap temobitmpa=loadScaledBitmapFromUri(路径640480);
返回getImageUri(getApplicationContext(),temobitmpa);
公共位图loadScaledBitmapFromUri(字符串文件路径、整型宽度、整型高度){
BitmapFactory.Options=new-BitmapFactory.Options();
options.inJustDecodeBounds=true;
//计算纵横比
int[]retval=calculateAspectRatio(options.outWidth,options.outHeight);
options.inJustDecodeBounds=false;
options.inSampleSize=计算样本大小(options.outWidth,
选项(高度、宽度、高度);
Log.i(“测试”,“样本大小::”+选项.inSampleSize);
位图unscaledBitmap=BitmapFactory.decodeFile(文件路径,选项);
返回位图.createScaledBitmap(unscaledBitmap,retval[0],retval[1],
正确的);
}
私有int[]计算光谱比率(int-origWidth、int-origHeight){
int newWidth=640;
int newHeight=480;
//如果未指定新的宽度或高度,则返回原始位图
如果(新的宽度和方向){
返回srcWidth/dstWidth;
}否则{
返回srchheight/dsthheight;
}
}

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


质量和图像分辨率是同义词,因此它实际上只是文字游戏。基本上,降低图像分辨率与降低质量是一样的,因为两者都是由图像包含的像素数定义的。但是android不提供BuffereImage对象