/// <summary>
        /// Rotate via EXIF information
        /// </summary>
        /// <param name="photoPath"></param>
        /// <returns></returns>
        public byte[] RotateImage(string photoPath)
        {
            Android.Graphics.BitmapFactory.Options options = new Android.Graphics.BitmapFactory.Options();
            options.InPreferredConfig = Android.Graphics.Bitmap.Config.Argb8888;
            Android.Graphics.Bitmap bitmap = Android.Graphics.BitmapFactory.DecodeFile(photoPath, options);

            try
            {
                Android.Media.ExifInterface exifInterface = new Android.Media.ExifInterface(photoPath);
                int orientation = exifInterface.GetAttributeInt(Android.Media.ExifInterface.TagOrientation, (int)Android.Media.Orientation.Normal);

                int rotate = 0;

                switch (orientation)
                {
                case (int)Android.Media.Orientation.Normal:
                    rotate = 0;
                    break;

                case (int)Android.Media.Orientation.Rotate90:
                    rotate = 90;
                    break;

                case (int)Android.Media.Orientation.Rotate270:
                    rotate = 270;
                    break;

                case (int)Android.Media.Orientation.Rotate180:
                    rotate = 180;
                    break;

                default:
                    rotate = 0;
                    break;
                }

                using (var ms = new System.IO.MemoryStream())
                {
                    Android.Graphics.Bitmap croppedBitmap = null;

                    Android.Graphics.Matrix mtx = new Android.Graphics.Matrix();
                    mtx.PreRotate(rotate);

                    if (bitmap.Width >= bitmap.Height)
                    {
                        croppedBitmap = Android.Graphics.Bitmap.CreateBitmap(
                            bitmap,
                            bitmap.Width / 2 - bitmap.Height / 2,
                            0,
                            bitmap.Height,
                            bitmap.Height,
                            mtx,
                            false);
                    }
                    else
                    {
                        croppedBitmap = Android.Graphics.Bitmap.CreateBitmap(
                            bitmap,
                            0,
                            bitmap.Height / 2 - bitmap.Width / 2,
                            bitmap.Width,
                            bitmap.Width,
                            mtx,
                            false);
                    }

                    croppedBitmap.Compress(Android.Graphics.Bitmap.CompressFormat.Png, 100, ms);

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

                    mtx.Dispose();
                    mtx = null;

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

                    return(ms.ToArray());
                }
            }
            catch
            {
                // <!-- Fail out -->
            }

            return(null);
        }
