Exemple #1
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;
            }
        }
Exemple #2
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;
            }
        }