Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I done something in the past where I have an overlay view on top of a surface view which holds camera preview.</p> <p>When the user wants to capture the pic, I take the next preview frame from Camera.PreviewCallback.onPreviewFrame() and then convert that from YUV to bitmap. </p> <p>I then get a canvas against that bitmap and pass that canvas to the Draw() method of the custom view class that is my overlay view, and it will draw whatever it had been drawing onscreen to my new canvas! I can then take the bitmap and save it a jpg/png to SD.</p> <p>Note that within that custom view class it has to deal with scaling due to the fact that the canvas it gets when being asked to draw on screen is a different size than the canvas it is given when asked to draw onto the canvas from the preview frame.</p> <p>Beware of preview sizes and out of memory errors here though as many phones have very large default preview frame sizes and making bitmaps to draw onto with canvas can chew up memory fast. Learn about how to use Bitmap.recycle() and how to monitor native heap to combat these.</p> <p>Hope that helps.</p> <p>After reading the follow up comments and the issue youre having with black/blank images I decided to post some psuedo code to show how I'm doing this...</p> <pre><code>// decode the camera data into an immutable bitmap Bitmap raw = CameraHelper.decodeYUV(frameInfo.frameData, frameInfo.frameSize.x, frameInfo.frameSize.y); frameInfo.frameData = null; // allow large bytearry to to get gc'd Bitmap cameraPic = raw.copy(Bitmap.Config.RGB_565, true); // make a mutable copy Canvas c2 = (new Canvas(cameraPic)); // create a canvas from the camera pic raw.recycle(); // we're done with raw bitmap now and can reclaim from native heap. reticleOverlay.draw(c2); // reticle overlay is a class that implements View String filename = acquireNextShotFilename()); // get a filename to write to SD FileOutputStream fileoutputStream; try { fileoutputStream = new FileOutputStream(filename); } catch (FileNotFoundException e) { MyLogger.e("Couldnt open fileoutputstream: ", e); throw e; } cameraPic.compress(CompressFormat.PNG, 100, fileoutputStream); cameraPic.recycle(); try { fileoutputStream.flush(); fileoutputStream.close(); } catch (IOException e) { MyLogger.e("Error writing to file: ", e); fileoutputStream.close(); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload