/// <summary> /// Rotate the picture taken /// https://forums.xamarin.com/discussion/5409/photo-being-saved-in-landscape-not-portrait /// </summary> /// <param name="path"></param> /// <param name="width"></param> /// <param name="height"></param> /// <returns></returns> void RotateBitmap(string path, byte[] data) { try { //using (Android.Graphics.Bitmap picture = Android.Graphics.BitmapFactory.DecodeFile(path)) using (Android.Graphics.Bitmap picture = data.ToBitmap()) using (Android.Graphics.Matrix mtx = new Android.Graphics.Matrix()) { ExifInterface exif = new ExifInterface(path); string orientation = exif.GetAttribute(ExifInterface.TagOrientation); var camOrientation = int.Parse(orientation); switch (_imageRotation) { case 0: // landscape break; case 90: // landscape upside down mtx.PreRotate(270); break; case 180: // portrait mtx.PreRotate(180); break; case 270: // portrait upside down mtx.PreRotate(90); break; } var maxSize = 1024; double w = picture.Width; double h = picture.Height; if (picture.Width > maxSize || picture.Height > maxSize) { // set scaled width and height to prevent out of memory exception double scaleFactor = (double)maxSize / (double)picture.Width; if (picture.Height > picture.Width) { scaleFactor = (double)maxSize / picture.Height; } w = picture.Width * scaleFactor; h = picture.Height * scaleFactor; } using (var scaledPiture = Android.Graphics.Bitmap.CreateScaledBitmap(picture, (int)w, (int)h, false)) using (var rotatedPiture = Android.Graphics.Bitmap.CreateBitmap(scaledPiture, 0, 0, (int)w, (int)h, mtx, false)) { SaveFile(path, rotatedPiture.ToBytes()); } } } catch (Java.Lang.OutOfMemoryError e) { e.PrintStackTrace(); throw; } }
public async void OnPictureTaken(byte[] data, Camera camera) { img_container.bitma_pic = Android.Graphics.BitmapFactory.DecodeByteArray(data, 0, data.Length); string cur_time_milisec = System.DateTime.Now.Millisecond.ToString(); string cur_time_sec = System.DateTime.Now.Second.ToString(); img_container.temp_storage = new Java.IO.File(img_container.path_file, "image" + cur_time_milisec + cur_time_sec + ".jpg"); if (!img_container.temp_storage.Exists()) { img_container.temp_storage.CreateNewFile(); } img_container.absolut_path_pic = img_container.temp_storage.AbsolutePath; Android.Graphics.Matrix rotation_matrix = new Android.Graphics.Matrix(); //caz ii camera spate if (gcur_cam != gDef_front_cam) { rotation_matrix.PreRotate(90); } else //camera frontala { rotation_matrix.PreRotate(270); } img_container.bitma_pic = Android.Graphics.Bitmap.CreateBitmap(img_container.bitma_pic, 0, 0, img_container.bitma_pic.Width, img_container.bitma_pic.Height, rotation_matrix, true); MemoryStream stream = new MemoryStream(); Android.Graphics.Bitmap bmp = img_container.bitma_pic; bmp.Compress(Android.Graphics.Bitmap.CompressFormat.Jpeg, 100, stream); byte[] byte_array = stream.ToArray(); FileOutputStream outstream = new FileOutputStream(img_container.temp_storage); outstream.Write(byte_array); outstream.Flush(); outstream.Close(); }
/// <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); }
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; }