来自支持库的ScriptIntrinsicBlur对图像的缺陷

时间:2014-02-04 21:00:28

标签: android android-support-library renderscript

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

在我的Nexus 4上,一切正常并且非常快,但是当我在使用Android 2.3.3的三星Galaxy S上试用它时,我得到的图片看起来像这样:

noisy image

我使用Roman Nurik的提示将位图宽度设置为4的倍数,但我不认为这是我的问题的原因。我的模糊代码看起来与this帖子完全一样。谢谢你的任何建议。

这是我的代码:

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

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

Renderscript代码:

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输出中注意到了这个错误:

  

E / RenderScript_jni:没有GC方法

之后我的申请被冻结了。

1 个答案:

答案 0 :(得分:21)

我在应用程序中从ScriptInstinsicBlur获得了一个非常相似的图像。这需要一段时间来解决这个问题,但事实证明,MediaMetadataRetiever getFrameAt方法返回一个RGB_565的Bitmap配置。在renderscript中应用模糊可以获得时髦的效果,因为它显然不能处理565像素。

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

希望这有助于其他人。

这是我发现转换它的方法。 (它来自SO帖子,我没有书签)

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