cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From an-s...@apache.org
Subject [4/5] cordova-plugin-camera git commit: CR fixes
Date Tue, 07 Apr 2015 18:48:35 GMT
CR fixes


Project: http://git-wip-us.apache.org/repos/asf/cordova-plugin-camera/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-plugin-camera/commit/9e11ab4d
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-camera/tree/9e11ab4d
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-camera/diff/9e11ab4d

Branch: refs/heads/master
Commit: 9e11ab4dfb03222b3da57632c6ae9fb6fee9f826
Parents: 29c9ea3
Author: Murat Sutunc <muratsu@microsoft.com>
Authored: Fri Apr 3 11:08:00 2015 -0700
Committer: Murat Sutunc <muratsu@microsoft.com>
Committed: Fri Apr 3 11:08:00 2015 -0700

----------------------------------------------------------------------
 src/windows/CameraProxy.js | 320 ++++++++++++++++++++--------------------
 1 file changed, 162 insertions(+), 158 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-plugin-camera/blob/9e11ab4d/src/windows/CameraProxy.js
----------------------------------------------------------------------
diff --git a/src/windows/CameraProxy.js b/src/windows/CameraProxy.js
index aa2e391..3d4d24b 100644
--- a/src/windows/CameraProxy.js
+++ b/src/windows/CameraProxy.js
@@ -158,33 +158,33 @@ function takePictureFromFile(successCallback, errorCallback, mediaType,
destinat
     }
 
     fileOpenPicker.pickSingleFileAsync().done(function (file) {
-        if (file) {
-            if (destinationType == Camera.DestinationType.FILE_URI || destinationType ==
Camera.DestinationType.NATIVE_URI) {
-                if (targetHeight > 0 && targetWidth > 0) {
-                    resizeImage(successCallback, errorCallback, file, targetWidth, targetHeight,
encodingType);
-                }
-                else {
-                    var storageFolder = Windows.Storage.ApplicationData.current.localFolder;
-                    file.copyAsync(storageFolder, file.name, Windows.Storage.NameCollisionOption.replaceExisting).done(function
(storageFile) {
-                        successCallback(URL.createObjectURL(storageFile));
-                    }, function () {
-                        errorCallback("Can't access localStorage folder.");
-                    });
-
-                }
+        if (!file) {
+            errorCallback("User didn't choose a file.");
+            return;
+        }
+        if (destinationType == Camera.DestinationType.FILE_URI || destinationType == Camera.DestinationType.NATIVE_URI)
{
+            if (targetHeight > 0 && targetWidth > 0) {
+                resizeImage(successCallback, errorCallback, file, targetWidth, targetHeight,
encodingType);
             }
             else {
-                if (targetHeight > 0 && targetWidth > 0) {
-                    resizeImageBase64(successCallback, errorCallback, file, targetWidth,
targetHeight);
-                } else {
-                    Windows.Storage.FileIO.readBufferAsync(file).done(function (buffer) {
-                        var strBase64 = Windows.Security.Cryptography.CryptographicBuffer.encodeToBase64String(buffer);
-                        successCallback(strBase64);
-                    }, errorCallback);
-                }
+                var storageFolder = Windows.Storage.ApplicationData.current.localFolder;
+                file.copyAsync(storageFolder, file.name, Windows.Storage.NameCollisionOption.replaceExisting).done(function
(storageFile) {
+                    successCallback(URL.createObjectURL(storageFile));
+                }, function () {
+                    errorCallback("Can't access localStorage folder.");
+                });
+
+            }
+        }
+        else {
+            if (targetHeight > 0 && targetWidth > 0) {
+                resizeImageBase64(successCallback, errorCallback, file, targetWidth, targetHeight);
+            } else {
+                Windows.Storage.FileIO.readBufferAsync(file).done(function (buffer) {
+                    var strBase64 = Windows.Security.Cryptography.CryptographicBuffer.encodeToBase64String(buffer);
+                    successCallback(strBase64);
+                }, errorCallback);
             }
-        } else {
-            errorCallback("User didn't choose a file.");
         }
     }, function () {
         errorCallback("User didn't choose a file.");
@@ -240,40 +240,41 @@ function takePictureFromCameraWP(successCallback, errorCallback, args)
{
         var expectedPanel = cameraDirection === 1 ? Windows.Devices.Enumeration.Panel.front
: Windows.Devices.Enumeration.Panel.back;
         Windows.Devices.Enumeration.DeviceInformation.findAllAsync(Windows.Devices.Enumeration.DeviceClass.videoCapture)
         .done(function (devices) {
-            if (devices.length > 0) {
-                devices.forEach(function(currDev) {
-                    if (currDev.enclosureLocation.panel && currDev.enclosureLocation.panel
== expectedPanel) {
-                        captureSettings.videoDeviceId = currDev.id;
-                    }
-                });
-
-                capture.initializeAsync(captureSettings).done(function () {
-                    // This is necessary since WP8.1 MediaCapture outputs video stream rotated
90 degrees CCW
-                    // TODO: This can be not consistent across devices, need additional testing
on various devices
-                    capture.setPreviewRotation(Windows.Media.Capture.VideoRotation.clockwise90Degrees);
-                    // msdn.microsoft.com/en-us/library/windows/apps/hh452807.aspx
-                    capturePreview.msZoom = true;
-                    capturePreview.src = URL.createObjectURL(capture);
-                    capturePreview.play();
-
-                    // Insert preview frame and controls into page
-                    document.body.appendChild(capturePreview);
-                    document.body.appendChild(captureCancelButton);
-
-                    // Bind events to controls
-                    capturePreview.addEventListener('click', captureAction);
-                    captureCancelButton.addEventListener('click', function () {
-                        destroyCameraPreview();
-                        errorCallback('Cancelled');
-                    }, false);
-                }, function (err) {
-                    destroyCameraPreview();
-                    errorCallback('Camera intitialization error ' + err);
-                });
-            } else {
+            if (devices.length <= 0) {
                 destroyCameraPreview();
                 errorCallback('Camera not found');
+                return;
             }
+
+            devices.forEach(function(currDev) {
+                if (currDev.enclosureLocation.panel && currDev.enclosureLocation.panel
== expectedPanel) {
+                    captureSettings.videoDeviceId = currDev.id;
+                }
+            });
+
+            capture.initializeAsync(captureSettings).done(function () {
+                // This is necessary since WP8.1 MediaCapture outputs video stream rotated
90 degrees CCW
+                // TODO: This can be not consistent across devices, need additional testing
on various devices
+                capture.setPreviewRotation(Windows.Media.Capture.VideoRotation.clockwise90Degrees);
+                // msdn.microsoft.com/en-us/library/windows/apps/hh452807.aspx
+                capturePreview.msZoom = true;
+                capturePreview.src = URL.createObjectURL(capture);
+                capturePreview.play();
+
+                // Insert preview frame and controls into page
+                document.body.appendChild(capturePreview);
+                document.body.appendChild(captureCancelButton);
+
+                // Bind events to controls
+                capturePreview.addEventListener('click', captureAction);
+                captureCancelButton.addEventListener('click', function () {
+                    destroyCameraPreview();
+                    errorCallback('Cancelled');
+                }, false);
+            }, function (err) {
+                destroyCameraPreview();
+                errorCallback('Camera intitialization error ' + err);
+            });
         }, errorCallback);
     };
 
@@ -341,51 +342,52 @@ function takePictureFromCameraWP(successCallback, errorCallback, args)
{
                     }
                 };
 
-                if (saveToPhotoAlbum) {
-                    /*
-                        Need to add and remove an event listener to catch activation state
-                        Using FileSavePicker will suspend the app and it's required to catch
the pickSaveFileContinuation
-                        https://msdn.microsoft.com/en-us/library/windows/apps/xaml/dn631755.aspx
-                    */
-                    var cameraActivationHandler = function(eventArgs) {
-                        if (eventArgs.kind === Windows.ApplicationModel.Activation.ActivationKind.pickSaveFileContinuation)
{
-                            var file = eventArgs.file;
-                            if (file) {
-                                // Prevent updates to the remote version of the file until
we're done
-                                Windows.Storage.CachedFileManager.deferUpdates(file);
-                                capturedFile.moveAndReplaceAsync(file)
-                                    .then(function() {
-                                        // Let Windows know that we're finished changing
the file so
-                                        // the other app can update the remote version of
the file.
-                                        return Windows.Storage.CachedFileManager.completeUpdatesAsync(file);
-                                    })
-                                    .done(function(updateStatus) {
-                                            if (updateStatus === Windows.Storage.Provider.FileUpdateStatus.complete)
{
-                                                success(capturedFile);
-                                            } else {
-                                                errorCallback();
-                                            }
-                                    }, errorCallback);
-                            } else {
-                                errorCallback();
-                            }
-                            Windows.UI.WebUI.WebUIApplication.removeEventListener("activated",
cameraActivationHandler);
-                        }
-                    };
+                if (!saveToPhotoAlbum) {
+                    success(capturedFile);
+                    return;
+                }
 
-                    var savePicker = new Windows.Storage.Pickers.FileSavePicker();
-                    savePicker.suggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.documentsLibrary;
-                    if (encodingType === Camera.EncodingType.PNG) {
-                        savePicker.fileTypeChoices.insert("PNG", [".png"]);
-                    } else {
-                        savePicker.fileTypeChoices.insert("JPEG", [".jpg"]);
+                /*
+                    Need to add and remove an event listener to catch activation state
+                    Using FileSavePicker will suspend the app and it's required to catch
the pickSaveFileContinuation
+                    https://msdn.microsoft.com/en-us/library/windows/apps/xaml/dn631755.aspx
+                */
+                var cameraActivationHandler = function(eventArgs) {
+                    if (eventArgs.kind === Windows.ApplicationModel.Activation.ActivationKind.pickSaveFileContinuation)
{
+                        var file = eventArgs.file;
+                        if (file) {
+                            // Prevent updates to the remote version of the file until we're
done
+                            Windows.Storage.CachedFileManager.deferUpdates(file);
+                            capturedFile.moveAndReplaceAsync(file)
+                                .then(function() {
+                                    // Let Windows know that we're finished changing the
file so
+                                    // the other app can update the remote version of the
file.
+                                    return Windows.Storage.CachedFileManager.completeUpdatesAsync(file);
+                                })
+                                .done(function(updateStatus) {
+                                        if (updateStatus === Windows.Storage.Provider.FileUpdateStatus.complete)
{
+                                            success(capturedFile);
+                                        } else {
+                                            errorCallback();
+                                        }
+                                }, errorCallback);
+                        } else {
+                            errorCallback();
+                        }
+                        Windows.UI.WebUI.WebUIApplication.removeEventListener("activated",
cameraActivationHandler);
                     }
-                    savePicker.suggestedFileName = fileName;
-                    Windows.UI.WebUI.WebUIApplication.addEventListener("activated", cameraActivationHandler);
-                    savePicker.pickSaveFileAndContinue();
+                };
+
+                var savePicker = new Windows.Storage.Pickers.FileSavePicker();
+                savePicker.suggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.documentsLibrary;
+                if (encodingType === Camera.EncodingType.PNG) {
+                    savePicker.fileTypeChoices.insert("PNG", [".png"]);
                 } else {
-                    success(capturedFile);
+                    savePicker.fileTypeChoices.insert("JPEG", [".jpg"]);
                 }
+                savePicker.suggestedFileName = fileName;
+                Windows.UI.WebUI.WebUIApplication.addEventListener("activated", cameraActivationHandler);
+                savePicker.pickSaveFileAndContinue();
             }, function(err) {
                     destroyCameraPreview();
                     errorCallback(err);
@@ -432,76 +434,78 @@ function takePictureFromCameraWindows(successCallback, errorCallback,
args) {
     }
 
     cameraCaptureUI.captureFileAsync(Windows.Media.Capture.CameraCaptureUIMode.photo).done(function(picture)
{
-        if (picture) {
-            // save to photo album successCallback
-            var success = function() {
-                if (destinationType == Camera.DestinationType.FILE_URI) {
-                    if (targetHeight > 0 && targetWidth > 0) {
-                        resizeImage(successCallback, errorCallback, picture, targetWidth,
targetHeight, encodingType);
-                    } else {
-                        var storageFolder = Windows.Storage.ApplicationData.current.localFolder;
-                        picture.copyAsync(storageFolder, picture.name, Windows.Storage.NameCollisionOption.replaceExisting).done(function(storageFile)
{
-                            successCallback("ms-appdata:///local/" + storageFile.name);
-                        }, errorCallback);
-                    }
+        if (!picture) {
+            errorCallback("User didn't capture a photo.");
+            return;
+        }
+
+        // save to photo album successCallback
+        var success = function() {
+            if (destinationType == Camera.DestinationType.FILE_URI) {
+                if (targetHeight > 0 && targetWidth > 0) {
+                    resizeImage(successCallback, errorCallback, picture, targetWidth, targetHeight,
encodingType);
                 } else {
-                    if (targetHeight > 0 && targetWidth > 0) {
-                        resizeImageBase64(successCallback, errorCallback, picture, targetWidth,
targetHeight);
-                    } else {
-                        Windows.Storage.FileIO.readBufferAsync(picture).done(function(buffer)
{
-                            var strBase64 = Windows.Security.Cryptography.CryptographicBuffer.encodeToBase64String(buffer);
-                            successCallback(strBase64);
-                        }, errorCallback);
-                    }
+                    var storageFolder = Windows.Storage.ApplicationData.current.localFolder;
+                    picture.copyAsync(storageFolder, picture.name, Windows.Storage.NameCollisionOption.replaceExisting).done(function(storageFile)
{
+                        successCallback("ms-appdata:///local/" + storageFile.name);
+                    }, errorCallback);
                 }
-            };
+            } else {
+                if (targetHeight > 0 && targetWidth > 0) {
+                    resizeImageBase64(successCallback, errorCallback, picture, targetWidth,
targetHeight);
+                } else {
+                    Windows.Storage.FileIO.readBufferAsync(picture).done(function(buffer)
{
+                        var strBase64 = Windows.Security.Cryptography.CryptographicBuffer.encodeToBase64String(buffer);
+                        successCallback(strBase64);
+                    }, errorCallback);
+                }
+            }
+        };
 
-            // save to photo album errorCallback
-            var fail = function() {
-                //errorCallback("FileError, code:" + fileError.code);
-                errorCallback("Save fail.");
-            };
+        // save to photo album errorCallback
+        var fail = function() {
+            //errorCallback("FileError, code:" + fileError.code);
+            errorCallback("Save fail.");
+        };
 
-            if (saveToPhotoAlbum) {
-                var savePicker = new Windows.Storage.Pickers.FileSavePicker();
+        if (!saveToPhotoAlbum) {
+            success();
+            return;
+        }
 
-                savePicker.suggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.documentsLibrary;
-                if (encodingType === Camera.EncodingType.PNG) {
-                    savePicker.fileTypeChoices.insert("PNG", [".png"]);
-                    savePicker.suggestedFileName = "photo.png";
-                } else {
-                    savePicker.fileTypeChoices.insert("JPEG", [".jpg"]);
-                    savePicker.suggestedFileName = "photo.jpg";
-                }
+        var savePicker = new Windows.Storage.Pickers.FileSavePicker();
 
-                savePicker.pickSaveFileAsync()
-                    .done(function(file) {
-                        if (file) {
-                            // Prevent updates to the remote version of the file until we're
done
-                            Windows.Storage.CachedFileManager.deferUpdates(file);
-                            picture.moveAndReplaceAsync(file)
-                                .then(function() {
-                                    // Let Windows know that we're finished changing the
file so
-                                    // the other app can update the remote version of the
file.
-                                    return Windows.Storage.CachedFileManager.completeUpdatesAsync(file);
-                                })
-                                .done(function(updateStatus) {
-                                    if (updateStatus === Windows.Storage.Provider.FileUpdateStatus.complete)
{
-                                        success();
-                                    } else {
-                                        fail();
-                                    }
-                                }, fail);
-                        } else {
-                            fail();
-                        }
-                    }, fail);
-            } else {
-                success();
-            }
+        savePicker.suggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.documentsLibrary;
+        if (encodingType === Camera.EncodingType.PNG) {
+            savePicker.fileTypeChoices.insert("PNG", [".png"]);
+            savePicker.suggestedFileName = "photo.png";
         } else {
-            errorCallback("User didn't capture a photo.");
+            savePicker.fileTypeChoices.insert("JPEG", [".jpg"]);
+            savePicker.suggestedFileName = "photo.jpg";
         }
+
+        savePicker.pickSaveFileAsync()
+            .done(function(file) {
+                if (file) {
+                    // Prevent updates to the remote version of the file until we're done
+                    Windows.Storage.CachedFileManager.deferUpdates(file);
+                    picture.moveAndReplaceAsync(file)
+                        .then(function() {
+                            // Let Windows know that we're finished changing the file so
+                            // the other app can update the remote version of the file.
+                            return Windows.Storage.CachedFileManager.completeUpdatesAsync(file);
+                        })
+                        .done(function(updateStatus) {
+                            if (updateStatus === Windows.Storage.Provider.FileUpdateStatus.complete)
{
+                                success();
+                            } else {
+                                fail();
+                            }
+                        }, fail);
+                } else {
+                    fail();
+                }
+            }, fail);
     }, function() {
         errorCallback("Fail to capture a photo.");
     });


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


Mime
View raw message