示例#2
0
        /// <summary>
        /// Take a picture and perform previews
        /// </summary>
        /// <param name="data"></param>
        /// <param name="camera"></param>
        void Camera.IPictureCallback.OnPictureTaken(byte[] data, Android.Hardware.Camera camera)
        {
			if (data != null && _box != null)
            {
				if (!_processDialog.IsShowing) {
					_processDialog.SetMessage ("Waiting for image results...");
					_processDialog.Show ();
				}
				Android.Graphics.BitmapFactory.Options options = new Android.Graphics.BitmapFactory.Options ();
				options.InJustDecodeBounds = true;
				options.InSampleSize = 2;
				options.InJustDecodeBounds = false;
				options.InTempStorage = new byte[16 * 1024];
                Android.Graphics.Bitmap bmp = Android.Graphics.BitmapFactory.DecodeByteArray(data, 0, data.Length, options);
                data = null;
				GC.Collect ();
                Android.Graphics.Bitmap rotatedBitmap;
                if (bmp.Width > bmp.Height)
                {
                    Android.Graphics.Matrix mtx = new Android.Graphics.Matrix();
                    mtx.SetRotate(90);
                    rotatedBitmap = Android.Graphics.Bitmap.CreateBitmap(bmp, 0, 0, bmp.Width, bmp.Height, mtx, false);
                    bmp.Recycle();
                    mtx.Dispose();
                    mtx = null;
                }
                else
                {
                    rotatedBitmap = bmp.Copy(bmp.GetConfig(), true);
                    bmp.Recycle();
                }
				GC.Collect ();
                double ratioX = (double)rotatedBitmap.Width / (double)screenWidth;
                double ratioY = (double)rotatedBitmap.Height / (double)screenHeight;
                int startX = (int)(ratioX * (double)(_box.MidX - _box.Width / 2));
                int startY = (int)(ratioY * (double)(_box.MidY - _box.Height / 2));
                int width = (int)(ratioX * (double)_box.Width);
                int height = (int)(ratioY * (double)_box.Height);
                Android.Graphics.Bitmap croppedBitmap = Android.Graphics.Bitmap.CreateBitmap(rotatedBitmap, startX, startY, width, height);
                rotatedBitmap.Recycle();
				GC.Collect ();
                bitmap = croppedBitmap.Copy(croppedBitmap.GetConfig(), true);
                PerformPreviews(croppedBitmap);
            }
            else
            {
                Toast.MakeText(this, "Data null error", ToastLength.Long).Show();
            }
        }
        private Android.Graphics.Bitmap LoadAndResizeBitmap(string fileName, Size newSize)
        {
            var exif = new ExifInterface(fileName);

            var width       = exif.GetAttributeInt(ExifInterface.TagImageWidth, 100);
            var height      = exif.GetAttributeInt(ExifInterface.TagImageLength, 80);
            var orientation = exif.GetAttribute(ExifInterface.TagOrientation);


            // We calculate the ratio that we need to resize the image by
            // in order to fit the requested dimensions.

            var inSampleSize = 1.0;

            if (newSize.Height < height || newSize.Width < width)
            {
                inSampleSize = newSize.Width > newSize.Height
                    ? newSize.Height / height
                        : newSize.Width / width;
            }

            var options = new Android.Graphics.BitmapFactory.Options {
                InJustDecodeBounds = false,
                InSampleSize       = (int)inSampleSize
            };
            // Now we will load the image and have BitmapFactory resize it for us.
            var resizedBitmap = Android.Graphics.BitmapFactory.DecodeFile(fileName, options);

            var rotate = false;

            switch (orientation)
            {
            case "1":     // landscape
            case "3":     // landscape
                if (width < height)
                {
                    rotate = true;
                }
                break;

            case "8":
            case "4":
            case "6":     // portrait
                if (width > height)
                {
                    rotate = true;
                }
                break;

            case "0":     //undefined
            default:
                break;
            }

            if (rotate)
            {
                var mtx = new Android.Graphics.Matrix();
                mtx.PreRotate(90);
                resizedBitmap = Android.Graphics.Bitmap.CreateBitmap(resizedBitmap, 0, 0, resizedBitmap.Width, resizedBitmap.Height, mtx, false);
                mtx.Dispose();
                mtx = null;
            }


            return(resizedBitmap);
        }
        private Android.Graphics.Bitmap LoadAndResizeBitmap(string fileName, Size newSize) {
            var exif = new ExifInterface(fileName);

            var width = exif.GetAttributeInt(ExifInterface.TagImageWidth, 100);
            var height = exif.GetAttributeInt(ExifInterface.TagImageLength, 80);
            var orientation = exif.GetAttribute(ExifInterface.TagOrientation);


            // We calculate the ratio that we need to resize the image by
            // in order to fit the requested dimensions.

            var inSampleSize = 1.0;

            if (newSize.Height < height || newSize.Width < width) {
                inSampleSize = newSize.Width > newSize.Height
                    ? newSize.Height / height
                        : newSize.Width / width;
            }

            var options = new Android.Graphics.BitmapFactory.Options {
                InJustDecodeBounds = false,
                InSampleSize = (int)inSampleSize
            };
            // Now we will load the image and have BitmapFactory resize it for us.
            var resizedBitmap = Android.Graphics.BitmapFactory.DecodeFile(fileName, options);

            var rotate = false;
            
            switch (orientation) {
                case "1": // landscape
                case "3": // landscape
                    if (width < height)
                        rotate = true;
                    break;
                case "8":
                case "4":
                case "6": // portrait
                    if (width > height)
                        rotate = true;
                    break;
                case "0": //undefined
                default:
                    break;
            }

            if (rotate) {
                var mtx = new Android.Graphics.Matrix();
                mtx.PreRotate(90);
                resizedBitmap = Android.Graphics.Bitmap.CreateBitmap(resizedBitmap, 0, 0, resizedBitmap.Width, resizedBitmap.Height, mtx, false);
                mtx.Dispose();
                mtx = null;

            }


            return resizedBitmap;
        }