2010-02-01 9 views
23

Ich zeichne 2D-Bilder auf der Leinwand.Bild auf Leinwand zu JPEG-Datei

Ich möchte das Bild auf Leinwand in JPEG-Datei gespeichert werden, wie kann ich es tun?

+0

Da der Link bezeichnet ist längst tot könnten Sie vielleicht ein bisschen mehr Kontext in die Frage selbst hinzufügen? – Flexo

Antwort

23
  1. ein leeres Bitmap
  2. ein neues Canvas-Objekt erstellen und diese Bitmap übergeben, um es
  3. Anruf view.draw (Canvas) ihm das Leinwand-Objekt übergeben Sie gerade erstellt hat. Refer Documentation of method for details.
  4. Verwenden Sie Bitmap.compress(), um den Inhalt der Bitmap in eine OutputStream-Datei zu schreiben.

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); 
+11

Hallo Samuh, Ich habe den Code versucht, es generiert eine JPEG-Datei, aber hat nicht die Leinwand gezeichnete Form oder was auch immer ich auf der Leinwand geschrieben habe. Jeder Kommentar. Danke, Ketan –

+0

Vielen Dank für die Antwort. –

12
  1. gesetzt Zeichnung Cache aktiviert
  2. Draw, was Sie wollen
  3. aus Sicht Bitmap-Objekt Get
  4. Komprimiert und die Image-Datei speichern

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

    } 

}
+7

Irgendwie ist es eine schlechte Idee, innerhalb der onDraw-Methode zu komprimieren und zu speichern. –

+0

es gibt mir null Zeiger Ausnahme – abh22ishek

6

Leinwand zu 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(); 
     } 
    } 
} 
Verwandte Themen