public static Android.Graphics.Bitmap rotateBitmap(Android.Graphics.Bitmap bitmap, int orientation) { Android.Graphics.Matrix matrix = new Android.Graphics.Matrix(); switch (orientation) { case (int)Android.Media.Orientation.Normal: return(bitmap); case (int)Android.Media.Orientation.FlipHorizontal: matrix.SetScale(-1, 1); break; case (int)Android.Media.Orientation.Rotate180: matrix.SetRotate(180); break; case (int)Android.Media.Orientation.FlipVertical: matrix.SetRotate(180); matrix.PostScale(-1, 1); break; case (int)Android.Media.Orientation.Transpose: matrix.SetRotate(90); matrix.PostScale(-1, 1); break; case (int)Android.Media.Orientation.Rotate90: matrix.SetRotate(90); break; case (int)Android.Media.Orientation.Transverse: matrix.SetRotate(-90); matrix.PostScale(-1, 1); break; case (int)Android.Media.Orientation.Rotate270: matrix.SetRotate(-90); break; default: return(bitmap); } try { Android.Graphics.Bitmap bmRotated = Android.Graphics.Bitmap.CreateBitmap(bitmap, 0, 0, bitmap.Width, bitmap.Height, matrix, true); bitmap.Recycle(); return(bmRotated); } catch (Java.Lang.OutOfMemoryError e) { e.PrintStackTrace(); return(null); } }
/// <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(); } }