title: Overlay images onto Camera preview SurfaceView and how to save them?
body: <p>I have a SurfaceView that is being used to draw images, and I would like to overlay them onto a live-feed from the phone's camera. I have read <a href="https://stackoverflow.com/questions/3548666/overlay-images-onto-camera-preview-surfaceview">Overlay images onto camera preview surfaceview</a>
  and <a href="https://stackoverflow.com/questions/2933882/how-to-draw-an-overlay-on-a-surfaceview-used-by-camera-on-android">how to draw an overlay on a surfaceview used by camera on android</a></p>
  
  <pre><code>    public class TestCameraOverlayActivity extends Activity { 
           Bitmap bitmap;
  
           /** Called when the activity is first created. */ 
           @Override 
           public void onCreate(Bundle savedInstanceState) { 
                 super.onCreate(savedInstanceState); 
                 requestWindowFeature(Window.FEATURE_NO_TITLE); 
                 Preview mPreview = new Preview(this); 
                 DrawOnTop mDraw = new DrawOnTop(this); 
                 bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
                 setContentView(mPreview); 
                 addContentView(mDraw, new LayoutParams (LayoutParams.WRAP_CONTENT,
                 LayoutParams.WRAP_CONTENT)); 
          } 
  
          class DrawOnTop extends View { 
              public DrawOnTop(Context context) { 
                  super(context); 
  
          } 
  
          @Override 
          protected void onDraw(Canvas canvas) { 
              Paint paint = new Paint(); 
              paint.setStyle(Paint.Style.STROKE); 
              paint.setColor(Color.BLACK); 
              canvas.drawText("Test Text", 10, 10, paint); 
              canvas.drawBitmap(bitmap, 0, 0, null);
              super.onDraw(canvas); 
          } 
      } 
  
      class Preview extends SurfaceView implements SurfaceHolder.Callback { 
          SurfaceHolder mHolder; 
          Camera mCamera; 
  
          Preview(Context context) { 
              super(context); 
              // Install a SurfaceHolder.Callback so we get notified when the 
              // underlying surface is created and destroyed. 
              mHolder = getHolder(); 
              mHolder.addCallback(this); 
              mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 
          } 
  
          public void surfaceCreated(SurfaceHolder holder) { 
              // The Surface has been created, acquire the camera and tell it where 
              // to draw. 
              mCamera = Camera.open(); 
               try {
          mCamera.setPreviewDisplay(holder);
          } catch (IOException e) {
  
              e.printStackTrace();
          } 
          } 
  
          public void surfaceDestroyed(SurfaceHolder holder) { 
             // Surface will be destroyed when we return, so stop the preview. 
             // Because the CameraDevice object is not a shared resource, it's very 
             // important to release it when the activity is paused. 
             mCamera.stopPreview(); 
             mCamera = null; 
          } 
  
          public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { 
              // Now that the size is known, set up the camera parameters and begin 
              // the preview. 
              Camera.Parameters parameters = mCamera.getParameters(); 
              parameters.setPreviewSize(w, h); 
              mCamera.setParameters(parameters); 
              mCamera.startPreview(); 
         } 
      } 
   }
  </code></pre>
  
  <p>if you run it you can one see image on the  preview SurfaceView, but my question is how to save them together as jpeg on the sdcard. Can you give some advice, if i need Format conversion so that i can save them at the same time. My partial solution to save the image only</p>
  
  <pre><code>/** Handles data for jpeg picture */
  PictureCallback jpegCallback = new PictureCallback() {
  
  public void onPictureTaken(byte[] data, Camera camera) {
      FileOutputStream outStream = null;
  try {
      // write to local sandbox file system
          // outStream = CameraDemo.this.openFileOutput(String.format("%d.jpg", 
             System.currentTimeMillis()), 0); 
  
          // Or write to sdcard
      outStream = new FileOutputStream(String.format("/sdcard/%d.jpg", 
          System.currentTimeMillis()));   
      outStream.write(data);
      outStream.close();
      Log.d(TAG, "onPictureTaken - wrote bytes: " + data.length);
  } catch (FileNotFoundException e) {
      e.printStackTrace();
  } catch (IOException e) {
      e.printStackTrace();
  } finally {
  }
  Log.d(TAG, "onPictureTaken - jpeg");
  }
  };
  </code></pre>