2 Star 0 Fork 0

Wingsfrog / wfPluginCamera

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

Build Status

cordova-plugin-camera

This plugin defines a global navigator.camera object, which provides an API for taking pictures and for choosing images from the system's image library.

Although the object is attached to the global scoped navigator, it is not available until after the deviceready event.

document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
    console.log(navigator.camera);
}

Installation

This requires cordova 5.0+

cordova plugin add cordova-plugin-camera

Older versions of cordova can still install via the deprecated id

cordova plugin add org.apache.cordova.camera

It is also possible to install via repo url directly ( unstable )

cordova plugin add https://github.com/apache/cordova-plugin-camera.git

How to Contribute

Contributors are welcome! And we need your contributions to keep the project moving forward. You can report bugs, improve the documentation, or contribute code.

There is a specific contributor workflow we recommend. Start reading there. More information is available on our wiki.

:warning: Found an issue? File it on JIRA issue tracker.

Have a solution? Send a Pull Request.

In order for your changes to be accepted, you need to sign and submit an Apache ICLA (Individual Contributor License Agreement). Then your name will appear on the list of CLAs signed by non-committers or Cordova committers.

And don't forget to test and document your code.

This documentation is generated by a tool

:warning: Run npm install in the plugin repo to enable automatic docs generation if you plan to send a PR.
jsdoc-to-markdown is used to generate the docs.
Documentation consists of template and API docs produced from the plugin JS code and should be regenerated before each commit (done automatically via husky, running npm run gen-docs script as a precommit hook - see package.json for details).


API Reference


camera

camera.getPicture(successCallback, errorCallback, options)

Takes a photo using the camera, or retrieves a photo from the device's image gallery. The image is passed to the success callback as a Base64-encoded String, or as the URI for the image file.

The camera.getPicture function opens the device's default camera application that allows users to snap pictures by default - this behavior occurs, when Camera.sourceType equals Camera.PictureSourceType.CAMERA. Once the user snaps the photo, the camera application closes and the application is restored.

If Camera.sourceType is Camera.PictureSourceType.PHOTOLIBRARY or Camera.PictureSourceType.SAVEDPHOTOALBUM, then a dialog displays that allows users to select an existing image. The camera.getPicture function returns a CameraPopoverHandle object, which can be used to reposition the image selection dialog, for example, when the device orientation changes.

The return value is sent to the cameraSuccess callback function, in one of the following formats, depending on the specified cameraOptions:

  • A String containing the Base64-encoded photo image.

  • A String representing the image file location on local storage (default).

You can do whatever you want with the encoded image or URI, for example:

  • Render the image in an <img> tag, as in the example below

  • Save the data locally (LocalStorage, Lawnchair, etc.)

  • Post the data to a remote server

NOTE: Photo resolution on newer devices is quite good. Photos selected from the device's gallery are not downscaled to a lower quality, even if a quality parameter is specified. To avoid common memory problems, set Camera.destinationType to FILE_URI rather than DATA_URL.

Supported Platforms

  • Android
  • BlackBerry
  • Browser
  • Firefox
  • FireOS
  • iOS
  • Windows
  • WP8
  • Ubuntu

More examples here. Quirks here.

Kind: static method of camera

Param Type Description
successCallback onSuccess
errorCallback onError
options CameraOptions CameraOptions

Example

navigator.camera.getPicture(cameraSuccess, cameraError, cameraOptions);

camera.cleanup()

Removes intermediate image files that are kept in temporary storage after calling camera.getPicture. Applies only when the value of Camera.sourceType equals Camera.PictureSourceType.CAMERA and the Camera.destinationType equals Camera.DestinationType.FILE_URI.

Supported Platforms

  • iOS

Kind: static method of camera
Example

navigator.camera.cleanup(onSuccess, onFail);

function onSuccess() {
    console.log("Camera cleanup success.")
}

function onFail(message) {
    alert('Failed because: ' + message);
}

camera.onError : function

Callback function that provides an error message.

Kind: static typedef of camera

Param Type Description
message string The message is provided by the device's native code.

camera.onSuccess : function

Callback function that provides the image data.

Kind: static typedef of camera

Param Type Description
imageData string Base64 encoding of the image data, or the image file URI, depending on cameraOptions in effect.

Example

// Show image
//
function cameraCallback(imageData) {
   var image = document.getElementById('myImage');
   image.src = "data:image/jpeg;base64," + imageData;
}

camera.CameraOptions : Object

Optional parameters to customize the camera settings.

Kind: static typedef of camera
Properties

