Android Camera (1)

Source: Internet
Author: User

The Android framework supports a variety of Camera and the available Camera functions on it. It allows you to capture photos and videos in applications. This article describes how to quickly and easily take photos and videos, and describes an advanced method for users to create custom Camera experience.

Notes

Before enabling applications to use the Camera feature on Android devices, consider how to use these hardware features:

1. Camera requirements: Consider whether the application must run on a Camera device. If so, declare the Camera requirements in the Application List;

2. snapshot or custom Camera: How are applications ready to use Camera? Are you only interested in capturing or video editing? Or should the application provide a new method for using Camera? For capturing or editing, consider using the existing Camera application. For the development of customized Camera functions, see "Building a Camera application" below"

3. Storage: Are images or videos intended to be visible only to this application, or are they shared with other applications, such as photo albums or other media and community applications? Do you want these images and videos to be available after the application is uninstalled? See how the following "Save a media file" solves these problems.

Basic

The Android framework supports photo taking and video recording through the Camera API or the Camera Intent. The following are related classes:

Camera

This class is the main API that controls the device Camera. When building a Camera application, it is used for taking photos or recording videos.

SurfaceView

This class is used to preview Camera in real time.

MediaRecorder

This class is used to record videos from Camera

Intent

MediaStore. ACTION_IMAGE_CAPTURE or MediaStore. ACTION_VIDEO_CAPTURE Intent actions are used to take photos or videos without using the Camera object directly.

List Declaration

Before using the Camera API to develop applications, make sure that the configuration file has an appropriate declaration to allow the use of Camera hardware and other related features.

1. Camera permission: The application must apply for the permission to use the device Camera.

<Uses-permissionandroid: name = "android. permission. CAMERA"/>

Note: If you use Camera using Intent, the application does not need to apply for this permission.

2. Camera function: the application must also declare the desired Camera function, for example:

<Uses-featureandroid: name = "android. hardware. camera"/>

Adding the Camera function to the Application List will prevent Google Play from installing the program on a device that does not contain Camera or does not support the required Camera function. For more information about how to use function-based Filtering for Google Play, see Google Play and function-based filtering.

If the application can use Camera or operate the Camera function correctly but does not need it, you should specify the android: required attribute in the list and set the attribute value to false:

<Uses-feature android: name = "android. hardware. camera "android: required =" false "/> 3. storage permission: if the application needs to save images or videos to the external storage of the device (such as the SD card), you must specify this permission in the list:

<Uses-permissionandroid: name = "android. permission. WRITE_EXTERNAL_STORAGE"/> 4. Audio Recording permission: for audio recordings collected by a video, the application must apply for the following permissions:

<Uses-permissionandroid: name = "android. permission. RECORD_AUDIO"/> 5. Location locating permission: if the application wants to mark the GPS location information for the image, it must apply for location locating permission:

<Uses-permissionandroid: name = "android. permission. ACCESS_FINE_LOCATION"/>

For more information about user location, see "positioning policy"

Use existing Camera applications

You can quickly enable the Camera or video recording without too much additional code in the application by using Intent to call an existing Android Camera application. A Camera Intent can apply to collect a photo or a video through the existing Camera application and the playback control it returns to the application. This section will show you how to use this technology to collect an image or a video.

The process of calling the Camera Intent follows these steps:

1. Compile a Camera Intent: Create an Intent object that applies for images or videos. One of the following Intent types is used:

MediaStore. ACTION_IMAGE_CAPTURE: apply for the Intent action type of the image function from an existing Camera application;

MediaStore. ACTION_VIDEO_CAPTURE: Specifies the Intent action type for applying video functions from an existing Camera application.

2. Start the Intent of Camera: Use startActivityForResult () to execute the Intent of Camera. After Intent is started, the user interface of the Camera application will be displayed on the screen, and the user can take pictures or video;

