cordova-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Mikejo5000 <...@git.apache.org>
Subject [GitHub] cordova-plugin-camera pull request: Added Sample section to the Ca...
Date Wed, 13 Apr 2016 18:30:21 GMT
Github user Mikejo5000 commented on a diff in the pull request:

    https://github.com/apache/cordova-plugin-camera/pull/203#discussion_r59600615
  
    --- Diff: jsdoc2md/TEMPLATE.md ---
    @@ -203,3 +203,194 @@ Tizen only supports a `destinationType` of
     [web_activities]: https://hacks.mozilla.org/2013/01/introducing-web-activities/
     [wp8_bug]: https://issues.apache.org/jira/browse/CB-2083
     [msdn_wp8_docs]: http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh394006.aspx
    +
    +## Sample: Take Pictures, Select Pictures from the Picture Library, and Get Thumbnails
<a name="sample"></a>
    +
    +The Camera plugin allows you to do things like open the device's Camera app and take
a picture, or open the file picker and select one. The code snippets in this section demonstrate
different tasks including:
    +
    +* Open the Camera app and [take a Picture](#takePicture)
    +* Take a picture and [return thumbnails](#getThumbnails) (resized picture)
    +* Take a picture and [generate a FileEntry object](#convert)
    +* [Select a file](#selectFile) from the picture library
    +* Select a JPEG image and [return thumbnails](#getFileThumbnails) (resized image)
    +* Select an image and [generate a FileEntry object](#convert)
    +
    +## Take a Picture <a name="takePicture"></a>
    +
    +Before you can take a picture, you need to set some Camera plugin options to pass into
the Camera plugin's `getPicture` function. Here is a common set of recommendations. In this
example, you create the object that you will use for the Camera options, and set the `sourceType`
dynamically to support both the Camera app and the file picker.
    +
    +```js
    +function setOptions(srcType) {
    +    var options = {
    +        // Some common settings are 20, 50, and 100
    +        quality: 50,
    +        destinationType: Camera.DestinationType.FILE_URI,
    +        // In this app, dynamically set the picture source, Camera or photo gallery
    +        sourceType: srcType,
    +        encodingType: Camera.EncodingType.JPEG,
    +        mediaType: Camera.MediaType.PICTURE,
    +        allowEdit: true,
    +        correctOrientation: true  //Corrects Android orientation quirks
    +    }
    +    return options;
    +}
    +```
    +
    +Typically, you want to use a FILE_URI instead of a DATA_URL to avoid most memory issues.
JPEG is the recommended encoding type for Android.
    +
    +You take a picture by passing in the options object to `getPicture`, which takes a CameraOptions
object as the third argument. When you call `setOptions`, pass `Camera.PictureSourceType.CAMERA`
as the picture source.
    +
    +```js
    +function openCamera(selection) {
    +
    +    var srcType = Camera.PictureSourceType.CAMERA;
    +    var options = setOptions(srcType);
    +    var func = copyToFile;
    +
    +    navigator.camera.getPicture(function cameraSuccess(imageUri) {
    +
    +        displayImage(imageUri);
    +        // You may choose to copy the picture, save it somewhere, or upload.
    +        func(imageUri);
    +
    +    }, function cameraError(error) {
    +        console.debug("Unable to obtain picture: " + error, "app");
    +
    +    }, options);
    +}
    +```
    +
    +Once you take the picture, you can display it or do something else. In this example,
call the app's `displayImage` function from the preceding code.
    +
    +```js
    +function displayImage(imgUri) {
    +
    +    var elem = document.getElementById('imageFile');
    +    elem.src = imgUri;
    +}
    +```
    +
    +## Take a Picture and Return Thumbnails (Resize the Picture) <a name="getThumbnails"></a>
    +
    +To get smaller images, you can return a resized image by passing both `targetHeight`
and `targetWidth` values with your CameraOptions object. In this example, you resize the returned
image to fit in a 100px by 100px box (the aspect ratio is maintained, so 100px is either the
height or width, whichever is greater in the source).
    +
    +```js
    +function openCamera(selection) {
    +
    +    var srcType = Camera.PictureSourceType.CAMERA;
    +    var options = setOptions(srcType);
    +    var func = copyToFile;
    +
    +    if (selection == "camera-thmb") {
    +        options.targetHeight = 100;
    +        options.targetWidth = 100;
    +    }
    +
    +    navigator.camera.getPicture(function cameraSuccess(imageUri) {
    +
    +        // Do something
    +
    +    }, function cameraError(error) {
    +        console.debug("Unable to obtain picture: " + error, "app");
    +
    +    }, options);
    +}
    +```
    +
    +## Select a File from the Picture Library <a name="selectFile"></a>
    +
    +When selecting a file using the file picker, you also need to set the CameraOptions object.
In this example, set the `sourceType` to `Camera.PictureSourceType.SAVEDPHOTOALBUM`. To open
the file picker, call `getPicture` just as you did in the previous example, passing in the
success and error callbacks along with CameraOptions object.
    +
    +```js
    +function openFilePicker(selection) {
    +
    +    var srcType = Camera.PictureSourceType.SAVEDPHOTOALBUM;
    +    var options = setOptions(srcType);
    +    var func = copyToFile;
    +
    +    navigator.camera.getPicture(function cameraSuccess(imageUri) {
    +
    +        // Do something
    +
    +    }, function cameraError(error) {
    +        console.debug("Unable to obtain picture: " + error, "app");
    +
    +    }, options);
    +}
    +```
    +
    +## Select an Image and Return Thumbnails (resized images) <a name="getFileThumbnails"></a>
    +
    +Resizing a file selected with the file picker works just like resizing using the Camera
app. If you are using the file picker instead of the Camera, and you are resizing the image,
the `Camera.EncodingType` value must match the value for the selected image. We previously
set this value to JPEG in the app's `setOptions` function.
    --- End diff --
    
    I spent some more time testing this; it is an issue that shows up on Windows 10 (x64/x86)
but not on Android/iOS it appears. I think I uncovered a likely bug here; if you set config.xml
to Win10 (and target Win10), the image URI returned by getPicture can't be used as an Image
source (won't display when assigned to img.src); this specific issue described in the text
only appears when config.xml is set to 8.1 AND you are targeting Win10. So the bigger issue
appears to be a returned file URI on Win10 that is not easily used as an image source...


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@cordova.apache.org
For additional commands, e-mail: dev-help@cordova.apache.org


Mime
View raw message