Android Camera (2)

Source: Internet
Author: User

Build a Camera Application

Some developers may want to customize the Camera user interface in their applications, or provide some special functions. Creating a custom Activity requires more code than using Intent, but it can provide a better user experience.

Follow these steps to create a customized Camera interface:

1. Check and access Camera: Create Code to check the existence of Camera and requested access;

2. Create a preview class: Inherit SurfaceView to create a Camera preview class and implement the SurfaceHolder interface. This class is used to preview the implementation images of Camera.

3. Build a preview layout: Once the Camera preview class is available, you can combine this preview class with the user interface control you want to create a view layout.

4. Create a listener for collection: connect the listener with the interface control that responds to user actions (such as pressing the button) to start image or video collection.

5. Collect and save files: write code for the images or videos you actually collect and save the output.

6. Release Camera: After using Camera, your application must release Camera so that other applications can use it.

Camera hardware is a shared resource that must be carefully managed. Therefore, when your application uses it, it cannot conflict with other applications. The following describes how to check the Camera hardware, how to request access to Camera, how to collect images and videos, and how to release Camera after the application is used.

Warning when your application uses Camera, remember to call the Camera. release () method to release the Camera object. If your application does not correctly release Camera, all subsequent views will fail to access Camera, including your own applications, and may close your application or other applications.

Check Camera hardware

If your application does not use the Special Declaration in the list to use Camera, you should check whether Camera is valid at runtime. Use the PackageManager. hasSystemFeature () method to perform this check, as shown in the following code:

/** Check if this device has a camera */

Private boolean checkCameraHardware (Context context ){

If (context. getPackageManager (). hasSystemFeature (PackageManager. FEATURE_CAMERA )){

// This device has a camera

Return true;

} Else {

// No camera on this device

Return false;

}

}

Android devices can have multiple Camera, such as Camera on the back of photography and Camera on the front of video sessions. Versions later than Android2.3 (API Level9) allow you to use the Camera. getNumberOfCameras () method to check the number of available Camera devices.

Access Camera

If you have determined that there is a Camera on the device where the application is running, you must obtain a Camera instance to request access to it (unless you use Intent to access Camera ).

Use the Camera. open () method to access the main Camera and ensure that any exceptions are caught, as shown in the following code:

/** A safe way to get an instance of the Camera object .*/

Public static Camera getCameraInstance (){

Camera c = null;

Try {

C = Camera. open (); // attempt to get a Camera instance

}

Catch (Exception e ){

// Camera is not available (in use or does not exist)

}

Return c; // returns null if camera is unavailable

}

Warning always check for exceptions when using Camera. open. If you do not check whether Camera is used or exists, your application will be shut down by the system.

On devices running Android2.3 (API Level9) or later, you can use the Camera. open (int) method to access the specified Camera. The sample code above accesses the first Camera, that is, the Camera on the back of multiple Camera devices.

Check the Camera function

Once you have obtained the permission to access Camera, you can use the Camera. getParameters () method to obtain more information about Camera capabilities. This method returns a Camera. Parameters object. When using API Level9 or a later version, you can use the Camera. getCameraInfo () method to determine whether Camera is in front or back of a device and the direction of the image.

Create a preview class

Valid photos or videos must be visible on the Camera of the device. The Camera preview class is a SurfaceView class that can display image data from Camera in real time. Therefore, users can capture scenes and images or videos.

The sample code in the following example demonstrates how to create a basic Camera preview class that can be included in the View layout. To capture the Callback events for creating and destroying views, this class implements the SurfaceHolder. Callback interface, which is used to match the preview input of Camera:

/** A basic Camera preview class */publicclassCameraPreviewextendsSurfaceViewimplementsSurfaceHolder. callback {privateSurfaceHolder mHolder; privateCamera mCamera; publicCameraPreview (Context context, Camera camera) {super (context); mCamera = camera; // Install a SurfaceHolder. callback so we get notified when the // underlying surface is created and destroyed. mHolder = getHolder (); mHolder. addCallback (this); // deprecated setting, but required on Android versions prior to 3.0 mHolder. setType (SurfaceHolder. SURFACE_TYPE_PUSH_BUFFERS);} publicvoid surfaceCreated (SurfaceHolder holder) {// The Surface has been created, now tell the camera where to draw the preview. try {mCamera. setPreviewDisplay (holder); mCamera. startPreview ();} catch (IOException e) {Log. d (TAG, "Error setting camera preview:" + e. getMessage () ;}} publicvoid surfaceDestroyed (SurfaceHolder holder) {// empty. take care of releasing the Camera preview in your activity .} publicvoid surfaceChanged (SurfaceHolder holder, int format, int w, int h) {// If your preview can change or rotate, take care of those events here. // Make sure to stop the preview before resizing or reformatting it. if (mHolder. getSurface () = null) {// preview surface does not exist return;} // stop preview before making changes try {mCamera. stopPreview ();} catch (Exception e) {// ignore: tried to stop a non-existent preview} // set preview size and make any resize, rotate or // reformatting changes here // start preview with new settings try {mCamera. setPreviewDisplay (mHolder); mCamera. startPreview ();} catch (Exception e) {Log. d (TAG, "Error starting camera preview:" + e. getMessage ());}}}

If you want to set a specific size for your Camera preview window, you can set the surfaceChanged () method as described in the preceding annotations. When setting the preview size, you must use the value from the getSupportedPreviewSizes () method. You cannot use arbitrary values in the setPreviewSize () method.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.