[ https://issues.apache.org/jira/browse/CB-2083?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15236185#comment-15236185 ]
ASF GitHub Bot commented on CB-2083: ------------------------------------ Github user riknoll commented on a diff in the pull request: https://github.com/apache/cordova-plugin-camera/pull/203#discussion_r59299069 --- Diff: README.md --- @@ -526,3 +526,207 @@ 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 + +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 +* Take a picture and return thumbnails (resized picture) +* Take a picture and generate a FileEntry object +* Select a file from the picture library +* Select a JPEG image and return thumbnails (resized image) +* Select an image and generate a FileEntry object + +## Take a Picture + +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 = { + quality: 20, + 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) + +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 images to 100px (the aspect ratio is maintained, so 100px is either the height or width, whichever is greater). + +```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 + +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) + +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. + +```js +function openFilePicker(selection) { + + var srcType = Camera.PictureSourceType.SAVEDPHOTOALBUM; + var options = setOptions(srcType); + var func = copyToFile; + + if (selection == "picker-thmb") { + // To downscale a selected image, + // Camera.EncodingType (e.g., JPEG) must match the selected image type. + options.targetHeight = 100; + options.targetWidth = 100; + } + + navigator.camera.getPicture(function cameraSuccess(imageUri) { + + // Do something with image + + }, function cameraError(error) { + console.debug("Unable to obtain picture: " + error, "app"); + + }, options); +} +``` + +## Take a picture and get a FileEntry Object + +If you want to do something like copy the image to another location, or upload it somewhere using the FileTransfer plugin, you need to get a FileEntry object for the returned picture. To do that, call `window.resolveLocalFileSystemURL` on the file URI returned by the Camera app. If you need to use a FileEntry object, set the `destinationType` to `Camera.DestinationType.FILE_URI` in your CameraOptions object (this is also the default value). + +```js +function setOptions(srcType) { + var options = { + quality: 20, + 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; +} +``` + +Here is the call to `window.resolveLocalFileSystemURL`. The image URI is passed to this function from the success callback of `getPicture`. The success handler of `resolveLocalFileSystemURL` receives the FileEntry object. + +```js +function getFileEntry(imgUri) { + window.resolveLocalFileSystemURL(imgUri, function success(fileEntry) { + + // Do something with the FileEntry object, like write to it, upload it, etc. + // writeFile(fileEntry, imgUri); + console.log("got file: " + fileEntry.fullPath); + displayFileData(fileEntry.nativeURL, "Native URL"); + + }, function () { + // If you can't easily get the FileEntry (for example, no write access + // to Pictures library) copy to a new FileEntry. + copyToFile(imgUri); + }); +} +``` + +In the example shown in the preceding code, you call the app's `copyToFile` function if you don't get a valid FileEntry object. The image URI returned from the Camera app should result in a valid FileEntry, but platform behavior may be different for files returned from the file picker. + --- End diff -- Why wouldn't you be able to access the pictures library? I guess in Android the user could deny the permission request. In that case, it would not be able to read the file either, though. Is this something that you experienced in testing? Might be related to the file plugin bug that was blocking writes to external storage on Android (that bug should be fixed in the next plugin release) > Cordova for WP8 getPicture Leaves Photos in Camera Roll > ------------------------------------------------------- > > Key: CB-2083 > URL: https://issues.apache.org/jira/browse/CB-2083 > Project: Apache Cordova > Issue Type: Bug > Components: WP8 (deprecated) > Affects Versions: 2.2.0 > Environment: Windows Phone 8 > Reporter: Alan Neveu > Assignee: Jesse MacFadyen > Priority: Minor > Labels: Annoyance, Enhancement > Original Estimate: 48h > Remaining Estimate: 48h > > getPicture with FileURI does not work, it crashes. > getPicture with DataURL works, but it leaves a copy of the photo on the > phone's Camera Roll. The user then gets very frustrated because they have > this mess of photos from your app cluttering up their personal photos, and > then have to delete them. If they are using SkyDrive on Windows Phone 8, > they will also have a copy of every photo automatically saved to their > SkyDrive account, which then they have to delete from there as well, cursing > your app with every delete operation. Oh, I think that in Windows Phone 7 > this is not the case, but it is the case with Windows Phone 8. > It turns out this is due to the CameraCaptureTask function, which makes it > exceedingly simple to get a photo but has this undesirable side effect. You > cannot just delete the file, though, because of a security exception. > The solution is to re-write Camera.cs so that it uses the full camera API > rather than just the CameraCaptureTask function. That's a couple days of > labor, for sure, but someone needs to do this or else the Windows Phone 8 > platform will suffer from this annoying bug for all that rely on Cordova. > When asked why their app has this annoyance the reply will be "Ah, it's one > of those stupid phonegap apps..." I hate that answer. So I hope that > someone who has the time will contribute to this issue. I will give it a try > in a few weeks when I finish up the projects that I have been putting on hold > while upgrading my app to Windows Phone 8 capable, but I just can't get to it > for a few weeks. I am hoping that this summary will help someone else get a > jump start on this issue as they won't have to do so much preliminary > research before starting. -- This message was sent by Atlassian JIRA (v6.3.4#6332) --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@cordova.apache.org For additional commands, e-mail: issues-h...@cordova.apache.org