示例#1
0
        /// <summary>
        /// Invoke the method specified by the
        /// <see cref="CloudPact.MowblyFramework.Core.Features.JSMessage">JSMessage</see> object
        /// </summary>
        /// <param name="message">
        /// <see cref="CloudPact.MowblyFramework.Core.Features.JSMessage">JSMessage</see> object
        /// </param>
        internal async override void InvokeAsync(JSMessage message)
        {
            switch (message.Method)
            {
            case "getPicture":
                int           source  = Convert.ToInt32(message.Args[0]);
                CameraOptions options =
                    JsonConvert.DeserializeObject <CameraOptions>(message.Args[1].ToString());
                string callbackId = message.CallbackId;
                try
                {
                    if (source == (int)Camera.Source.CAMERA)
                    {
                        FilePath filePath         = options.FilePath;
                        bool     isWriteToGallery = (filePath == null);
                    }

                    // Create the CameraTask
                    CameraTask cameraTask;
                    if (source == (int)Camera.Source.CAMERA)
                    {
                        Logger.Debug("Launching camera...");
                        cameraTask = new MowblyCameraCaptureTask(callbackId, options);
                    }
                    else
                    {
                        Logger.Debug("Launching photo chooser...");
                        cameraTask = new MowblyPhotoChooserTask(callbackId, options);
                    }

                    // Subscribe to CameraTask Completed event
                    cameraTask.Completed += OnCameraTaskCompleted;

                    // Show the CameraTask
                    UiDispatcher.BeginInvoke(() =>
                    {
                        cameraTask.Show();
                    });

                    // Make a note that app is navigated to external task
                    Mowbly.AppNavigatedToExternalPage = true;
                }
                catch (Exception ce)
                {
                    Logger.Error("Exception has occured. Reason - " + ce.Message);
                }

                break;

            case "getConfiguration":
                List <CameraConfig> cameraConfigList = await GetCameraConfigurations();

                MethodResult result = cameraConfigList.Count > 0 ?
                                      new MethodResult
                {
                    Result = cameraConfigList
                } :
                new MethodResult
                {
                    Code  = MethodResult.FAILURE_CODE,
                    Error = new MethodError
                    {
                        Message = Mowbly.GetString(Constants.STRING_CAMERA_INITIALIZATION_ERROR)
                    }
                };
                InvokeCallbackJavascript(message.CallbackId, result);
                break;

            default:
                Logger.Error("Feature " + Name + " does not support method " + message.Method);
                break;
            }
        }
示例#2
0
        // Event handler called when the camera capture task is completed
        private void OnCameraTaskCompleted(object sender, PhotoResult e)
        {
            Dictionary <string, string> result = new Dictionary <string, string>();
            bool       status     = false;
            string     error      = String.Empty;
            CameraTask cameraTask = sender as CameraTask;
            string     callbackId = cameraTask.CallbackId;

            if (e.TaskResult == TaskResult.OK)
            {
                if (e.Error == null)
                {
                    CameraOptions options  = cameraTask.Options;
                    string        filePath = (options.FilePath != null) ?
                                             FileManager.GetAbsolutePath(options.FilePath) :
                                             FileManager.GetAbsolutePath(new FilePath
                    {
                        Path        = Path.GetFileName(e.OriginalFileName),
                        Level       = FileLevel.App,
                        StorageType = StorageType.Cache
                    });
                    result.Add(KEY_PATH, filePath);


                    if (cameraTask.Type == CameraTaskType.PhotoChooser || options.ReadData)
                    {
                        // Load the image as bitmap to know dimensions
                        BitmapImage bi = new BitmapImage();
                        bi.SetSource(e.ChosenPhoto);
                        int imgWidth  = bi.PixelWidth;
                        int imgHeight = bi.PixelHeight;

                        // Get the target dimensions with user requested width/height
                        int width, height;
                        if (options.Width == -1)
                        {
                            if (options.Height == -1)
                            {
                                // Auto width and height. Do nothing.
                                height = imgHeight;
                                width  = imgWidth;
                            }
                            else
                            {
                                // Auto width, scale by height
                                float scale = imgHeight / options.Height;
                                width  = (int)(imgWidth * scale);
                                height = options.Height;
                            }
                        }
                        else
                        {
                            if (options.Height == -1)
                            {
                                // Auto height, scale by width
                                float scale = imgWidth / options.Width;
                                height = (int)(imgHeight * scale);
                                width  = options.Width;
                            }
                            else
                            {
                                // User provided required dimensions. Scale to them.
                                height = options.Height;
                                width  = options.Width;
                            }
                        }

                        // Format the image as specified in options
                        // Though width and height can be same as the captured image,
                        // formatting is required as quality might be different
                        try
                        {
                            e.ChosenPhoto.Seek(0, SeekOrigin.Begin);
                            byte[] data = FormatImage(e.ChosenPhoto, width, height, options.Quality);
                            if (data != null)
                            {
                                try
                                {
                                    // Write to File
                                    FileManager.WriteDataToFile(filePath, data, false);

                                    // Set data in result
                                    if (options.ReadData)
                                    {
                                        result.Add(KEY_DATA, Convert.ToBase64String(data));
                                    }
                                    status = true;
                                }
                                catch (Exception ex)
                                {
                                    // Error writing picture
                                    error = String.Concat(Mowbly.GetString(Constants.STRING_CAMERA_WRITE_PICTURE_ERROR), ex.Message);
                                }
                            }
                            {
                                // Error in formatting picture
                                error = Mowbly.GetString(Constants.STRING_CAMERA_FORMAT_ERROR);
                            }
                        }
                        catch (Exception ex)
                        {
                            // Error formatting picture
                            error = String.Concat(Mowbly.GetString(Constants.STRING_CAMERA_PROCESS_PICTURE_ERROR), ex.Message);
                        }
                    }
                    else
                    {
                        try
                        {
                            // Choose pic with no read
                            FileManager.WriteDataToFile(filePath, e.ChosenPhoto, false);

                            status = true;
                        }
                        catch (Exception ex)
                        {
                            // Error writing picture
                            error = String.Concat(Mowbly.GetString(Constants.STRING_CAMERA_WRITE_PICTURE_ERROR), ex.Message);
                        }
                    }
                }
                else
                {
                    // Error in capturing picture
                    error = String.Concat(Mowbly.GetString(Constants.STRING_CAMERA_CAPTURE_ERROR), e.Error.Message);
                }
            }
            else
            {
                // User cancelled the task
                error = Mowbly.GetString(Constants.STRING_ACTIVITY_CANCELLED);
            }

            if (status)
            {
                InvokeCallbackJavascript(callbackId, new MethodResult
                {
                    Result = result
                });
            }
            else
            {
                Logger.Error(error);
                InvokeCallbackJavascript(callbackId, new MethodResult
                {
                    Code  = MethodResult.FAILURE_CODE,
                    Error = new MethodError
                    {
                        Message = error
                    }
                });
            }
            Logger.Debug(cameraTask.Name + " completed");
        }