gpt4 book ai didi

java - RenderScript 模糊覆盖原始位图

转载 作者:太空狗 更新时间:2023-10-29 16:13:25 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com