2016-05-05 3 views
1

を上書きします。私はまた、ビットマップの明確なオリジナルコピーが必要なので、ImageViewのイメージとして設定することができます。のrenderScriptブラーは、私が<em>ImageViewの</em>が含まれてい<em>のLinearLayout</em>の背景として設定するにはぼやけたビットマップを作成するために<strong>のrenderScript</strong>を使用しようとしています、元のビットマップ

ここに私のコードです:

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

それでは、どのように私は作るのですか同じビットマップのの2つのコピー、そのうちの1つがでぼやけたと他方クリアでオリジナルです

答えて

6

問題は出力ビットマップの作成方法にあります。あなたは入力Bitmapオブジェクトに基づいて不変のBitmapオブジェクトを与える呼び出しを使用しています。この行を変更します。

Bitmap outputBitmap = Bitmap.createBitmap(image); 

このように:

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

あなたの元と可変のコピーである別のBitmapオブジェクトを与えること。現在、レンダスクリプトはオリジナルを実際に修正しています(実際には、outputBitmapが不変だったために失敗するはずです)。

+0

それはありました!ありがとう – AES

+0

嬉しいです! –

関連する問題

 関連する問題