繁体   English   中英

RenderScript模糊将覆盖原始位图

[英]RenderScript blur overrides the original Bitmap

我正在尝试使用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;
}

这就是我希望最终结果如何: 我要这样

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

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

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

Bitmap outputBitmap = Bitmap.createBitmap(image);

是这样的:

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

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

暂无
暂无

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

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