PhoneGap Primer--4 Camera Camera

Source: Internet
Author: User
Tags base64

Today Saturday, every time to Saturday do not know why to go, so boring ah. There are in Beijing if Saturday is also boring, you can contact me all go out to play, qq598660766.

Nothing to do, then write a blog, today to introduce camera this object. The Camera object provides access to the device's default camera application.

The camera provides two methods:

Camera.getpicture
Camera.cleanup

You need to configure it before using the camera object:

App/res/xml/config.xml
<plugin name= "Camera" value= "Org.apache.cordova.CameraLauncher"/>

App/androidmanifest
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>

First, introduce the first method:

Navigator.camera.getPicture (Camerasuccess, Cameraerror, [cameraoptions]);

Choose to use a camera to take a picture or get a photo from a device photo album. The picture is returned in the form of a base64 encoded string or picture URI.

For Cameraoptions, this is mainly about the configuration parameters when using this method.

The Camera.getpicture function opens the device's default camera application, allowing the user to take photos (if Camera.sourcetype is set to Camera.PictureSourceType.CAMERA, which is also the default). Once the photo is finished, the camera application closes and resumes the user application.
If Camera.sourcetype = Camera.PictureSourceType.PHOTOLIBRARY or Camera.PictureSourceType.SAVEDPHOTOALBUM, the system pops up the Photo selection dialog box, where users can select photos from the set.
The return value is sent to the camerasuccess callback function in one of the following formats that the user sets through the cameraoptions parameter:
A string that contains the BASE64 encoded photo image (by default).
A string representing the location of the image file stored locally.


Let's look at a simple example

<! DOCTYPE html>  

The operation effect is as follows:


The small picture is the 12th button after the picture is displayed, the big picture is the three or four button to select from the album to display.

Here's how to cameraoptions:

The general format is as follows:

{quality:75,
  DestinationType:Camera.DestinationType.DATA_URL,
  sourcetype: Camera.PictureSourceType.CAMERA,
  allowedit:true,
  EncodingType:Camera.EncodingType.JPEG,
  targetwidth:100,
  targetheight:100,
  popoveroptions:camerapopoveroptions,
  savetophotoalbum:false};

Quality: The quality of the stored image, the range is [0,100]. (number Type)
destinationtype: Select the format of the returned data. Defined by the Navigator.camera.DestinationType. (number Type)

Camera.destinationtype = { 
	data_url:0,//Return image Data Base64 encoded string 
	file_uri:1//Return the URI of the image file 
SourceType: Set the source of the picture. Defined by the Nagivator.camera.PictureSourceType. (number Type)
Camera.picturesourcetype = { 
	photolibrary:0, 
	camera:1, 
	savedphotoalbum:2 
AllowEdit: Allows simple editing of a picture before it is selected for operation. (Boolean type)
Encodingtype: Select the encoding of the returned image file, defined by Navigator.camera.EncodingType. (number Type)
Camera.encodingtype = { 
	jpeg:0,//back to JPEG format Picture 
	png:1//back to PNG format picture 
Targetwidth: In pixels, the image contraction relaxation must be used in conjunction with Targetheight. The corresponding ratio of aspect to height remains unchanged. (number Type)
Targetheight: The image scaling height in pixels must be used in conjunction with Targetwidth. The corresponding ratio of aspect to height remains unchanged. (number Type)

These properties do not work on all devices, and the specific support for each device is also referenced in the official documentation.


The second method is described below

Camera.cleanup

Empty cached files that are generated when you use a camera to take photos.

Temporary files are generated when we use the following parameters, and this method is used to make the temporary files clear in time.

Camera.sourcetype = Camera.PictureSourceType.CAMERA and Camera.destinationtype = Camera.DestinationType.FILE_URI

Use the following methods:

Navigator.camera.cleanup (camerasuccess, cameraerror);

There are no parameters in the callback method of this method, just to call the corresponding method, you can alert some things in the corresponding method, to determine whether the empty.


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.