我正在尝试使用 RenderScript 支持库中的 ScriptIntrinsicBlur 对图像进行模糊处理。我正在使用 gradle,我已经使用了 this使用支持库版本的 RenderScript 的方法。
在我的 Nexus 4 上,一切正常而且速度非常快,但是当我在我的 Samsung Galaxy S 和 Android 2.3.3 上试用时,我得到的图片如下所示:
我使用 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 输出中注意到这个错误:
E/RenderScript_jni﹕ No GC methods
在那之后我的申请被卡住了。
最佳答案
我在我的应用程序中从 ScriptInstinsicBlur 获得了一张非常相似的图像。花了一段时间才弄清楚这一点,但事实证明 MediaMetadataRetiever getFrameAt 方法返回一个位图配置,即 RGB_565。在 renderscript 中应用模糊会得到奇怪的结果,因为它显然不适用于 565 像素。
将我的位图转换为 ARGB_8888,然后将其交给渲染脚本,得到了我想要的模糊效果。
希望这对其他人有帮助。
这是我找到的转换它的方法。 (它来 self 没有添加书签的 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;
}
关于android - 支持库中带有 ScriptIntrinsicBlur 的图像缺陷,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21563299/