3. Receive Intent results: Create an onActivityResult () method in your application to receive reconciliation data from the Camera Intent. The system will call this method when the user completes taking a photo or video (or canceling the operation.

Image Collection Intent

Using Camera Intent to collect images is a shortcut for your application to take photos with the least amount of code. An Image Collection Intent can contain the following additional information:

MediaStore. EXTRA_OUTPUT: This setting requires a Uri object that specifies the image path and file name. This setting is optional, but it is strongly recommended. If this value is not specified, the Camera application saves the collected image to the default position with the default name. These default values are specified in the return fields of the Intent. getData () method.

The following example shows how to build an image collection Intent and execute it. In the example, the GetOutputMediaFileUri () method references the sample code in the "Save a media file" section below:

Private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;

Private Uri fileUri;

 

@ Override

Public void onCreate (Bundle savedInstanceState ){

Super. onCreate (savedInstanceState );

SetContentView (R. layout. main );

 

// Create Intent to take a picture and return control to the calling application

Intent intent = new Intent (MediaStore. ACTION_IMAGE_CAPTURE );

 

FileUri = getOutputMediaFileUri (MEDIA_TYPE_IMAGE); // create a file to save the image

Intent. putExtra (MediaStore. EXTRA_OUTPUT, fileUri); // set the image file name

 

// Start the image capture Intent

StartActivityForResult (intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE );

}

After the startActivityResult () method is executed, you will see the Camera application interface. After taking a photo (or canceling the operation), the user interface will return to your application, and you must listen to the onActivityResult () method to receive the Intent result, and continue the execution of your application.

Video collection Intent

Using Camera Intent to collect videos is a quick way for your applications to video with the least amount of code. The video collection Intent can contain the following additional information:

MediaStore. EXTRA_OUTPUT: This setting requires a URI to specify the path and file name for saving the video. Although it is optional, it is strongly recommended to use this setting. If this setting is not specified, the Camera application saves the collected video to the default location with the default name. The default setting is in the Intent. returned in the getData () method field.

MediaStore. EXTRA_VIDEO_QUALITY: The value range is 0 ~ At, the quality is the lowest and the file size is the lowest. At 1, the quality is the highest and the file size is the maximum.

MediaStore. EXTRA_DURATION_LIMIT: this value is in seconds and displays the video collection duration.

MediaStore. EXTRA_SIZE_LIMIT: The value is in bytes and the size of the file to be collected is limited.

The following example demonstrates how to construct a video collection Intent and execute it. The getOutputMediaFileUri () method in this example references the sample code in "Save a media file" below:

Private static final int CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE = 200;

Private Uri fileUri;

 

@ Override

Public void onCreate (Bundle savedInstanceState ){

Super. onCreate (savedInstanceState );

SetContentView (R. layout. main );

 

// Create new Intent

Intent intent = new Intent (MediaStore. ACTION_VIDEO_CAPTURE );

 

FileUri = getOutputMediaFileUri (MEDIA_TYPE_VIDEO); // create a file to save the video

Intent. putExtra (MediaStore. EXTRA_OUTPUT, fileUri); // set the image file name

 

Intent. putExtra (MediaStore. EXTRA_VIDEO_QUALITY, 1); // set the video image quality to high

 

// Start the Video Capture Intent

StartActivityForResult (intent, CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE );

}

When the startActivityForResult () method is executed, you will see an editable Camera application interface. After the user completes the video recording (or cancels the operation), the user interface will be returned to your application, and you must listen to the onActivityResult () method to receive the Intent result, and continue to execute your application.

Receive Camera Intent results

Once you build and execute a Camera Intent for an image or video, you must configure your application to receive Intent results. This section shows you how to listen for callbacks from the Camera Intent so that the application can further process the collected images or videos.

To receive Intent results, the onActivityResult () method must be rewritten in the Activity where Intent is started. The following example demonstrates how to override the onActivityResult () method to collect the returned results of the image Camera Intent or the video Camera Intent:

Private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;

Private static final int CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE = 200;

 

@ Override

Protected void onActivityResult (int requestCode, int resultCode, Intent data ){

If (requestCode = CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE ){

If (resultCode = RESULT_ OK ){

// Image captured and saved to fileUri specified in the Intent

Toast. makeText (this, "Image saved to: \ n" +

Data. getData (), Toast. LENGTH_LONG). show ();

} Else if (resultCode = RESULT_CANCELED ){

// User canceled the image capture

} Else {

// Image capture failed, advise user

}

}

 

If (requestCode = CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE ){

If (resultCode = RESULT_ OK ){

// Video captured and saved to fileUri specified in the Intent

Toast. makeText (this, "Video saved to: \ n" +

Data. getData (), Toast. LENGTH_LONG). show ();

} Else if (resultCode = RESULT_CANCELED ){

// User canceled the video capture

} Else {

// Video capture failed, advise user

}

}

}

Once the Activity receives a successful result, your application can access the captured images or videos in the specified position.

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.