Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image on canvas to JPEG file

I'm drawing 2D images on the canvas.

I want to save image shown on canvas to JPEG file, how can I do it?

like image 554
d-man Avatar asked Feb 01 '10 05:02

d-man


People also ask

How do I save a canvas file as a JPEG?

You can save a canvas to an image file by using the method canvas. toDataURL() , that returns the data URI for the canvas' image data. The method can take two optional parameters canvas.

How do I convert a canvas object to an image?

function convertCanvasToImage() { let canvas = document. getElementById("canvas"); let image = new Image(); image. src = canvas. toDataURL(); return image; } let pnGImage = convertCanvasToImage(); document.


3 Answers

  1. create an empty bitmap
  2. create a new Canvas object and pass this bitmap to it
  3. call view.draw(Canvas) passing it the canvas object you just created. Refer Documentation of method for details.
  4. Use Bitmap.compress() to write the contents of the bitmap to an OutputStream, file maybe.

Pseudo code:

Bitmap  bitmap = Bitmap.createBitmap( view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); view.draw(canvas);  bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);  
like image 64
Samuh Avatar answered Oct 02 '22 17:10

Samuh


  1. set Drawing Cache Enabled
  2. Draw whatever you want
  3. Get Bitmap object from view
  4. Compress and save the image file
 import java.io.File; import java.io.FileOutputStream;  import android.app.Activity; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.os.Bundle; import android.util.Log; import android.view.View;  public class CanvasTest extends Activity {      @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);          Draw2d d = new Draw2d(this);         setContentView(d);     }      public class Draw2d extends View {          public Draw2d(Context context) {             super(context);             setDrawingCacheEnabled(true);         }          @Override         protected void onDraw(Canvas c) {             Paint paint = new Paint();             paint.setColor(Color.RED);                       c.drawCircle(50, 50, 30, paint);              try {                 getDrawingCache().compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(new File("/mnt/sdcard/arun.jpg")));             } catch (Exception e) {                 Log.e("Error--------->", e.toString());             }             super.onDraw(c);         }      }  }
like image 32
Arun Avatar answered Oct 02 '22 16:10

Arun


Canvas to JPG:

Canvas canvas = null;
FileOutputStream fos = null;
Bitmap bmpBase = null;

bmpBase = Bitmap.createBitmap(image_width, image_height, Bitmap.Config.ARGB_8888);
canvas = new Canvas(bmpBase);
// draw what ever you want canvas.draw...

// Save Bitmap to File
try
{
    fos = new FileOutputStream(your_path);
    bmpBase.compress(Bitmap.CompressFormat.PNG, 100, fos);

    fos.flush();
    fos.close();
    fos = null;
}
catch (IOException e)
{
    e.printStackTrace();
}
finally
{
    if (fos != null)
    {
        try
        {
            fos.close();
            fos = null;
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
}
like image 21
Harry Avatar answered Oct 02 '22 16:10

Harry