Android multimedia and camera details 6

Source: Internet
Author: User

Receive camera intent results
Once you build and execute an image or video camera intent, your application must be configured to receive intent results. this section describes how to intercept callbacks from the camera intent so that your application can perform more operations on the acquired images and videos.
To receive the result of an intent, you must overwrite the onActivityResult () method of the activity that starts the intent. The following Code demonstrates how to override onActivityResult () to get the result of the camera intent.

[Java]
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
}
}
}
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 your activity receives a successful result, the obtained images or videos are waiting for you to process them in a certain path.

 


Bind to a camera Application

Some developers may need a camera user interface to customize the appearance of their applications or provide special features. Creating a custom camera activity requires more code than using intent, but it can provide a more attractive user experience.

 

 

The general steps for creating a custom camera interface for your application are as follows:

Detect and use the camera-create code to check whether the camera exists and request to use it.

Create a preview class-create a camera preview class that derives from SurfaceView and implements the SurfaceHolder interface. This class allows you to preview real-time images from the camera.

Create a preview Layout-when you have a camera preview class, create a viewlayout containing the preview class and the user interface for control.

Create an image acquisition listener-create a listener for your control interface to respond to user actions.

Get and save it to a file-create the code to get the video or image and store it.

Release the camera-after using the camera, your app must release it correctly for other apps.

 

 

Camera hardware is a shared resource and must be managed carefully so that your applications do not conflict with other applications that use the camera. The following sections discuss how to detect cameras, how to request a camera, how to obtain images or videos, and how to release the camera.

 

Warning: Remember to call Camera. release () to release the Camera object when use is complete! If your app does not correctly release the camera, all subsequent use of the camera, including those sent by your own app, it will fail and your own application or other applications will be disabled.


Detect camera hardware

If your application does not use manifest to request the camera, you should check whether the camera is available at runtime. To perform this check, use the PackageManager. hasSystemFeature () method as follows:

[Java]
/** Check whether the device has a camera */
Devate 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;
}
}
/** Check whether the device has a camera */
Devate 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;
}
}


An Android device may have multiple cameras. For example, a video chat camera on the front and a photo camera on the back. Android2.3 (API Level 9) and later versions allow you to check the number of cameras and use the Camera. getNumberOfCameras () method.

 


Use a camera

If you are sure there is a camera on the device where your app is located, you must obtain an instance of the camera before requesting to use it (unless you are using intent to use the camera ).


To use the primary Camera, use the Camera. open () method and note that any exceptions are caught, as shown in the following code:

[Java]
/** A security method for obtaining the camera object instance */
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
}
/** A security method for obtaining the camera object instance */
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. Otherwise, if the camera is in use or does not exist, your application will be shut down by the system.

On devices running Android2.3 (API Level 9) or later, you can use Camera. open (int) to access a specific Camera. The above example code will access the first camera, that is, the opposite camera.


Check camera features

Once you get a Camera, you can use the Camera. getParameters () method to obtain information about the Camera's capabilities, mainly by checking the returned Camera. Parameters object to determine the Camera's capabilities. When APILevel 9 or later is used, use Camera. getCameraInfo () to determine whether the Camera is on the front or back, and whether its image is horizontal or vertical.

Author: nkmnkm

 

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.