0

我正在使用培训材料中的 BitmapFun 在 GridView 中显示我的图像。但是代码返回的图像非常模糊。ImageResizer我在方法类的第 184 行跟踪了罪魁祸首之一decodeSampledBitmapFromDescriptor

options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

这是实际的方法

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) {

            final int halfHeight = height / 2;
            final int halfWidth = width / 2;

            // Calculate the largest inSampleSize value that is a power of 2 and keeps both
            // height and width larger than the requested height and width.
            while (halfHeight / inSampleSize > reqHeight && halfWidth / inSampleSize > reqWidth) {
                inSampleSize *= 2;
            }

            // This offers some additional logic in case the image has a strange
            // aspect ratio. For example, a panorama may have a much larger
            // width than height. In these cases the total pixels might still
            // end up being too large to fit comfortably in memory, so we should
            // be more aggressive with sample down the image (=larger inSampleSize).

            long totalPixels = width * height / inSampleSize;

            // Anything more than 2x the requested pixels we'll sample down further
            final long totalReqPixelsCap = reqWidth * reqHeight * 2;

            while (totalPixels > totalReqPixelsCap) {
                inSampleSize *= 2;
                totalPixels /= 2;
            }
        }
        return inSampleSize;
    }

所以我想要的是不要那么激进地采样。但这会导致一些问题:既然这是加载位图的官方建议,那么将采样更改为不那么激进有什么问题?有没有人不得不更改此代码以获得更好质量的图片?我的调查是否正确?即这段代码是我的图像模糊的原因吗?这不是问题/疑问的详尽清单,但这应该让读者对我的担忧有所了解。最后:如何在不影响 BitmapFun 目的的情况下解决这个问题?显然,我去 BitmapFun 是有原因的:我的应用程序运行不正常并且经常崩溃。现在它没有崩溃,但图像太模糊了。

4

1 回答 1

0

我不得不更改此代码以获得更好质量的图片。

更改只是删除“附加逻辑”。最后一个while循环使图像看起来非常模糊。

- 编辑 - -

我认为“附加逻辑”中有一个错误导致模糊。

totalPixels 应该由 inSampleSize 的平方计算,因为图像是在 x 和 y 维度上采样的。所以只需改变这一行:

long totalPixels = width * height / (inSampleSize * inSampleSize);

以下是有关我的案例的更多详细信息:

请求宽度 = 357 请求高度 = 357

options.outWidth = 4128 options.outHeight = 3096

当它到达“附加逻辑”时,inSampleSize 为 8

所以,totalPixels = 1597536 和 totalReqPixelsCap = 254898(这是很远的)

于 2014-03-23T11:11:34.917 回答