This example describes how to use the camera of a device to preview the photo to be taken. In this example, you need a real mobile phone to run the video:
In this example, we use SurfaceView and android. hardware. Camera.
SurfaceView is a child class of View. It provides a Surface for plotting to allow plotting on this Surface in the working thread, so that time-consuming plotting operations do not need to be placed in the UI thread, instead, you can draw at your own pace in a separate work thread.
When using SurfaceView, a subclass must be derived from SurfaceView. This subclass must implement SurfaceHolder. callback interface. This interface provides several Callback functions to notify the application of the message about the Surface, such as when to create, change the size, and destroy the Surface. It is very important to implement these callback functions. The application can use these callback functions to adjust some descriptions of the Surface and know when the Surface can be drawn. A worker thread is usually defined in the SurfaceView derived class for plotting.
When drawing on the Surface, it is not through direct function call, but an indirect method to obtain the Surface handle (SurfaceHolder. therefore, getHolder () is called to obtain the Surface handle during Surface initialization. Generally, after obtaining the SurfaceHolder, call mHolder. addCallback (this) to respond to the SurfaceHolder. Callback event.
How to Use the Canvas of the Surface in the working thread to draw a graph is not involved in this example.
The following code defines the SurfaceView subclass and implements the SurfaceHolder. Callback interface.
[Java]
Class Preview extends SurfaceView implements SurfaceHolder. Callback {
SurfaceHolder mHolder;
Camera mCamera;
...
Public void surfaceCreated (SurfaceHolder holder ){
// The Surface has been created, acquire
// Camera and tell it where
// To draw.
MCamera = Camera. open ();
Try {
MCamera. setPreviewDisplay (holder );
} Catch (IOException exception ){
MCamera. release ();
MCamera = null;
// TODO: add more exception handling logic here
}
}
Public void surfaceDestroyed (SurfaceHolder holder ){
// Surface will be destroyed when we return,
// So stop the preview.
// Because the CameraDevice object is not
// Shared resource, it's very
// Important to release it when the activity is paused.
MCamera. stopPreview ();
MCamera. release ();
MCamera = null;
}
Public void surfaceChanged (SurfaceHolder holder,
Int format, int w, int h ){
// Now that the size is known, set up
// Camera parameters and begin
// The preview.
Camera. Parameters parameters = mCamera. getParameters ();
List <Size> sizes = parameters. getSupportedPreviewSizes ();
Size optimalSize = getOptimalPreviewSize (sizes, w, h );
Parameters. setPreviewSize (optimalSize. width, optimalSize. height );
MCamera. setParameters (parameters );
MCamera. startPreview ();
}
Class Preview extends SurfaceView implements SurfaceHolder. Callback {
SurfaceHolder mHolder;
Camera mCamera;
...
Public void surfaceCreated (SurfaceHolder holder ){
// The Surface has been created, acquire
// Camera and tell it where
// To draw.
MCamera = Camera. open ();
Try {
MCamera. setPreviewDisplay (holder );
} Catch (IOException exception ){
MCamera. release ();
MCamera = null;
// TODO: add more exception handling logic here
}
}
Public void surfaceDestroyed (SurfaceHolder holder ){
// Surface will be destroyed when we return,
// So stop the preview.
// Because the CameraDevice object is not
// Shared resource, it's very
// Important to release it when the activity is paused.
MCamera. stopPreview ();
MCamera. release ();
MCamera = null;
}
Public void surfaceChanged (SurfaceHolder holder,
Int format, int w, int h ){
// Now that the size is known, set up
// Camera parameters and begin
// The preview.
Camera. Parameters parameters = mCamera. getParameters ();
List <Size> sizes = parameters. getSupportedPreviewSizes ();
Size optimalSize = getOptimalPreviewSize (sizes, w, h );
Parameters. setPreviewSize (optimalSize. width, optimalSize. height );
MCamera. setParameters (parameters );
MCamera. startPreview ();
}
The Camera class is defined as the Camera hardware in the android. hardware package. The Camera class can be used to configure the Camera of the device, start/stop previewing, taking photos or videos. This class is the client of the Camera service (used to manage services of actual devices.
To use the camera of the device, you must define the permission and uses-feature in AndroidManifest. xml as follows:
<Uses-permission android: name = "android. permission. CAMERA"/>
<Uses-feature android: name = "android. hardware. camera"/>
<Uses-feature android: name = "android. hardware. camera. autofocus" android: required = "false"/>
The general steps for taking a photo using Camera are as follows:
Call open (int) to obtain an example object of Camera.
Use getParameters () to obtain the default configuration parameters of Camera.
If necessary, call setParameters (Camera. Parameters) to modify the Parameters of Camera.
If necessary, call setDisplayOrientation (int) to set the landscape to vertical or horizontal.
Use setPreviewDisplay (SurfaceHolder) to initialize a SurfaceHolder. If no Surface is specified, Camera cannot start previewing.
Call startPreview () to Start Screen preview. You must start previewing before taking a picture.
To take a photo, call takePicture (Camera. ShutterCallback, Camera. PictureCallback, Camera. PictureCallback, and Camera. PictureCallback) to notify the application of the completed photo taking event by using a callback function.
When taking a picture, Camera automatically stops previewing. You need to call startPreview to take a new picture.
Call stopPreview () to stop previewing.
Finally, do not use Camera to call release () to release resources so that other applications can use Camera.
Author: mapdigit