コード例 #1
0
        protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
        {
            IsCamera = SamplePage.IsCamera;

            base.OnActivityResult(requestCode, resultCode, data);

            if (IsCamera == true)
            {
                Intent mediaScanIntent = new Intent(Intent.ActionMediaScannerScanFile);
                Uri    contentUri      = Uri.FromFile(AppClass._file);
                mediaScanIntent.SetData(contentUri);
                SendBroadcast(mediaScanIntent);

                //int height = Resources.DisplayMetrics.HeightPixels;
                int width = Resources.DisplayMetrics.WidthPixels;
                AppClass.bitmap = AppClass._file.Path.LoadAndResizeBitmap(width, width);

                byte[] bitmapData = new byte[0];

                if (AppClass.bitmap != null)
                {
                    using (var stream = new MemoryStream())
                    {
                        AppClass.bitmap.Compress(Bitmap.CompressFormat.Png, 50, stream);
                        bitmapData = stream.ToArray();
                    }

                    AppClass.bitmap = null;
                }

                GC.Collect();

                SamplePage.Cameraimage(bitmapData);
            }
            else
            {
                if (requestCode == 1)
                {
                    if (resultCode == Result.Ok)
                    {
                        if (data.Data != null)
                        {
                            Android.Net.Uri uri = data.Data;

                            int orientation       = getOrientation(uri);
                            BitmapWorkerTask task = new BitmapWorkerTask(this.ContentResolver, uri);
                            task.Execute(orientation);
                        }
                    }
                }
            }
        }
コード例 #2
0
        private void GotAccessToCamera()
        {
            var imagePicker = new UIImagePickerController {
                SourceType = UIImagePickerControllerSourceType.Camera
            };

            var window = UIApplication.SharedApplication.KeyWindow;
            var vc     = window.RootViewController;

            while (vc.PresentedViewController != null)
            {
                vc = vc.PresentedViewController;
            }

            vc.PresentViewController(imagePicker, true, null);

            imagePicker.FinishedPickingMedia += (sender, e) =>
            {
                UIImage image = (UIImage)e.Info.ObjectForKey(new NSString("UIImagePickerControllerOriginalImage"));

                UIImage rotateImage = RotateImage(image, image.Orientation);

                rotateImage = rotateImage.Scale(new CGSize(rotateImage.Size.Width, rotateImage.Size.Height), 0.5f);

                var jpegImage = rotateImage.AsJPEG();

                byte[] myByteArray = new byte[jpegImage.Length];
                System.Runtime.InteropServices.Marshal.Copy(jpegImage.Bytes, myByteArray, 0, Convert.ToInt32(jpegImage.Length));

                SamplePage.Cameraimage(myByteArray);

                Device.BeginInvokeOnMainThread(() =>
                {
                    vc.DismissViewController(true, null);
                });
            };


            imagePicker.Canceled += (sender, e) => vc.DismissViewController(true, null);
        }