簡體   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