PhoneGap API help documentation-Camera (Camera)

Source: Internet
Author: User

"The camera object provides access to the default camera application of the device.
Method:
Camera. getPicture
Parameters:
CameraSuccess
CameraError
CameraOptions
Camera. getPicture

Choose to take a photo with the camera or get a picture from the photo album of the device. The image is returned as a base64 encoded string or image URI.

Simple Example:
Navigator. camera. getPicture (cameraSuccess, cameraError, [cameraOptions]);
Copy code
Note:
The camera. getPicture function opens the default Camera application of the device so that users can take photos (if Camera. sourceType is set to CAMERA. PictureSourceType. camera, this is also the default value ). Once the photo is taken, the camera application closes and restores the user application.
If Camera. sourceType = Camera. PictureSourceType. PHOTOLIBRARY or Camera. PictureSourceType. SAVEDPHOTOALBUM, the Select a photo dialog box is displayed. You can select a photo from the dataset.
The returned value is sent to the cameraSuccess callback function according to one of the following formats set by the user through the cameraOptions parameter:

A string containing a Base64 encoded photo image (default ).
A string that represents the location of the image file stored locally.

You can perform any processing on the encoded image or URI, for example:

Use tags to render images (see subsequent examples)
Stored as local data (LocalStorage, Lawnchair *, etc)
Send data to remote server

Note: The quality of photos taken by cameras on newer devices is quite good, encoding these photos with Base64 has resulted in memory problems for some devices (such as iPhone 4 and BlackBerry Torch 9800 ). Therefore, we strongly recommend that you set "Camera. destinationType" to FILE_URI.

Supported platforms:
Android
BlackBerry WebWorks (OS 5.0 or later)
IOS


Simple Example:
Take a photo and obtain a Base64 encoded image:
Navigator. camera. getPicture (onSuccess, onFail, {quality: 50 });

Unction onSuccess (imageData ){

Var image = document. getElementById ('myimage ');

Image. src = "data: image/jpeg; base64," + imageData;

}

