RenderScript模糊覆盖原始位图

AES*_*AES 2 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;
}
Run Code Online (Sandbox Code Playgroud)

这就是我想要的最终结果: 我想要这样.

但这就是我得到的: 我不想要这个

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

Lar*_*fer 6

问题在于如何创建输出位图.您正在使用一个Bitmap基于输入Bitmap对象为您提供不可变对象的调用.改变这一行:

Bitmap outputBitmap = Bitmap.createBitmap(image);
Run Code Online (Sandbox Code Playgroud)

是这样的:

Bitmap outputBitmap = image.copy(image.getConfig(), true);
Run Code Online (Sandbox Code Playgroud)

这将为您提供一个单独的Bitmap对象,它是原始和可变的副本.现在Renderscript正在修改原始版本(虽然它确实应该失败,因为它outputBitmap是不可变的.