From f6e8548381db0e7e4b7d6e095c6718313a001edf Mon Sep 17 00:00:00 2001 From: Jesse MacFadyen Date: Mon, 7 Apr 2014 16:32:21 -0700 Subject: [PATCH] combining callbacks, removing lots of dupe code --- src/wp/Camera.cs | 99 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 94 insertions(+), 5 deletions(-) diff --git a/src/wp/Camera.cs b/src/wp/Camera.cs index 53ae61a..c9f6f78 100644 --- a/src/wp/Camera.cs +++ b/src/wp/Camera.cs @@ -200,7 +200,7 @@ namespace WPCordovaClassLib.Cordova.Commands } catch (Exception ex) { - this.DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION, ex.Message)); + DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION, ex.Message)); return; } @@ -225,6 +225,95 @@ namespace WPCordovaClassLib.Cordova.Commands } + } + + public void onTaskCompleted(object sender, PhotoResult e) + { + var task = sender as ChooserBase; + 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) + { + // 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) + { + 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 onCameraTaskCompleted(object sender, PhotoResult e) @@ -278,13 +367,13 @@ namespace WPCordovaClassLib.Cordova.Commands // we should return stream position back after saving stream to media library rotImageStream.Seek(0, SeekOrigin.Begin); - imagePathOrContent = this.SaveImageToLocalStorage(rotImageStream, Path.GetFileName(e.OriginalFileName)); + imagePathOrContent = SaveImageToLocalStorage(rotImageStream, Path.GetFileName(e.OriginalFileName)); } else if (cameraOptions.DestinationType == DATA_URL) { - imagePathOrContent = this.GetImageContent(e.ChosenPhoto); + imagePathOrContent = GetImageContent(e.ChosenPhoto); } else { @@ -336,11 +425,11 @@ namespace WPCordovaClassLib.Cordova.Commands if (cameraOptions.DestinationType == FILE_URI) { - imagePathOrContent = this.SaveImageToLocalStorage(e.ChosenPhoto, Path.GetFileName(e.OriginalFileName)); + imagePathOrContent = SaveImageToLocalStorage(e.ChosenPhoto, Path.GetFileName(e.OriginalFileName)); } else if (cameraOptions.DestinationType == DATA_URL) { - imagePathOrContent = this.GetImageContent(e.ChosenPhoto); + imagePathOrContent = GetImageContent(e.ChosenPhoto); } else