Android multimedia and camera details 9

Source: Internet
Author: User

Release a camera
Devices are resources shared by applications on devices. your application can use it after obtaining the camera instance, and your application must be released after use, and when paused (Activity. onPause () should also be released. if your app does not properly release the camera, all subsequent operations that attempt to obtain the camera's right to use, including your own apps, will fail and cause those apps to close.


The following code uses Camera. release () to release a Camera object instance:


[Java]
Public class CameraActivity extends Activity {
Private Camera mCamera;
Private SurfaceView mPreview;
Private MediaRecorder mMediaRecorder;
 
...
 
@ Override
Protected void onPause (){
Super. onPause ();
ReleaseMediaRecorder (); // if you are using MediaRecorder, release it first
ReleaseCamera (); // release the camera immediately on pause event
}
 
Private void releaseMediaRecorder (){
If (mMediaRecorder! = Null ){
MMediaRecorder. reset (); // clear recorder configuration
MMediaRecorder. release (); // release the recorder object
MMediaRecorder = null;
MCamera. lock (); // lock camera for later use
}
}
 
Private void releaseCamera (){
If (mCamera! = Null ){
MCamera. release (); // release the camera for other applications
MCamera = null;
}
}
}

Public class CameraActivity extends Activity {
Private Camera mCamera;
Private SurfaceView mPreview;
Private MediaRecorder mMediaRecorder;

...

@ Override
Protected void onPause (){
Super. onPause ();
ReleaseMediaRecorder (); // if you are using MediaRecorder, release it first
ReleaseCamera (); // release the camera immediately on pause event
}

Private void releaseMediaRecorder (){
If (mMediaRecorder! = Null ){
MMediaRecorder. reset (); // clear recorder configuration
MMediaRecorder. release (); // release the recorder object
MMediaRecorder = null;
MCamera. lock (); // lock camera for later use
}
}

Private void releaseCamera (){
If (mCamera! = Null ){
MCamera. release (); // release the camera for other applications
MCamera = null;
}
}
}

Save a media file

A media file, such as a video or video, must be stored in the external storage of the device (SD Card ), in this way, the system space can be retained and users can obtain these files without using the device. A device has many path locations that can store media files. However, there are only two standard paths that developers must consider:

· Environment. getExternalStoragePublicDirectory (Environment. DIRECTORY_PICTURES)-This method returns a standard, shared, and recommended storage location. this path is shared (public), so other applications can easily discover, read, modify, and delete files under this path. if your application is uninstalled, the files saved in this path will be deleted. to avoid interfering with existing image or video files, you should create a subfolder for your application in the path, as shown in the following example. this method can be used from Android 2.2 (API Level 8). For calls to the equivalent early APIs, see "Save shared files.

· Context. getExternalFilesDir (Environment. DIRECTORY_PICTURES)-This method returns a standard storage image and video location, which is associated with your application. if your application is deleted, all files in this location will be deleted. files in this location do not force application security features, so other applications can read, modify, and delete them.


The following code example demonstrates how to create a file or Uri location for a media file. This file can be used when a camera of a device is called through intent or when a camera application is created:

 


[Java]
Public static final int MEDIA_TYPE_IMAGE = 1;
Public static final int MEDIA_TYPE_VIDEO = 2;
 
/** Create a file Uri to store an image or video */
Private static Uri getOutputMediaFileUri (int type ){
Return Uri. fromFile (getOutputMediaFile (type ));
}
 
/** Create a file to store images or videos */
Private static File getOutputMediaFile (int type ){
// For security, check whether the SDCard is mounted,
// Therefore, Environment. getExternalStorageState () should be called first ().
 
File mediaStorageDir = new File (Environment. getExternalStoragePublicDirectory (
Environment. DIRECTORY_PICTURES), "MyCameraApp ");
// If you want to create an image and share it among applications, and want your image to exist after it is detached from your application,
// This location is the best choice.
 
// If the storage location does not exist, create it.
If (! MediaStorageDir. exists ()){
If (! MediaStorageDir. mkdirs ()){
Log. d ("MyCameraApp", "failed to create directory ");
Return null;
}
}
 
// Create a media file name
String timeStamp = new SimpleDateFormat ("yyyyMMdd_HHmmss"). format (new Date ());
File mediaFile;
If (type = MEDIA_TYPE_IMAGE ){
MediaFile = new File (mediaStorageDir. getPath () + File. separator +
"IMG _" + timeStamp + ". jpg ");
} Else if (type = MEDIA_TYPE_VIDEO ){
MediaFile = new File (mediaStorageDir. getPath () + File. separator +
"VID _" + timeStamp + ". mp4 ");
} Else {
Return null;
}
 
Return mediaFile;
}

Public static final int MEDIA_TYPE_IMAGE = 1;
Public static final int MEDIA_TYPE_VIDEO = 2;

/** Create a file Uri to store an image or video */
Private static Uri getOutputMediaFileUri (int type ){
Return Uri. fromFile (getOutputMediaFile (type ));
}

/** Create a file to store images or videos */
Private static File getOutputMediaFile (int type ){
// For security, check whether the SDCard is mounted,
// Therefore, Environment. getExternalStorageState () should be called first ().

File mediaStorageDir = new File (Environment. getExternalStoragePublicDirectory (
Environment. DIRECTORY_PICTURES), "MyCameraApp ");
// If you want to create an image and share it among applications, and want your image to exist after it is detached from your application,
// This location is the best choice.

// If the storage location does not exist, create it.
If (! MediaStorageDir. exists ()){
If (! MediaStorageDir. mkdirs ()){
Log. d ("MyCameraApp", "failed to create directory ");
Return null;
}
}

// Create a media file name
String timeStamp = new SimpleDateFormat ("yyyyMMdd_HHmmss"). format (new Date ());
File mediaFile;
If (type = MEDIA_TYPE_IMAGE ){
MediaFile = new File (mediaStorageDir. getPath () + File. separator +
"IMG _" + timeStamp + ". jpg ");
} Else if (type = MEDIA_TYPE_VIDEO ){
MediaFile = new File (mediaStorageDir. getPath () + File. separator +
"VID _" + timeStamp + ". mp4 ");
} Else {
Return null;
}

Return mediaFile;
}

Note: Environment. getExternalStoragePublicDirectory () can only be used in Android 2.2 (API Level 8) or later. If your target device uses an earlier version, the application uses Environment. getExternalStorageDirectory () instead.

 

Camera Features
Android supports a large number of camera features, and you can adjust them in your application. compared to image format, flash mode, focus settings, and more. this section lists common camera features and briefly describes how to use them. most Camera features can use Camera. obtain or set Parameters. however, there are some important features that need to be better than Camera. parameters are configured in a more complex way. These features are described in the following sections:


· Metering and focus areas

· Face Detection

· Regular video shooting


For general features that are controlled by Camera. Parameters, see the "use Camera features" section.

 

Table 1. Common camera features introduced in API versions

Feature
API Level
Description
 
Face Detection
14
Detects the face position and uses the result to calculate the focus, light measurement, and white balance.
 
Metering Areas
14
Specify one or more areas in an image to calculate the white balance.
 
Focus Areas
14
Set one or more focus areas in an image
 
White Balance Lock
14
Stop or start automatic White Balance Adjustment
 
Exposure Lock
14
Stop or start Automatic Exposure Adjustment
 
Video Snapshot
14
Capture an image when shooting a video
 
Time Lapse Video
11
Timed recording
 
Multiple Cameras
9
One or more cameras are supported on one device, including front and back cameras.
 
Focus Distance
9
Focal Length
 
Zoom
8
Set the magnification of the image
 
Exposure Compensation
8
Reduce or increase the exposure level
 
GPS Data
5
The image contains or does not contain geographical location information.
 
White Balance
5
Set white balance mode
 
Focus Mode
5
Set how to focus on an object, such as automatic, fixed, Macro, and infinite.
 
Scene Mode
5
Scene mode, such as night, beach, snow or candlelight.
 
JPEG Quality
5
Sets the compression level for JPEG images.
 
Flash Mode
5
Set flashing to On, off, or automatic.
 
Color Effects
5
Apply a color effect to an image, such as black and white, brown, and reversed.
 
Anti-Bandingwww.2cto.com
5
Reduce the Edge Effect of color gradient during JPEG compression.
 
Picture Format
1
File Format of the specified Image
 
Picture Size
1
Specify the image width and height
 

Note: These features are not supported on all devices, because the hardware differences and software implementations are different.

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.