How to paint on Image and save that image in to Android?

前端 未结 1 1681
春和景丽
春和景丽 2021-01-03 13:47

I am new to canvas. I want to use the My already saved Image and want some paint on that image. after that i want to save it.

I know that with using Canvas it is pos

相关标签:
1条回答
  • 2021-01-03 14:33

    Your problem is your drawing over and over on your entire canvas:

     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
    

    Use logic roughly like this:

    @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();
    }
    

    Also don't forget to add necessary permission to save your file:

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

    outside <application></application> in your manifest file.

    0 讨论(0)
提交回复
热议问题