cleanup, finalize implementations/consolidations

This commit is contained in:
Jesse MacFadyen 2014-04-07 18:13:57 -07:00
parent f6e8548381
commit ae2acd9ab2

View File

@ -204,27 +204,32 @@ namespace WPCordovaClassLib.Cordova.Commands
return; 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) if (cameraOptions.PictureSourceType == CAMERA)
{ {
CameraCaptureTask cameraTask = new CameraCaptureTask(); chooserTask = new CameraCaptureTask();
cameraTask.Completed += onCameraTaskCompleted;
cameraTask.Show();
} }
else if ((cameraOptions.PictureSourceType == PHOTOLIBRARY) || (cameraOptions.PictureSourceType == SAVEDPHOTOALBUM)) else if ((cameraOptions.PictureSourceType == PHOTOLIBRARY) || (cameraOptions.PictureSourceType == SAVEDPHOTOALBUM))
{ {
PhotoChooserTask photoChooserTask = new PhotoChooserTask(); chooserTask = new PhotoChooserTask();
photoChooserTask.Completed += onPickerTaskCompleted; }
photoChooserTask.Show(); // if chooserTask is still null, then PictureSourceType was invalid
if (chooserTask != null)
{
chooserTask.Completed += onTaskCompleted;
chooserTask.Show();
} }
else else
{ {
Debug.WriteLine("Unrecognized PictureSourceType :: " + cameraOptions.PictureSourceType.ToString());
DispatchCommandResult(new PluginResult(PluginResult.Status.NO_RESULT)); DispatchCommandResult(new PluginResult(PluginResult.Status.NO_RESULT));
} }
} }
public void onTaskCompleted(object sender, PhotoResult e) public void onTaskCompleted(object sender, PhotoResult e)
@ -232,7 +237,7 @@ namespace WPCordovaClassLib.Cordova.Commands
var task = sender as ChooserBase<PhotoResult>; var task = sender as ChooserBase<PhotoResult>;
if (task != null) if (task != null)
{ {
task.Completed -= onCameraTaskCompleted; task.Completed -= onTaskCompleted;
} }
if (e.Error != null) if (e.Error != null)
@ -247,18 +252,17 @@ namespace WPCordovaClassLib.Cordova.Commands
try try
{ {
string imagePathOrContent = string.Empty; string imagePathOrContent = string.Empty;
if (cameraOptions.DestinationType == FILE_URI)
{ // Save image back to media library
// Save image in media library // only save to photoalbum if it didn't come from there ...
if (cameraOptions.SaveToPhotoAlbum) if (cameraOptions.PictureSourceType == CAMERA && cameraOptions.SaveToPhotoAlbum)
{ {
MediaLibrary library = new MediaLibrary(); MediaLibrary library = new MediaLibrary();
Picture pict = library.SavePicture(e.OriginalFileName, e.ChosenPhoto); // to save to photo-roll ... Picture pict = library.SavePicture(e.OriginalFileName, e.ChosenPhoto); // to save to photo-roll ...
} }
/*
int orient = ImageExifHelper.getImageOrientationFromStream(e.ChosenPhoto); int orient = ImageExifHelper.getImageOrientationFromStream(e.ChosenPhoto);
int newAngle = 0; int newAngle = 270;
switch (orient) switch (orient)
{ {
case ImageExifOrientation.LandscapeLeft: case ImageExifOrientation.LandscapeLeft:
@ -274,184 +278,44 @@ namespace WPCordovaClassLib.Cordova.Commands
default: break; // 0 default already set default: break; // 0 default already set
} }
Stream rotImageStream = ImageExifHelper.RotateStream(e.ChosenPhoto, newAngle); if (newAngle != 0)
{
using (Stream rotImageStream = ImageExifHelper.RotateStream(e.ChosenPhoto, newAngle))
{
// we should reset stream position after saving stream to media library // we should reset stream position after saving stream to media library
rotImageStream.Seek(0, SeekOrigin.Begin); rotImageStream.Seek(0, SeekOrigin.Begin);
imagePathOrContent = SaveImageToLocalStorage(rotImageStream, Path.GetFileName(e.OriginalFileName)); if (cameraOptions.DestinationType == DATA_URL)
{
*/ imagePathOrContent = GetImageContent(rotImageStream);
imagePathOrContent = SaveImageToLocalStorage(e.ChosenPhoto, Path.GetFileName(e.OriginalFileName));
} }
else if (cameraOptions.DestinationType == DATA_URL) else // FILE_URL
{
imagePathOrContent = SaveImageToLocalStorage(rotImageStream, Path.GetFileName(e.OriginalFileName));
}
}
}
else // no need to reorient
{
if (cameraOptions.DestinationType == DATA_URL)
{ {
imagePathOrContent = GetImageContent(e.ChosenPhoto); imagePathOrContent = GetImageContent(e.ChosenPhoto);
} }
else else // FILE_URL
{
// 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)
{
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)
{
// 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 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)); 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)); DispatchCommandResult(new PluginResult(PluginResult.Status.OK, imagePathOrContent));
} }
catch (Exception) catch (Exception)
{ {
DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, "Error retrieving image.")); DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, "Error retrieving image."));
} }
break; break;
case TaskResult.Cancel: case TaskResult.Cancel:
DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, "Selection cancelled.")); DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, "Selection cancelled."));
break; break;
default: default:
DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, "Selection did not complete!")); DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, "Selection did not complete!"));
break; break;