Game Development Reference
In-Depth Information
3.
Finally, it installs a callback to be invoked for every preview frame
in addition to displaying them on the screen. The callback will be
repeatedly called for as long as preview is active. This method can
be called at any time, even while preview is live, as seen in this code
fragment:
camera = Camera.open();
camera.setPreviewDisplay(holder);
camera.setPreviewCallback(new Camera.PreviewCallback() {
public void onPreviewFrame(byte[] data, Camera cam) {
// Here we have a frame image in NV21 format
}
});
Tip The default image format of the camera preview is YCbCr_420_SP (NV21). This is the format
that will be received on the preview callback onPreviewFrame . Nevertheless, the format can be
controlled by calling camera.setPreviewFormat(format) . The image format constants are
defined in the ImageFormat class.
surfaceChanged
surfaceChanged is called immediately after surfaceCreated . It receives a reference to the
holder of the surface as well as the image format, plus width and height. In this case, this
method starts capturing and drawing preview frames to the screen. Preview will not actually
start until a surface is supplied with setPreviewDisplay(SurfaceHolder) or setPreviewTexture
(SurfaceTexture) , as shown in this code fragment:
Camera.Parameters parameters = camera.getParameters();
Log.d(TAG, "Preview size: " + parameters.getPreviewSize());
// Start preview: Default pix format YCbCr_420_SP (NV21)
camera.startPreview();
surfaceDestroyed
surfaceDestroyed is called immediately before a surface gets destroyed. After returning from
this call, you should no longer try to access this surface. If you have a rendering thread that
directly accesses the surface, you must ensure that thread is no longer touching the surface
before returning from this function. The following function must be used to stop the camera
preview and dispose of resources:
camera.stopPreview();
camera = null;
 
Search WWH ::




Custom Search