我正在尝试使用RenderScript支持库中的ScriptIntrinsicBlur模糊图像。我正在使用gradle,并且已经使用this方法来使用RenderScript的支持库版本。

在Nexus 4上,一切正常且运行很快,但是当我在装有Android 2.3.3的三星Galaxy S上试用时,得到的图片如下:

我使用Roman Nurik的技巧将位图宽度设为4的倍数,但我不认为这是造成我问题的原因。我的模糊代码与this帖子中的代码完全一样。感谢您的任何建议。

这是我的代码:

获取 View 的位图并重新缩放位图:

public static Bitmap loadBitmapFromView(View v) {
        v.setDrawingCacheEnabled(false);
        v.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_LOW);
        v.setDrawingCacheEnabled(true);
        Bitmap b = v.getDrawingCache();
        return b;
    }

public static Bitmap scaledBitmap(Bitmap dest, float scale) {
        int scaledWidth = (int) (scale * dest.getWidth());
        if (scaledWidth % 4 != 0) { //workaround for bug explained here https://plus.google.com/+RomanNurik/posts/TLkVQC3M6jW
            scaledWidth = (scaledWidth / 4) * 4;
        }
        return Bitmap.createScaledBitmap(dest, scaledWidth, (int) (scale * dest.getHeight()), true);
    }

渲染脚本代码:
Bitmap bitmap = sentBitmap.copy(sentBitmap.getConfig(), true);

final RenderScript rs = RenderScript.create(context);
final Allocation input = Allocation.createFromBitmap(rs, sentBitmap, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
final Allocation output = Allocation.createTyped(rs, input.getType());
final ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
script.setRadius(radius);
script.setInput(input);
script.forEach(output);
output.copyTo(bitmap);
return bitmap;

我在logcat输出中注意到此错误:



之后,我的应用程序被卡住。

最佳答案

我在应用程序中从ScriptInstinsicBlur获得了非常相似的图像。花了一段时间才能弄清楚这一点,但事实证明MediaMetadataRetiever getFrameAt方法返回的是RGB_565的位图配置。在渲染脚本中应用模糊效果会给您带来时髦的效果,因为它显然不适用于565像素。

将我的位图转换为ARGB_8888,然后将其交给渲染脚本,这给了我想要的模糊效果。

希望这可以帮助其他人。

这是我发现的转换方法。 (来自我未加书签的帖子)

 private Bitmap RGB565toARGB888(Bitmap img) {
    int numPixels = img.getWidth()* img.getHeight();
    int[] pixels = new int[numPixels];

    //Get JPEG pixels.  Each int is the color values for one pixel.
    img.getPixels(pixels, 0, img.getWidth(), 0, 0, img.getWidth(), img.getHeight());

    //Create a Bitmap of the appropriate format.
    Bitmap result = Bitmap.createBitmap(img.getWidth(), img.getHeight(), Bitmap.Config.ARGB_8888);

    //Set RGB pixels.
    result.setPixels(pixels, 0, result.getWidth(), 0, 0, result.getWidth(), result.getHeight());
    return result;
}

10-08 17:25