Je dessine des images 2D sur la toile.
Je veux enregistrer l'image affichée sur la toile dans un fichier JPEG, comment puis-je le faire?
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);
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);
}
}
}
Toile en 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();
}
}
}