Example #1
0
        public void onCameraTaskCompleted(object sender, PhotoResult e)
        {
            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);

                        WriteableBitmap image = PictureDecoder.DecodeJpeg(rotImageStream);

                        imagePathOrContent = this.SaveImageToLocalStorage(image, Path.GetFileName(e.OriginalFileName));
                    }
                    else if (cameraOptions.DestinationType == DATA_URL)
                    {
                        imagePathOrContent = this.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;
            }
        }
Example #2
0
        public void onTaskCompleted(object sender, PhotoResult e)
        {
            var task = sender as ChooserBase <PhotoResult>;

            if (task != null)
            {
                task.Completed -= onTaskCompleted;
            }

            if (e.Error != null)
            {
                DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR));
                return;
            }

            switch (e.TaskResult)
            {
            case TaskResult.OK:
                try
                {
                    string imagePathOrContent = string.Empty;

                    // Save image back to media library
                    // only save to photoalbum if it didn't come from there ...
                    if (cameraOptions.PictureSourceType == CAMERA && cameraOptions.SaveToPhotoAlbum)
                    {
                        MediaLibrary library = new MediaLibrary();
                        Picture      pict    = library.SavePicture(e.OriginalFileName, e.ChosenPhoto); // to save to photo-roll ...
                    }

                    int newAngle = 0;
                    // There's bug in Windows Phone 8.1 causing Seek on a DssPhotoStream not working properly.
                    // https://connect.microsoft.com/VisualStudio/feedback/details/783252
                    // But a mis-oriented file is better than nothing, so try and catch.
                    try {
                        int orient = ImageExifHelper.getImageOrientationFromStream(e.ChosenPhoto);
                        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
                        }
                    } catch {
                        Debug.WriteLine("Error fetching orientation from Exif");
                    }

                    if (newAngle != 0)
                    {
                        using (Stream rotImageStream = ImageExifHelper.RotateStream(e.ChosenPhoto, newAngle))
                        {
                            // 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 or NATIVE_URI (both use the same resultant uri format)
                            {
                                imagePathOrContent = SaveImageToLocalStorage(rotImageStream, Path.GetFileName(e.OriginalFileName));
                            }
                        }
                    }
                    else       // no need to reorient
                    {
                        if (cameraOptions.DestinationType == DATA_URL)
                        {
                            imagePathOrContent = GetImageContent(e.ChosenPhoto);
                        }
                        else      // FILE_URL or NATIVE_URI (both use the same resultant uri format)
                        {
                            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;
            }
        }
Example #3
0
        /// <summary>
        /// Handles result of capture to save image information
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e">stores information about current captured image</param>
        private void cameraTask_Completed(object sender, PhotoResult e)
        {
            if (e.Error != null)
            {
                DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR));
                return;
            }

            switch (e.TaskResult)
            {
            case TaskResult.OK:
                try
                {
                    string fileName = System.IO.Path.GetFileName(e.OriginalFileName);

                    // Save image in media library
                    MediaLibrary library = new MediaLibrary();
                    Picture      image   = library.SavePicture(fileName, e.ChosenPhoto);

                    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);

                    // Save image in isolated storage

                    // we should return stream position back after saving stream to media library
                    rotImageStream.Seek(0, SeekOrigin.Begin);

                    byte[] imageBytes = new byte[rotImageStream.Length];
                    rotImageStream.Read(imageBytes, 0, imageBytes.Length);
                    rotImageStream.Dispose();
                    string pathLocalStorage = this.SaveImageToLocalStorage(fileName, isoFolder, imageBytes);
                    imageBytes = null;
                    // Get image data
                    MediaFile data = new MediaFile(pathLocalStorage, image);

                    this.files.Add(data);

                    if (files.Count < this.captureImageOptions.Limit)
                    {
                        cameraTask.Show();
                    }
                    else
                    {
                        DispatchCommandResult(new PluginResult(PluginResult.Status.OK, files));
                        files.Clear();
                    }
                }
                catch (Exception)
                {
                    DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, "Error capturing image."));
                }
                break;

            case TaskResult.Cancel:
                if (files.Count > 0)
                {
                    // User canceled operation, but some images were made
                    DispatchCommandResult(new PluginResult(PluginResult.Status.OK, files));
                    files.Clear();
                }
                else
                {
                    DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, "Canceled."));
                }
                break;

            default:
                if (files.Count > 0)
                {
                    DispatchCommandResult(new PluginResult(PluginResult.Status.OK, files));
                    files.Clear();
                }
                else
                {
                    DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, "Did not complete!"));
                }
                break;
            }
        }
Example #4
0
        public void onTaskCompleted(object sender, PhotoResult e)
        {
            var task = sender as ChooserBase <PhotoResult>;

            if (task != null)
            {
                task.Completed -= onTaskCompleted;
            }

            if (e.Error != null)
            {
                DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR));
                return;
            }

            switch (e.TaskResult)
            {
            case TaskResult.OK:
                try
                {
                    string imagePathOrContent = string.Empty;

                    // Save image back to media library
                    // only save to photoalbum if it didn't come from there ...
                    if (cameraOptions.PictureSourceType == CAMERA && 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
                    }

                    if (newAngle != 0)
                    {
                        using (Stream rotImageStream = ImageExifHelper.RotateStream(e.ChosenPhoto, newAngle))
                        {
                            // 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));
                            }
                        }
                    }
                    else       // no need to reorient
                    {
                        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;
            }
        }