How do I put graphics on top of my camera preview in Android?
<p>I see a couple of threads on this topic, but none of them seem to answer the question, so, I'm giving it another go...</p>

<p>Very simple idea... I want to some portion of the screen to preview (and shoot) the camera and the other portions of the screen to add my own graphical elements (buttons, colorful frame around the camera preview, etc).</p>

<p>The example that comes with Android fills the screen.  The obvious solution (that doesn't seem to be working) is to use my own SurfaceView (rather than the one they do in the example) and have it sitting in my XML.  The code (beneath) doesn't crash and reports that it's firing up the camera... it just never renders on the screen.</p>

<p>I have no idea what I'm doing incorrectly.  My guess is that it has something to do with the display thread never having the urge to redraw anything after it renders the initial screen, but I even tried adding a button and initiating the camera to render on the surface view upon button click, but that has not provoked it any further.
(most of this code is pulled directly out of the Android sample code, so, I'm sure it's correct.  I'm just doing something wrong in how I pull my View in after the fact, I think).</p>

<p>Any and all help greatly appreciated.</p>

<pre><code>public class TakePhoto extends Activity implements SurfaceHolder.Callback{
    /** Called when the activity is first created. */
    SurfaceHolder mHolder;
    Camera mCamera;


@Override
protected void onCreate(Bundle savedInstanceState) {            
    super.onCreate(savedInstanceState);


    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
    WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.take_photo);        

    try{
        SurfaceView sv = (SurfaceView) findViewById(R.id.camera_surface);
        mHolder = sv.getHolder();
        mHolder.addCallback(this);     
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);  
        toast("added callback");
    }catch(Exception e){
        toast("push: " + e);
    }       
}


public void surfaceCreated(SurfaceHolder holder) {

    mCamera = Camera.open();
    try {

       mCamera.setPreviewDisplay(mHolder);
    } catch (IOException exception) {
        mCamera.release();
        mCamera = null;
        toast("error:" + exception);
    }
}

public void surfaceDestroyed(SurfaceHolder holder) {
    mCamera.stopPreview();
    mCamera.release();
    mCamera = null;
}


private Size getOptimalPreviewSize(List&lt;Size&gt; sizes, int w, int h) {
    final double ASPECT_TOLERANCE = 0.05;
    double targetRatio = (double) w / h;
    if (sizes == null) return null;

    Size optimalSize = null;
    double minDiff = Double.MAX_VALUE;

    int targetHeight = h;


    for (Size size : sizes) {
        double ratio = (double) size.width / size.height;
        if (Math.abs(ratio - targetRatio) &gt; ASPECT_TOLERANCE) continue;
        if (Math.abs(size.height - targetHeight) &lt; minDiff) {
            optimalSize = size;
            minDiff = Math.abs(size.height - targetHeight);
        }
    }


    if (optimalSize == null) {
        minDiff = Double.MAX_VALUE;
        for (Size size : sizes) {
            if (Math.abs(size.height - targetHeight) &lt; minDiff) {
                optimalSize = size;
                minDiff = Math.abs(size.height - targetHeight);
            }
        }
    }
    return optimalSize;
}

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {

    Camera.Parameters parameters = mCamera.getParameters();

    List&lt;Size&gt; sizes = parameters.getSupportedPreviewSizes();
    Size optimalSize = getOptimalPreviewSize(sizes, w, h);        

    parameters.setPreviewSize(optimalSize.width, optimalSize.height);        

    mCamera.setParameters(parameters);
    mCamera.startPreview();
}

private void toast(String str){
    Context context = getApplicationContext();       
    int duration = Toast.LENGTH_SHORT;
    Toast toast = Toast.makeText(context, str, duration);
    toast.show();

}
</code></pre>

<p>}</p>
