简体   繁体   English

如何使用Renderscript将大图像模糊成较小的图像?

[英]How to use Renderscript to blur a large image into a smaller one?

Background 背景

Suppose I have a rather large bitmap, and I want to blur it into a smaller bitmap. 假设我有一个相当大的位图,我想将它模糊成一个较小的位图。

I actually ask this because I got weird crashes within Renderscript itself on some rare devices, so maybe it's something with the input (which in fact I already make sure is quite small): 我实际上问这个是因为我在Renderscript本身的一些罕见的设备上遇到了奇怪的崩溃,所以也许这是输入的东西(事实上我已经确定它很小):

java.lang.OutOfMemoryError: Failed to allocate a 1920169996 byte allocation with 16777216 free bytes and 67MB until OOM
   at android.renderscript.RenderScript$MessageThread.run(RenderScript.java:1111)

It's weird, because there is no way the input bitmap is so large (almost 2GB?!). 这很奇怪,因为输入位图无法如此大(几乎2GB?!)。

The problem 问题

The code below tries to do it, but for some reason, the output bitmap takes only the top-left area of the large bitmap into itself: 下面的代码试图这样做,但由于某种原因,输出位图只将大位图的左上区域变为自身:

        outputBitmap = Bitmap.createBitmap(srcBitmap.getWidth(), srcBitmap.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(outputBitmap);
        canvas.drawBitmap(srcBitmap, 0, 0, null);
        Allocation overlayAlloc = Allocation.createFromBitmap(rs, outputBitmap, MipmapControl.MIPMAP_NONE, Allocation.USAGE_GRAPHICS_TEXTURE);
        ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
        blur.setInput(overlayAlloc);
        blur.setRadius(radius);
        blur.forEach(overlayAlloc);
        overlayAlloc.copyTo(outputBitmap);
        rs.destroy();
        return outputBitmap;

The question 这个问题

How could this be? 怎么会这样? What should be done to fix it? 应该怎么做来解决它? Obviously, I could create a new bitmap as the input, and make it the same size as the small one, but this creates yet another bitmap that takes memory, and I'd like to avoid it. 显然,我可以创建一个新的位图作为输入,并使其与小的位图相同,但这会创建另一个占用内存的位图,我想避免它。

Before trying to save memory without creating small bitmaps try to check this: http://trickyandroid.com/advanced-blurring-techniques/ 在尝试保存内存而不创建小位图之前,请尝试检查: http//trickyandroid.com/advanced-blurring-techniques/

You can save time if you downscale you image, blur it and then upscale - there is no big difference between tricky blur and fair blur but it saves time. 你可以节省时间,如果你缩小你的图像,模糊它然后升级 - 棘手的模糊和公平模糊之间没有太大的区别,但它节省了时间。

PS. PS。 also you can check renderscript sample from this topic. 您也可以查看此主题的renderscript示例。 PSS. PSS。 also you can check this thread about blur: Fast Bitmap Blur For Android SDK 你也可以查看关于模糊的这个主题: Android SDK的快速位图模糊

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM