This project demonstrates how to implement a camera preview step-by-step.
GitHub URL: https://github.com/pikanji/CameraPreviewSample
- minimum -- Minimum code implementing camera preview.
- orientation -- Display in correct orientation.
- aspectratio -- Display in correct aspect ratio.
- aspectratio_refine -- Fixed a non-critical issue that onSurfaceChanged() is called twice.
- resizable -- Includes UI to arbitrarily set camera preview size.
- previewsize_fix -- Fixed a problem that preview couldn't start even with supported preview size.
- multicamera -- Supports multiple cameras.
- practical -- Allows the user program to set the layout size, position, and callbacks.
This release is a minimum implementation of camera preview, which may display the preview in wrong orientation and broken aspect ratio.
- CameraPreview.java -- Primary file implementing camera preview feature.
- MainActivity.java -- Driver Activity to demonstrate the usage of CameraPreview.java.
This release handles the orientation change to display in the correct orientation.
The same as the ones in "minimum."
This release displays camera preview in correct aspect ratio. Whatever the actual size of the preview is, it keeps the width & height ratio while it scale the preview to fit to the display. This although has a non-critical issue that might configure the camera parameters twice for one configuration change of the display. It is caused by the multiple invocation of onSurfaceChanged for one orientation change because layout parameter might be updated at the first call of onSurfaceChanged. This will be fixed in the next release.
The same as the ones in "orientation."
This release fixed the known issue explained in the previous section. It was also changed the algorithm to select the preview size, and now picks the size of which the ratio is closest to the one of the display. There is a known issue that the camera preview might fail to start even with the supported preview size on some devices. It is observed on Japanese device, IS03, so far.
The same as the ones in "aspectratio."
This release allows user to arbitrarily set the size of camera preview from the supported sizes. It still keeps the aspect ratio and scales it to fit to the display.
- CameraPreview.java -- Primary file implementing camera preview feature.
- MainActivity.java -- Driver Activity to demonstrate the usage of CameraPreview.java. This file includes both code that utilizes CameraPreview and ResizeableCameraPreview, one of them commented out.
- ResizableCameraPreview.java -- Extended class of CameraPreview allowing user to specify the preview size.
This release includes a fix that solves the issue that the preview can't be started even with supported preview size. The solution was to set picture size with supported picture size, as well as preview size. This release also supported more display rotation, Surface.ROTATION_270 in addition to Surface.ROTATION_90. This affects only Android 2.2 and later.
The same as "resizable."
- If the display rotation is changed from Surface.ROTATION_90 to Surface.ROTATION_270 or vice versa, the rotation of the camera preview is not updated.
- Orientation is not correct on an optimus pad (or all Honeycomb).
This release supports multiple camera. If the device has more than one camera on it, the user can choose a camera to display the camera preview. CameraPreview (and ResizableCameraPreview) takes a camera ID, which is usually 0 for back-facing camera and 1 for front-facing camera if exists. To switch between cameras, call stop() and instantiate another camera preview object with different camera ID. This release also shows how to stop the camera preview when it is not on the foreground. Stopping camera preview prevents from wasting the battery power and blocking other applications to use camera device.
The same as "picturesize_fix."
This version allows user class to specify the size and position of the camera preview. Also, implemented setter for a callback called on preview-ready, and wrapper for Camera.set*Callback. Refined sample driver activities.
- CameraPreview.java -- Primary file implementing camera preview feature.
- MainActivity.java -- UI that allows users to select a sample activity or a test activity to open.
- CameraPreviewSampleActivity.java -- Driver Activity to demonstrate the usage of CameraPreview.java.
- CameraPreviewTestActivity.java -- Driver Activity for testing CameraPreview.java.
- ResizableCameraPreview.java -- Extended class of CameraPreview allowing user to specify the preview size, for the purpose of testing.