You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by pu...@apache.org on 2014/04/08 20:52:20 UTC

[3/3] git commit: cleanup, finalize implementations/consolidations

cleanup, finalize implementations/consolidations


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/ae2acd9a
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-camera/tree/ae2acd9a
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-camera/diff/ae2acd9a

Branch: refs/heads/dev
Commit: ae2acd9ab237df976bd90c3da2fcf0498b027935
Parents: f6e8548
Author: Jesse MacFadyen <pu...@gmail.com>
Authored: Mon Apr 7 18:13:57 2014 -0700
Committer: Jesse MacFadyen <pu...@gmail.com>
Committed: Mon Apr 7 18:13:57 2014 -0700

----------------------------------------------------------------------
 src/wp/Camera.cs | 252 ++++++++++++--------------------------------------
 1 file changed, 58 insertions(+), 194 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-plugin-camera/blob/ae2acd9a/src/wp/Camera.cs
----------------------------------------------------------------------
diff --git a/src/wp/Camera.cs b/src/wp/Camera.cs
index c9f6f78..977634b 100644
--- a/src/wp/Camera.cs
+++ b/src/wp/Camera.cs
@@ -204,27 +204,32 @@ namespace WPCordovaClassLib.Cordova.Commands
                 return;
             }
 
-            //TODO Check if all the options are acceptable
-
+            if(cameraOptions.DestinationType != Camera.FILE_URI && cameraOptions.DestinationType != Camera.DATA_URL )
+            {
+                DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, "Incorrect option: destinationType"));
+                return;
+            }
 
+            ChooserBase<PhotoResult> chooserTask = null;
             if (cameraOptions.PictureSourceType == CAMERA)
             {
-                CameraCaptureTask cameraTask = new CameraCaptureTask();
-                cameraTask.Completed += onCameraTaskCompleted;
-                cameraTask.Show();
+                chooserTask = new CameraCaptureTask();
             }
             else if ((cameraOptions.PictureSourceType == PHOTOLIBRARY) || (cameraOptions.PictureSourceType == SAVEDPHOTOALBUM))
             {
-                PhotoChooserTask photoChooserTask = new PhotoChooserTask();
-                photoChooserTask.Completed += onPickerTaskCompleted;
-                photoChooserTask.Show();
+                chooserTask = new PhotoChooserTask();
+            }
+            // if chooserTask is still null, then PictureSourceType was invalid
+            if (chooserTask != null)
+            {
+                chooserTask.Completed += onTaskCompleted;
+                chooserTask.Show();
             }
             else
             {
+                Debug.WriteLine("Unrecognized PictureSourceType :: " + cameraOptions.PictureSourceType.ToString());
                 DispatchCommandResult(new PluginResult(PluginResult.Status.NO_RESULT));
             }
-
-
         }
 
         public void onTaskCompleted(object sender, PhotoResult e)
@@ -232,7 +237,7 @@ namespace WPCordovaClassLib.Cordova.Commands
             var task = sender as ChooserBase<PhotoResult>;
             if (task != null)
             {
-                task.Completed -= onCameraTaskCompleted;
+                task.Completed -= onTaskCompleted;
             }
 
             if (e.Error != null)
