简体   繁体   English

RenderScript模糊将覆盖原始位图

[英]RenderScript blur overrides the original Bitmap

I am trying to use RenderScript to create a blurred bitmap to set it as a background for a LinearLayout which contains an ImageView . 我正在尝试使用RenderScript创建模糊的位图,以将其设置为包含ImageViewLinearLayout的背景。 I also want a clear original copy of the bitmap so that I can set it as an image in the ImageView . 我还想要位图的原始副本,以便可以在ImageView中将其设置为图像。

Here's my code: 这是我的代码:

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

This is how I want the final result to be: 这就是我希望最终结果如何: 我要这样

But this is what I get: 但这就是我得到的: 我不要这个

So how do I make two copies of the same bitmap in which one of them is blurred and the other is clear and original ? 那么,如何制作同一位图的两个副本 ,其中一个模糊 ,另一个清晰且原始

The problem is with how the output bitmap is being created. 问题在于如何创建输出位图。 You're using a call that gives you an immutable Bitmap object based on an input Bitmap object. 您正在使用一个基于输入Bitmap对象为您提供不变的Bitmap对象的调用。 Change this line: 更改此行:

Bitmap outputBitmap = Bitmap.createBitmap(image);

to be this: 是这样的:

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

That will give you a separate Bitmap object which is a copy of the original and mutable. 这将为您提供一个单独的Bitmap对象,该对象是原始且可变的副本。 Right now Renderscript is really modifying the original (though it really should fail because the outputBitmap was immutable. 现在,Renderscript确实在修改原始文件(尽管它确实应该失败,因为outputBitmap是不可变的。

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

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