Name Type Default Description
quality number 50 Quality of the saved image, expressed as a range of 0-100, where 100 is typically full resolution with no loss from file compression. (Note that information about the camera's resolution is unavailable.)
destinationType DestinationType FILE_URI Choose the format of the return value.
sourceType PictureSourceType CAMERA Set the source of the picture.
allowEdit Boolean true Allow simple editing of image before selection.
encodingType EncodingType JPEG Choose the returned image file's encoding.
targetWidth number Width in pixels to scale image. Must be used with targetHeight. Aspect ratio remains constant.
targetHeight number Height in pixels to scale image. Must be used with targetWidth. Aspect ratio remains constant.
mediaType MediaType PICTURE Set the type of media to select from. Only works when PictureSourceType is PHOTOLIBRARY or SAVEDPHOTOALBUM.
correctOrientation Boolean Rotate the image to correct for the orientation of the device during capture.
saveToPhotoAlbum Boolean Save the image to the photo album on the device after capture.
popoverOptions CameraPopoverOptions iOS-only options that specify popover location in iPad.
cameraDirection Direction BACK Choose the camera to use (front- or back-facing).

Camera

Camera.DestinationType : enum

Kind: static enum property of Camera
Properties

Name Type Default Description
DATA_URL number 0 Return base64 encoded string. DATA_URL can be very memory intensive and cause app crashes or out of memory errors. Use FILE_URI or NATIVE_URI if possible
FILE_URI number 1 Return file uri (content://media/external/images/media/2 for Android)
NATIVE_URI number 2 Return native uri (eg. asset-library://... for iOS)

Camera.EncodingType : enum

Kind: static enum property of Camera
Properties

Name Type Default Description
JPEG number 0 Return JPEG encoded image
PNG number 1 Return PNG encoded image

Camera.MediaType : enum

Kind: static enum property of Camera
Properties

Name Type Default Description
PICTURE number 0 Allow selection of still pictures only. DEFAULT. Will return format specified via DestinationType
VIDEO number 1 Allow selection of video only, ONLY RETURNS URL
ALLMEDIA number 2 Allow selection from all media types

Camera.PictureSourceType : enum

Kind: static enum property of Camera
Properties

Name Type Default Description
PHOTOLIBRARY number 0 Choose image from picture library (same as SAVEDPHOTOALBUM for Android)
CAMERA number 1 Take picture from camera
SAVEDPHOTOALBUM number 2 Choose image from picture library (same as PHOTOLIBRARY for Android)

Camera.PopoverArrowDirection : enum

Matches iOS UIPopoverArrowDirection constants to specify arrow location on popover.

Kind: static enum property of Camera
Properties

Name Type Default
ARROW_UP number 1
ARROW_DOWN number 2
ARROW_LEFT number 4
ARROW_RIGHT number 8
ARROW_ANY number 15

Camera.Direction : enum

Kind: static enum property of Camera
Properties

Name Type Default Description
BACK number 0 Use the back-facing camera
FRONT number 1 Use the front-facing camera

CameraPopoverOptions

iOS-only parameters that specify the anchor element location and arrow direction of the popover when selecting images from an iPad's library or album. Note that the size of the popover may change to adjust to the direction of the arrow and orientation of the screen. Make sure to account for orientation changes when specifying the anchor element location.

Param Type Default Description
[x] Number 0 x pixel coordinate of screen element onto which to anchor the popover.
[y] Number 32 y pixel coordinate of screen element onto which to anchor the popover.
[width] Number 320 width, in pixels, of the screen element onto which to anchor the popover.
[height] Number 480 height, in pixels, of the screen element onto which to anchor the popover.
[arrowDir] PopoverArrowDirection ARROW_ANY Direction the arrow on the popover should point.

CameraPopoverHandle

A handle to an image picker popover.

Supported Platforms

  • iOS

Example

var cameraPopoverHandle = navigator.camera.getPicture(onSuccess, onFail,
{
    destinationType: Camera.DestinationType.FILE_URI,
    sourceType: Camera.PictureSourceType.PHOTOLIBRARY,
    popoverOptions: new CameraPopoverOptions(300, 300, 100, 100, Camera.PopoverArrowDirection.ARROW_ANY)
});

// Reposition the popover if the orientation changes.
window.onorientationchange = function() {
    var cameraPopoverOptions = new CameraPopoverOptions(0, 0, 100, 100, Camera.PopoverArrowDirection.ARROW_ANY);
    cameraPopoverHandle.setPosition(cameraPopoverOptions);
}

camera.getPicture Errata

Example

Take a photo and retrieve the image's file location:

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);
}

Take a photo and retrieve it as a Base64-encoded image:

/**
 * Warning: Using DATA_URL is not recommended! The DATA_URL destination
 * type is very memory intensive, even with a low quality setting. Using it
 * can result in out of memory errors and application crashes. Use FILE_URI
 * or NATIVE_URI instead.
 */
navigator.camera.getPicture(onSuccess, onFail, { quality: 25,
    destinationType: Camera.DestinationType.DATA_URL
});

function onSuccess(imageData) {
    var image = document.getElementById('myImage');
    image.src = "data:image/jpeg;base64," + imageData;
}

function onFail(message) {
    alert('Failed because: ' + message);
}

Preferences (iOS)

  • CameraUsesGeolocation (boolean, defaults to false). For capturing JPEGs, set to true to get geolocation data in the EXIF header. This will trigger a request for geolocation permissions if set to true.

     <preference name="CameraUsesGeolocation" value="false" />

Amazon Fire OS Quirks

Amazon Fire OS uses intents to launch the camera activity on the device to capture images, and on phones with low memory, the Cordova activity may be killed. In this scenario, the image may not appear when the Cordova activity is restored.

Android Quirks

Android uses intents to launch the camera activity on the device to capture images, and on phones with low memory, the Cordova activity may be killed. In this scenario, the result from the plugin call will be delivered via the resume event. See the Android Lifecycle guide for more information. The pendingResult.result value will contain the value that would be passed to the callbacks (either the URI/URL or an error message). Check the pendingResult.pluginStatus to determine whether or not the call was successful.

Browser Quirks

Can only return photos as Base64-encoded image.

Firefox OS Quirks

Camera plugin is currently implemented using Web Activities.

iOS Quirks

Including a JavaScript alert() in either of the callback functions can cause problems. Wrap the alert within a setTimeout() to allow the iOS image picker or popover to fully close before the alert displays:

setTimeout(function() {
    // do your thing here!
}, 0);

Windows Phone 7 Quirks

Invoking the native camera application while the device is connected via Zune does not work, and triggers an error callback.

Tizen Quirks

Tizen only supports a destinationType of Camera.DestinationType.FILE_URI and a sourceType of Camera.PictureSourceType.PHOTOLIBRARY.

CameraOptions Errata

Amazon Fire OS Quirks

  • Any cameraDirection value results in a back-facing photo.

  • Ignores the allowEdit parameter.

  • Camera.PictureSourceType.PHOTOLIBRARY and Camera.PictureSourceType.SAVEDPHOTOALBUM both display the same photo album.

Android Quirks

  • Any cameraDirection value results in a back-facing photo.

  • allowEdit is unpredictable on Android and it should not be used! The Android implementation of this plugin tries to find and use an application on the user's device to do image cropping. The plugin has no control over what application the user selects to perform the image cropping and it is very possible that the user could choose an incompatible option and cause the plugin to fail. This sometimes works because most devices come with an application that handles cropping in a way that is compatible with this plugin (Google Plus Photos), but it is unwise to rely on that being the case. If image editing is essential to your application, consider seeking a third party library or plugin that provides its own image editing utility for a more robust solution.

  • Camera.PictureSourceType.PHOTOLIBRARY and Camera.PictureSourceType.SAVEDPHOTOALBUM both display the same photo album.

  • Ignores the encodingType parameter if the image is unedited (i.e. quality is 100, correctOrientation is false, and no targetHeight or targetWidth are specified). The CAMERA source will always return the JPEG file given by the native camera and the PHOTOLIBRARY and SAVEDPHOTOALBUM sources will return the selected file in its existing encoding.

BlackBerry 10 Quirks

  • Ignores the quality parameter.

  • Ignores the allowEdit parameter.

  • Camera.MediaType is not supported.

  • Ignores the correctOrientation parameter.

  • Ignores the cameraDirection parameter.

Firefox OS Quirks

  • Ignores the quality parameter.

  • Camera.DestinationType is ignored and equals 1 (image file URI)

  • Ignores the allowEdit parameter.

  • Ignores the PictureSourceType parameter (user chooses it in a dialog window)

  • Ignores the encodingType

  • Ignores the targetWidth and targetHeight

  • Camera.MediaType is not supported.

  • Ignores the correctOrientation parameter.

  • Ignores the cameraDirection parameter.

iOS Quirks

  • When using destinationType.FILE_URI, photos are saved in the application's temporary directory. The contents of the application's temporary directory is deleted when the application ends.

  • When using destinationType.NATIVE_URI and sourceType.CAMERA, photos are saved in the saved photo album regardless on the value of saveToPhotoAlbum parameter.

Tizen Quirks

  • options not supported

  • always returns a FILE URI

Windows Phone 7 and 8 Quirks

  • Ignores the allowEdit parameter.

  • Ignores the correctOrientation parameter.

  • Ignores the cameraDirection parameter.

  • Ignores the saveToPhotoAlbum parameter. IMPORTANT: All images taken with the WP8/8 Cordova camera API are always copied to the phone's camera roll. Depending on the user's settings, this could also mean the image is auto-uploaded to their OneDrive. This could potentially mean the image is available to a wider audience than your app intended. If this is a blocker for your application, you will need to implement the CameraCaptureTask as documented on MSDN. You may also comment or up-vote the related issue in the issue tracker.

  • Ignores the mediaType property of cameraOptions as the Windows Phone SDK does not provide a way to choose videos from PHOTOLIBRARY.

Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

clone cordova-plugin-camera 展开 收起
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/wingsfrog/wfPluginCamera.git
git@gitee.com:wingsfrog/wfPluginCamera.git
wingsfrog
wfPluginCamera
wfPluginCamera
master

搜索帮助