@@ -247,217 +252,76 @@ namespace WPCordovaClassLib.Cordova.Commands
                     try
                     {
                         string imagePathOrContent = string.Empty;
-                        if (cameraOptions.DestinationType == FILE_URI)
-                        {
-                            // Save image in media library
-                            if (cameraOptions.SaveToPhotoAlbum)
-                            {
-                                MediaLibrary library = new MediaLibrary();
-                                Picture pict = library.SavePicture(e.OriginalFileName, e.ChosenPhoto); // to save to photo-roll ...
-                            }
-
-                            /*
-                                                 int orient = ImageExifHelper.getImageOrientationFromStream(e.ChosenPhoto);
-                    int newAngle = 0;
-                    switch (orient)
-                    {
-                        case ImageExifOrientation.LandscapeLeft:
-                            newAngle = 90;
-                            break;
-                        case ImageExifOrientation.PortraitUpsideDown:
-                            newAngle = 180;
-                            break;
-                        case ImageExifOrientation.LandscapeRight:
-                            newAngle = 270;
-                            break;
-                        case ImageExifOrientation.Portrait:
-                        default: break; // 0 default already set
-                    }
-
-                    Stream rotImageStream = ImageExifHelper.RotateStream(e.ChosenPhoto, newAngle);
-
-                    // we should reset stream position after saving stream to media library
-                    rotImageStream.Seek(0, SeekOrigin.Begin);
-                    imagePathOrContent = SaveImageToLocalStorage(rotImageStream, Path.GetFileName(e.OriginalFileName));
 
-                             */
-                              
-                             
-                            imagePathOrContent = SaveImageToLocalStorage(e.ChosenPhoto, Path.GetFileName(e.OriginalFileName));
-                        }
-                        else if (cameraOptions.DestinationType == DATA_URL)
+                        // Save image back to media library
+                        // only save to photoalbum if it didn't come from there ...
+                        if (cameraOptions.PictureSourceType == CAMERA && cameraOptions.SaveToPhotoAlbum)
                         {
-                            imagePathOrContent = GetImageContent(e.ChosenPhoto);
+                            MediaLibrary library = new MediaLibrary();
+                            Picture pict = library.SavePicture(e.OriginalFileName, e.ChosenPhoto); // to save to photo-roll ...
                         }
-                        else
+
+                        int orient = ImageExifHelper.getImageOrientationFromStream(e.ChosenPhoto);
+                        int newAngle = 270;
+                        switch (orient)
                         {
-                            // TODO: shouldn't this happen before we launch the camera-picker?
-                            DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, "Incorrect option: destinationType"));
-                            return;
+                            case ImageExifOrientation.LandscapeLeft:
+                                newAngle = 90;
+                                break;
+                            case ImageExifOrientation.PortraitUpsideDown:
+                                newAngle = 180;
+                                break;
+                            case ImageExifOrientation.LandscapeRight:
+                                newAngle = 270;
+                                break;
+                            case ImageExifOrientation.Portrait:
+                            default: break; // 0 default already set
                         }
-                        DispatchCommandResult(new PluginResult(PluginResult.Status.OK, imagePathOrContent));
-
-                    }
-                    catch (Exception)
-                    {
-                        DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, "Error retrieving image."));
-                    }
-                    break;
-
-                case TaskResult.Cancel:
-                    DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, "Selection cancelled."));
-                    break;
 
