Save image overlay with camera captured image underneith
<p>My application has a "photobooth" feature which will allow the user to take a picture with the camera and at the same time show an overlay image on top of the camera view.  After the picture is taken, i need to save what the user saw while taking the picture to the filesystem.  </p>

<p>I have experienced 1 big problem while developing a solution to this:  capturing an image with the compatible dimensions in which i can attach an overlay image to resulting in what the user saw while taking the picture.</p>

<p>It seems i cannot capture an image from the camera with defined dimensions(i have to basically pick from a list of them).  Some phones only can produce certain dimensions.</p>

<p>Since i cannot choose the size of the captured image, it seems as though i will be required to include many different sizes of the overlay image, and attach the best match to the captured image.  I can't just slap any old overlay on top of the camera image and make it look right.</p>

<p>Questions: </p>

<ul>
<li>Am i over-complicating this "camera image + overlay image creation" process?</li>
<li>What suggestions do you have in completing this task without the need of including several different sizes overlay images?</li>
</ul>

<p>Edit:
Here is my solution(brief).  Please realize this is not a perfect and maybe not most efficient way to do this, but it works.  Some things may be unnecessary/redundant but whatever!</p>

<p>Notes:</p>

<ul>
<li>this doesn't work too great on tablet devices.</li>
<li>the overlay image needs to be rotated to be in landscape mode(even though you will be taking the image holding the phone in portrait)</li>
<li>overlay size is 480x320</li>
<li>you need to force the activity to landscape mode while taking the picture(now the overlay looks like its portrait!)</li>
<li>i add the overlay image view using <code>addContentView(overlayImageView, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));</code></li>
</ul>

<p>...</p>

<pre><code>final Camera.PictureCallback jpegCallback = new Camera.PictureCallback() {
    @Override
    public void onPictureTaken(byte[] data, Camera camera) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        Bitmap mutableBitmap = null;
        try {
                    //for a PORTRAIT overlay and taking the image holding the phone in PORTRAIT mode
            mutableBitmap = BitmapFactory.decodeByteArray(data, 0, data.length, options).copy(Bitmap.Config.RGB_565, true);
            Matrix matrix = new Matrix();
            int width = mutableBitmap.getWidth();
            int height = mutableBitmap.getHeight();
            int newWidth = overlayImage.getDrawable().getBounds().width();
            int newHeight = overlayImage.getDrawable().getBounds().height();
            float scaleWidth = ((float) newWidth) / width;
            float scaleHeight = ((float) newHeight) / height;
            matrix.postScale(scaleWidth, scaleHeight);
            matrix.postRotate(90);

            Bitmap resizedBitmap = Bitmap.createBitmap(mutableBitmap, 0, 0, mutableBitmap.getWidth(), mutableBitmap.getHeight(), matrix, true);
            finalBitmap = resizedBitmap.copy(Bitmap.Config.RGB_565, true);
            Canvas canvas = new Canvas(finalBitmap);

            Bitmap overlayBitmap = BitmapFactory.decodeResource(getResources(), overlay);
            matrix = new Matrix();
            matrix.postRotate(90);
            Bitmap resizedOverlay = Bitmap.createBitmap(overlayBitmap, 0, 0, overlayBitmap.getWidth(), overlayBitmap.getHeight(), matrix, true);
            canvas.drawBitmap(resizedOverlay, 0, 0, new Paint());
            canvas.scale(50, 0);
            canvas.save();
            //finalBitmap is the image with the overlay on it
        }
        catch(OutOfMemoryError e) {
            //fail
        }
    }
}
</code></pre>
