Ejemplo n.º 1
0
        protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
        {
            base.OnActivityResult(requestCode, resultCode, data);

            try
            {
                if (resultCode == Result.Ok)
                {
                    if (requestCode == SelectPictureRequestCode)
                    {
                        if (data == null)
                        {
                            Tcs.SetResult(null);
                            Tcs = null;
                            return;
                        }

                        try
                        {
                            var input  = ContentResolver.OpenInputStream(data.Data);
                            var bitmap = BitmapFactory.DecodeStream(input);

                            if (bitmap == null)
                            {
                                Tcs.SetResult(null);
                                Tcs = null;
                                return;
                            }

                            var path = BitmapExtension.GetRealPathFromUri(this, data.Data);

                            var exif = new ExifInterface(path);

                            var rotation          = (Orientation)exif.GetAttributeInt(ExifInterface.TagOrientation, (int)Android.Media.Orientation.Undefined);
                            var rotationInDegrees = BitmapExtension.ExifToDegrees(rotation);
                            var matrix            = new Matrix();

                            if (rotation != Orientation.Normal && rotationInDegrees != 0)
                            {
                                var original = bitmap;
                                matrix.PreRotate(rotationInDegrees);
                                var rotatedBitmap = Bitmap.CreateBitmap(original, 0, 0, original.Width, original.Height, matrix, true);

                                bitmap = rotatedBitmap;

                                original.Recycle();
                                original.Dispose();
                            }

                            if (bitmap.Width > 720 || bitmap.Height > 1280)
                            {
                                bitmap = bitmap.ResizeBitmap720x1280();
                            }


                            var bytes = Bytes(bitmap);

                            bitmap.Recycle();

                            Tcs.SetResult(bytes);
                            Tcs = null;
                        }
                        catch (Exception e)
                        {
                            Tcs.SetResult(null);
                            Tcs = null;
                        }
                    }
                    else if (requestCode == TakePictureRequestCode)
                    {
                        if (PhotoFile != null)
                        {
                            var bitmap = LoadImage(this, ImagesFolder, PhotoFile.Name);

                            // Update it available in gallery
                            if (PhotoFile != null)
                            {
                                Intent mediaScanIntent = new Intent(Intent.ActionMediaScannerScanFile);
                                Uri    contentUri      = Uri.FromFile(PhotoFile);
                                mediaScanIntent.SetData(contentUri);
                                SendBroadcast(mediaScanIntent);
                            }

                            //

                            if (bitmap != null)
                            {
                                var path = GetImagesPath(this, ImagesFolder) + "/";

                                var exif   = new ExifInterface(path + PhotoFile.Name);
                                var matrix = new Matrix();

                                var orientation =
                                    (Orientation)
                                    exif.GetAttributeInt(ExifInterface.TagOrientation, (int)Orientation.Undefined);

                                switch (orientation)
                                {
                                case Orientation.Undefined:     // Nexus 7 landscape...
                                    break;

                                case Orientation.Normal:     // landscape
                                    break;

                                case Orientation.FlipHorizontal:
                                    break;

                                case Orientation.Rotate180:
                                    matrix.PreRotate(180);
                                    bitmap = Bitmap.CreateBitmap(bitmap, 0, 0, bitmap.Width, bitmap.Height, matrix,
                                                                 false);
                                    matrix.Dispose();
                                    matrix = null;
                                    break;

                                case Orientation.FlipVertical:
                                    break;

                                case Orientation.Transpose:
                                    break;

                                case Orientation.Rotate90:     // portrait
                                    matrix.PreRotate(90);
                                    bitmap = Bitmap.CreateBitmap(bitmap, 0, 0, bitmap.Width, bitmap.Height, matrix,
                                                                 false);
                                    matrix.Dispose();
                                    matrix = null;
                                    break;

                                case Orientation.Transverse:
                                    break;

                                case Orientation.Rotate270:     // might need to flip horizontally too...
                                    matrix.PreRotate(270);
                                    bitmap = Bitmap.CreateBitmap(bitmap, 0, 0, bitmap.Width, bitmap.Height, matrix,
                                                                 false);
                                    matrix.Dispose();
                                    matrix = null;
                                    break;

                                default:
                                    matrix.PreRotate(90);
                                    bitmap = Bitmap.CreateBitmap(bitmap, 0, 0, bitmap.Width, bitmap.Height, matrix,
                                                                 false);
                                    matrix.Dispose();
                                    matrix = null;
                                    break;
                                }



                                if (bitmap.Width > 720 || bitmap.Height > 1280)
                                {
                                    bitmap = bitmap.ResizeBitmap720x1280();
                                }

                                var bytes = Bytes(bitmap);

                                bitmap.Recycle();
                                bitmap.Dispose();
                                bitmap = null;

                                //PhotoFile.Delete();
                                PhotoFile = null;

                                Tcs.SetResult(bytes);
                                Tcs = null;
                            }
                        }
                        else
                        {
                            Tcs.SetResult(null);
                            Tcs = null;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Toast.MakeText(this, ex.Message, ToastLength.Long).Show();
            }
        }