-                default:
-                    DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, "Selection did not complete!"));
-                    break;
-            }
-
-
-        }
-
-        public void onCameraTaskCompleted(object sender, PhotoResult e)
-        {
-            var task = sender as ChooserBase<PhotoResult>;
-            if (task != null)
-            {
-                task.Completed -= onCameraTaskCompleted;
-            }
-
-            if (e.Error != null)
-            {
-                DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR));
-                return;
-            }
-
-            switch (e.TaskResult)
-            {
-                case TaskResult.OK:
-                    try
-                    {
-                        string imagePathOrContent = string.Empty;
-
-                        if (cameraOptions.DestinationType == FILE_URI)
+                        if (newAngle != 0)
                         {
-                            // Save image in media library
-                            if (cameraOptions.SaveToPhotoAlbum)
+                            using (Stream rotImageStream = ImageExifHelper.RotateStream(e.ChosenPhoto, newAngle))
                             {
-                                MediaLibrary library = new MediaLibrary();
-                                Picture pict = library.SavePicture(e.OriginalFileName, e.ChosenPhoto); // to save to photo-roll ...
+                                // we should reset stream position after saving stream to media library
+                                rotImageStream.Seek(0, SeekOrigin.Begin);
+                                if (cameraOptions.DestinationType == DATA_URL)
+                                {
+                                    imagePathOrContent = GetImageContent(rotImageStream);
+                                }
+                                else   // FILE_URL
+                                {
+                                    imagePathOrContent = SaveImageToLocalStorage(rotImageStream, Path.GetFileName(e.OriginalFileName));
+                                }
                             }
-
-                            int orient = ImageExifHelper.getImageOrientationFromStream(e.ChosenPhoto);
-                            int newAngle = 0;
-                            switch (orient)
-                            {
-                                case ImageExifOrientation.LandscapeLeft:
-                                    newAngle = 90;
-                                    break;
-                                case ImageExifOrientation.PortraitUpsideDown:
-                                    newAngle = 180;
-                                    break;
-                                case ImageExifOrientation.LandscapeRight:
-                                    newAngle = 270;
-                                    break;
-                                case ImageExifOrientation.Portrait:
-                                default: break; // 0 default already set
-                            }
-
-                            Stream rotImageStream = ImageExifHelper.RotateStream(e.ChosenPhoto, newAngle);
-
-                            // we should return stream position back after saving stream to media library
-                            rotImageStream.Seek(0, SeekOrigin.Begin);
-                            imagePathOrContent = SaveImageToLocalStorage(rotImageStream, Path.GetFileName(e.OriginalFileName));
-
-
-                        }
-                        else if (cameraOptions.DestinationType == DATA_URL)
-                        {
-                            imagePathOrContent = GetImageContent(e.ChosenPhoto);
-                        }
-                        else
-                        {
-                            // TODO: shouldn't this happen before we launch the camera-picker?
-                            DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, "Incorrect option: destinationType"));
-                            return;
-                        }
-
-                        DispatchCommandResult(new PluginResult(PluginResult.Status.OK, imagePathOrContent));
-
-                    }
-                    catch (Exception)
-                    {
-                        DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, "Error retrieving image."));
-                    }
-                    break;
-
-                case TaskResult.Cancel:
-                    DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, "Selection cancelled."));
-                    break;
-
-                default:
-                    DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, "Selection did not complete!"));
-                    break;
-            }
-
-        }
-
-        public void onPickerTaskCompleted(object sender, PhotoResult e)
-        {
-            var task = sender as ChooserBase<PhotoResult>;
-            if (task != null)
-            {
-                task.Completed -= onCameraTaskCompleted;
-            }
-
-            if (e.Error != null)
-            {
-                DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR));
-                return;
-            }
-
-            switch (e.TaskResult)
-            {
-                case TaskResult.OK:
-                    try
-                    {
-                        string imagePathOrContent = string.Empty;
-
-                        if (cameraOptions.DestinationType == FILE_URI)
-                        {
-                            imagePathOrContent = SaveImageToLocalStorage(e.ChosenPhoto, Path.GetFileName(e.OriginalFileName));
-                        }
-                        else if (cameraOptions.DestinationType == DATA_URL)
-                        {
-                            imagePathOrContent = GetImageContent(e.ChosenPhoto);
-
                         }
-                        else
+                        else   // no need to reorient
                         {
-                            // TODO: shouldn't this happen before we launch the camera-picker?
-                            DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, "Incorrect option: destinationType"));
-                            return;
+                            if (cameraOptions.DestinationType == DATA_URL)
+                            {
+                                imagePathOrContent = GetImageContent(e.ChosenPhoto);
+                            }
+                            else  // FILE_URL
+                            {
+                                imagePathOrContent = SaveImageToLocalStorage(e.ChosenPhoto, Path.GetFileName(e.OriginalFileName));
+                            }
                         }
 
                         DispatchCommandResult(new PluginResult(PluginResult.Status.OK, imagePathOrContent));
-
                     }
                     catch (Exception)
                     {
                         DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, "Error retrieving image."));
                     }
                     break;
-
                 case TaskResult.Cancel:
                     DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, "Selection cancelled."));
                     break;
-
                 default:
                     DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, "Selection did not complete!"));
                     break;
             }
         }
-
+     
         /// <summary>
         /// Returns image content in a form of base64 string
         /// </summary>