[英]RenderScript blur overrides the original Bitmap
我正在嘗試使用RenderScript創建模糊的位圖,以將其設置為包含ImageView的LinearLayout的背景。 我還想要位圖的原始副本,以便可以在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.