2012-07-18 20 views
5

superficie junto con la imagen cuando hago clic en el botón que es guardar sólo la superficie de este he intentado siguiente código¿Cómo guardar la imagen junto con la vista de superficie en Android?

camera.takePicture(shutterCallback, rawCallback, jpegCallback); 
ShutterCallback shutterCallback = new ShutterCallback() 
{ 
    public void onShutter() 
    { 
     Log.d(TAG, "onShutter'd"); 
    } 
}; 

PictureCallback rawCallback = new PictureCallback() 
{ 
    public void onPictureTaken(byte[] data, Camera camera) 
    { 
     Log.d(TAG, "onPictureTaken - raw"); 
    } 
}; 

PictureCallback jpegCallback = new PictureCallback() 
{ 
    public void onPictureTaken(byte[] data, Camera camera) 
    { 
     try 
     { 
      File root = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM),"Camera"); 
      if (!root.exists()) 
      { 
       root.mkdirs(); 
      } 

      FileOutputStream f = new FileOutputStream(new File(root,System.currentTimeMillis()+".jpg")); 
      int len1 = data.length; 
      f.write(data,0, len1); 
      f.close(); 
     } 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 
     finally 
     { 
     } 
    } 
}; 

por este código conseguir que en la superficie, ¿hay alguna posible ahorrar superficie a lo largo con la imagen? si alguien sabe por favor ayúdenme

Respuesta

0

Intenta usar este ejemplo. Tal como

import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.OutputStream; 

import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.os.Environment; 
import android.util.AttributeSet; 
import android.util.Log; 
import android.view.KeyEvent; 
import android.view.MotionEvent; 
import android.view.SurfaceHolder; 
import android.view.SurfaceView; 
import android.widget.Toast; 

public class FingerpaintView extends SurfaceView implements SurfaceHolder.Callback { 
    private static final String TAG = "FingerpaintView"; 

    private Paint foregroundPaint; 
    private Paint backgroundPaint; 
    private int width, height; 
    private int lastTouchX, lastTouchY; 
    private Bitmap pictureBitmap; 
    private Canvas pictureCanvas; 
    private final Context context; 

    public FingerpaintView(Context context) { 
     super(context); 
     this.context=context; 
     init(); 
    } 

    public FingerpaintView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     this.context=context; 
     init(); 
    } 

    private void init() { 
     setFocusableInTouchMode(true); 
     getHolder().addCallback(this); 
     foregroundPaint = new Paint(); 
     foregroundPaint.setColor(Color.WHITE); 
     foregroundPaint.setStrokeWidth(4); 
     backgroundPaint = new Paint(); 
     backgroundPaint.setColor(Color.BLACK); 
     lastTouchX = -1; 
     lastTouchY = -1; 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
     final int x = (int) event.getX(); 
     final int y = (int) event.getY(); 
     if ((event.getAction() == MotionEvent.ACTION_DOWN) || (event.getAction() == MotionEvent.ACTION_MOVE)) { 
      Log.d(TAG, "Touched " + x + "," + y); 
      if ((lastTouchX != -1) && (lastTouchY != -1)) { 
       pictureCanvas.drawLine(lastTouchX, lastTouchY, x, y, foregroundPaint); 
       draw(); 
      } 
      lastTouchX = x; 
      lastTouchY = y; 
     } else { 
      lastTouchX = -1; 
      lastTouchY = -1; 
     } 
     return true; 
    } 

    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
     if (keyCode == KeyEvent.KEYCODE_MENU){ 
      clear(); 
      return true; 
     } 
     return super.onKeyDown(keyCode, event); 
    } 

    @Override 
    public void surfaceChanged(SurfaceHolder holder, int format, int width, 
      int height) { 
     this.width = width; 
     this.height = height; 
     if (pictureBitmap != null) { 
      pictureBitmap.recycle(); 
     } 
     pictureBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565); 
     pictureCanvas = new Canvas(pictureBitmap); 
     clear(); 
     draw(); 
    } 

    public void draw() { 
     final Canvas c = getHolder().lockCanvas(); 
     if (c != null) { 
      c.drawBitmap(pictureBitmap, 0, 0, null); 
      getHolder().unlockCanvasAndPost(c); 
     } 
    } 

    public void clear() { 
     pictureCanvas.drawRect(0, 0, width, height, backgroundPaint); 
     draw(); 
    } 

    @Override 
    public void surfaceCreated(SurfaceHolder holder) { 
    } 

    @Override 
    public void surfaceDestroyed(SurfaceHolder holder) { 
     if (pictureBitmap != null) { 

      saveFile(pictureBitmap,"MyImage"); 


      pictureBitmap.recycle(); 
     } 
     pictureBitmap = null; 
    } 

    private void saveFile(Bitmap bitmap, String name) { 
     // String filename = String.valueOf(System.currentTimeMillis()) ; 
     String extStorageDirectory; 
     extStorageDirectory = Environment.getExternalStorageDirectory().toString(); 
     OutputStream outStream = null; 
      final File file = new File(extStorageDirectory, name); 
      try { 
      outStream = new FileOutputStream(file); 
      bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream); 
      outStream.flush(); 
      outStream.close(); 

      Toast.makeText(context, "Saved", Toast.LENGTH_LONG).show(); 

      } catch (final FileNotFoundException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      Toast.makeText(context, e.toString(), Toast.LENGTH_LONG).show(); 
      } catch (final IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      Toast.makeText(context, e.toString(), Toast.LENGTH_LONG).show(); 
      } 

      } 

    } 

Espero que sea útil.

Cuestiones relacionadas