java - RenderScript 模糊覆盖原始位图

标签 java android bitmap blur renderscript

我正在尝试使用 RenderScript 创建模糊位图,将其设置为包含 ImageViewLinearLayout 的背景。我还想要位图的清晰原始副本,以便我可以将其设置为 ImageView 中的图像。

这是我的代码:

ImageView mainImage;

Bitmap mainBMP, blurredBMP

LinearLayout background;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_work_area);

    getImage(); // obtain bitmap from file
    mainImage.setImageBitmap(mainBMP); // set the original bitmap in imageview 

    // create a blurred bitmap drawable and set it as background for linearlayout
    BitmapDrawable drawable = new BitmapDrawable(getResources(), blur(mainBMP)); 
    mainBackground.setBackground(drawable); 


    registerForContextMenu(objectImage);
    registerForContextMenu(textArea);

}

private void getImage(){
    String filename = getIntent().getStringExtra("image");
    try {
        FileInputStream is = this.openFileInput(filename);
        mainBMP = BitmapFactory.decodeStream(is);
        is.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

@TargetApi(17)
public Bitmap blur(Bitmap image) {
    if (null == image) return null;

    Bitmap outputBitmap = Bitmap.createBitmap(image);
    final RenderScript renderScript = RenderScript.create(this);
    Allocation tmpIn = Allocation.createFromBitmap(renderScript, image);
    Allocation tmpOut = Allocation.createFromBitmap(renderScript, outputBitmap);

    //Intrinsic Gausian blur filter
    ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.create(renderScript, Element.U8_4(renderScript));
    theIntrinsic.setRadius(BLUR_RADIUS);
    theIntrinsic.setInput(tmpIn);
    theIntrinsic.forEach(tmpOut);
    tmpOut.copyTo(outputBitmap);
    return outputBitmap;
}

这就是我想要的最终结果: I want like this.

但这就是我得到的: I dont want this

那么,如何制作同一位图的两份,其中一份是模糊,另一份是清晰和原始

最佳答案

问题在于如何创建输出位图。您使用的调用会根据输入的 Bitmap 对象为您提供不可变的 Bitmap 对象。更改此行:

Bitmap outputBitmap = Bitmap.createBitmap(image);

是这样的:

Bitmap outputBitmap = image.copy(image.getConfig(), true);

这将为您提供一个单独的 Bitmap 对象,它是原始对象的副本并且是可变的。现在 Renderscript 确实在修改原始文件(尽管它确实应该失败,因为 outputBitmap 是不可变的。

关于java - RenderScript 模糊覆盖原始位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37045756/

相关文章:

java - 使用 java 解析器删除 XML 节点

java - java 中 PDF 文件的文档模板(寻找工具/库)

android - 在三星 Galaxy S 上调用 RingTonePreference 时出现 NullPointerException

android - 如何将常量和其他变量发送到另一个 Activity ?

android - android marshmallow 中 "identity"的 App 权限在哪里

c# - 如何在不使用互操作的情况下创建动态鼠标光标 .NET?

java - Hazelcast 实现 java.io.NotSerializedException 时出现错误 JCache

java - 如何减少多个 Activity 的内存分配

android - 如何在图像位图中使用ontouchevent绘制一条线作为android中的背景

java - CyclicBarrier.getNumberWaiting() 准确吗?