Function onFail (message ){

Alert ('failed' because: '+ message );

}
Copy the code to take a photo and get the image file path:
Navigator. camera. getPicture (onSuccess, onFail, {quality: 50,

DestinationType: Camera. DestinationType. FILE_URI });

Function onSuccess (imageURI ){

Var image = document. getElementById ('myimage ');

Image. src = imageURI;

}

Function onFail (message ){

Alert ('failed' because: '+ message );

}
Copy the complete sample code:
<! DOCTYPE html>

<Html>

<Head>

<Title> Capture Photo </title>

<Script type = "text/javascript" charset = "UTF-8" src = "phonegap. js"> </script>

<Script type = "text/javascript" charset = "UTF-8">

Var pictureSource; // Image Source

Var destinationType; // set the format of the returned value

// Wait for the PhoneGap to connect to the device

Document. addEventListener ("deviceready", onDeviceReady, false );

// PhoneGap is ready for use!

Function onDeviceReady (){

PictureSource = navigator. camera. PictureSourceType;

DestinationType = navigator. camera. DestinationType;

}

// Called after obtaining the Base64 encoded data of a photo

Function onPhotoDataSuccess (imageData ){

// Uncomment to view Base64 encoded image data

// Console. log (imageData );

// Obtain the image handle

Var smallImage = document. getElementById ('smallimage ');

// Cancel hidden image elements

SmallImage. style. display = 'block ';

// Display the taken photo

// Use the embedded CSS rules to scale the image

SmallImage. src = "data: image/jpeg; base64," + imageData;

}

// Called when the URI of a photo is obtained successfully

Function onPhotoURISuccess (imageURI ){

// Uncomment to view the URI of the image file

// Console. log (imageURI );

// Obtain the image handle

Var largeImage = document. getElementById ('largeimage ');

// Cancel hidden image elements

LargeImage. style. display = 'block ';

// Display the taken photo

// Use the embedded CSS rules to scale the image

LargeImage. src = imageURI;

}

// Click the "Capture Photo" button to trigger the function.

Function capturePhoto (){

// Use the camera on the device to take a photo and obtain the Base64 encoded string format Image

Navigator. camera. getPicture (onPhotoDataSuccess, onFail, {quality: 50 });

}

// Click the "Capture Editable Photo" button to trigger the function.

Function capturePhotoEdit (){

// Use the camera on the device to take a photo and obtain an editable image in Base64 encoded string format

Navigator. camera. getPicture (onPhotoDataSuccess, onFail, {quality: 20, allowEdit: true });

}

// Click the "From Photo Library"/"From Photo Album" button to trigger the Function

Function getPhoto (source ){

// Obtain the image file URI from the Set Source

Navigator. camera. getPicture (onPhotoURISuccess, onFail, {quality: 50,

DestinationType: destinationType. FILE_URI, sourceType: source });

}

// Trigger this function when an error occurs

Function onFail (mesage ){

Alert ('failed' because: '+ message );

}

</Script>

</Head>

<Body>

<Button onclick = "capturePhoto ();"> Capture Photo </button> <br>

<Button onclick = "capturePhotoEdit ();"> Capture Editable Photo </button> <br>

<Button onclick = "getPhoto (pictureSource. PHOTOLIBRARY);"> From Photo Library </button> <br>

<Button onclick = "getPhoto (pictureSource. SAVEDPHOTOALBUM);"> From Photo Album </button> <br>

</Body>

</Html>
Copy the code cameraSuccess
 
Provides the onSuccess callback function for image data.
Function (imageData ){

// Process the image

}
Copy code parameters:
ImageData: Specifies the Base64 encoded image data or image file URI Based on the preset values of cameraOptions. (String type)


Example:
// Display the image

Function cameraCallback (imageData ){

Var image = document. getElementById ('myimage ');

Image. src = "data: image/jpeg; base64," + imageData;

}
Copy the code cameraError
 
The onError callback function that provides error information.
Function (message ){

// Display useful information

}
Copy code parameters:
Message: indicates the error message provided by the local code of the device. (String type)


CameraOptions
 


Optional parameters for custom camera settings.
{Quality: 75,

DestinationType: Camera. DestinationType. DATA_URL,

SourceType: Camera. PictureSourceType. CAMERA,

AllowEdit: true,

EncodingType: Camera. EncodingType. JPEG,

TargetWidth: 100,

TargetHeight: 100 };
Copy code options:
Quality: the quality of the stored images. The value range is [1, 0,100]. (Number type)
DestinationType: select the format of the returned data. Define using navigator. camera. DestinationType. (Number type)

Camera. DestinationType = {

DATA_URL: 0, // returns the image data of a Base64 encoded string

FILE_URI: 1 // URI of the returned Image File

}
Copy code
SourceType: Set the image source. Use nagivator. camera. PictureSourceType for definition. (Number type)

Camera. PictureSourceType = {

PHOTOLIBRARY: 0,

CAMERA: 1,

SAVEDPHOTOALBUM: 2

}
Copy code
AllowEdit: Allows simple editing of an image before selecting it. (Boolean type)
EncodingType: select the encoding method of the returned image file, which is defined by navigator. camera. EncodingType. (Number type)

Camera. EncodingType = {

JPEG: 0, // returns an image in JPEG format.

PNG: 1 // returns an image in PNG format

};
Copy code

TargetWidth: The scaled width of the image in pixels, which must be used together with targetHeight. The corresponding aspect ratio remains unchanged. (Number type)
TargetHeight: The scaled height of the image in pixels, which must be used together with targetWidth. The corresponding aspect ratio remains unchanged. (Number type)

Android exception:
Ignore the allowEdit parameter.
Camera. PictureSourceType. PHOTOLIBRARY or Camera. PictureSourceType. SAVEDPHOTOALBUM will display the same phase set.
Camera. EncodingType is not supported.


BlackBerry exception:
Ignore the quality parameter.
Ignore the sourceType parameter.
Ignore the allowEdit parameter.
After the photo is taken, the application must have the key injection permission to close the local Camera application.
Using a large image size may cause devices of a model with a high-resolution camera to be unable to encode the image (for example, Torch 9800 ).


Special situation of Palm:
Ignore the quality parameter.
Ignore the sourceType parameter.
Ignore the allowEdit parameter.


IPhone exception:
To avoid Memory Errors on some devices, set the quality value to be less than 50.
When destinationType. FILE_URI is used, the photos taken and edited by the camera are stored in the Documents/tmp directory of the application.
When the application ends, the program's Documents/tmp directory will be deleted. If the bucket size is critical, the developer can also delete the Directory through the navigator. fileMgr interface.
Author: LuoXianXion

 

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.