2

我是新來的畫布。我想使用我已經保存的圖像,並希望在該圖像上塗一些顏料。之後我想保存它。如何在圖像上繪畫並將該圖像保存到Android?

我知道使用Canvas是可能的。我可以在圖像上繪畫,但是當我要存儲該圖像時,它只能保存繪畫。不是帶有繪畫的圖像。

所以,anybudy可以告訴我如何在圖像上繪畫和保存該圖像的代碼?

謝謝。

這裏是我的代碼,用於在SurfaceView上繪畫。 源代碼:

@Override 
     public void run() { 
      //Canvas canvas = null; 
      while (_run){ 

       try{ 
        canvas = mSurfaceHolder.lockCanvas(null); 
        if(mBitmap == null){ 
         mBitmap = Bitmap.createBitmap (1, 1, Bitmap.Config.ARGB_8888); 
        } 

        final Canvas c = new Canvas (mBitmap); 
        //canvas.drawColor(0, PorterDuff.Mode.CLEAR); 
        c.drawColor(0, PorterDuff.Mode.CLEAR); 
        canvas.drawColor(Color.WHITE); 
//     Bitmap kangoo = BitmapFactory.decodeResource(getResources(),R.drawable.icon); 

//     if(!(DrawingActivity.imagePath==null)){ 
//      canvas.drawBitmap(DrawingActivity.mBitmap, 0, 0, null); 
//     } 
        commandManager.executeAll(c); 
        canvas.drawBitmap (mBitmap, 0, 0,null); 
       } finally { 

        mSurfaceHolder.unlockCanvasAndPost(canvas); 
       } 
      } 

     } 

我使用mBitmap保存位圖到SD卡。

+0

告訴我們你的代碼? – ingsaurabh

+0

請查看更新後的問題。 –

回答

1

你的問題是你的繪圖一遍一遍整個畫布上:

final Canvas c = new Canvas (mBitmap); // creates a new canvas with your image is painted background 
c.drawColor(0, PorterDuff.Mode.CLEAR); // this makes your whole Canvas transparent 
canvas.drawColor(Color.WHITE); // this makes it all white on another canvas 
canvas.drawBitmap (mBitmap, 0, 0,null); // this draws your bitmap on another canvas 

使用邏輯大致是這樣的:

@Override 
public void run() { 

Canvas c = new Canvas(mBitmap); 


/* Paint your things here, example: c.drawLine()... Beware c.drawColor will fill your canvas, so your bitmap will be cleared!!!*/ 
... 

/* Now mBitmap will have both the original image & your painting */ 
String path = Environment.getExternalStorageDirectory().toString(); // this is the sd card 
OutputStream fOut = null; 
File file = new File(path, "MyImage.jpg"); 
fOut = new FileOutputStream(file); 
mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut); 
fOut.flush(); 
fOut.close(); 
} 

另外不要忘記添加必要的權限來保存文件:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

外部<application></application>在您的清單文件中。

相關問題