Created
April 20, 2016 18:37
-
-
Save jsturtevant/ebd6bbb2570797c659ce6fbbb7d06df1 to your computer and use it in GitHub Desktop.
Saving file in android using Xamarin native. Pieced together from https://forums.xamarin.com/discussion/5409/photo-being-saved-in-landscape-not-portrait
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//usage | |
int height = Resources.DisplayMetrics.HeightPixels; | |
int width = _imageView.Height; | |
Bitmap bitmap = _file.Path.ResizeAndRotate(width, height); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static Bitmap ResizeAndRotate(this string filePath, int width, int height) | |
{ | |
// First we get the the dimensions of the file on disk | |
BitmapFactory.Options options = new BitmapFactory.Options { InJustDecodeBounds = true }; | |
// Next we calculate the ratio that we need to resize the image by | |
// in order to fit the requested dimensions. | |
int outHeight = options.OutHeight; | |
int outWidth = options.OutWidth; | |
int inSampleSize = 1; | |
if (outHeight > height || outWidth > width) | |
{ | |
inSampleSize = outWidth > outHeight | |
? outHeight / height | |
: outWidth / width; | |
} | |
// Now we will load the image and have BitmapFactory resize it for us. | |
options.InSampleSize = inSampleSize; | |
options.InJustDecodeBounds = false; | |
Bitmap resizedBitmap = BitmapFactory.DecodeFile(filePath, options); | |
// Images are being saved in landscape, so rotate them back to portrait if they were taken in portrait | |
Matrix mtx = new Matrix(); | |
ExifInterface exif = new ExifInterface(filePath); | |
var orientation = (Android.Media.Orientation)exif.GetAttributeInt(ExifInterface.TagOrientation, (int)Android.Media.Orientation.Undefined); | |
switch (orientation) | |
{ | |
case Android.Media.Orientation.Undefined: // Nexus 7 landscape... | |
break; | |
case Android.Media.Orientation.Normal: // landscape | |
break; | |
case Android.Media.Orientation.FlipHorizontal: | |
break; | |
case Android.Media.Orientation.Rotate180: | |
mtx.PreRotate(180); | |
resizedBitmap = Bitmap.CreateBitmap(resizedBitmap, 0, 0, resizedBitmap.Width, resizedBitmap.Height, mtx, false); | |
mtx.Dispose(); | |
mtx = null; | |
break; | |
case Android.Media.Orientation.FlipVertical: | |
break; | |
case Android.Media.Orientation.Transpose: | |
break; | |
case Android.Media.Orientation.Rotate90: // portrait | |
mtx.PreRotate(90); | |
resizedBitmap = Bitmap.CreateBitmap(resizedBitmap, 0, 0, resizedBitmap.Width, resizedBitmap.Height, mtx, false); | |
mtx.Dispose(); | |
mtx = null; | |
break; | |
case Android.Media.Orientation.Transverse: | |
break; | |
case Android.Media.Orientation.Rotate270: // might need to flip horizontally too... | |
mtx.PreRotate(270); | |
resizedBitmap = Bitmap.CreateBitmap(resizedBitmap, 0, 0, resizedBitmap.Width, resizedBitmap.Height, mtx, false); | |
mtx.Dispose(); | |
mtx = null; | |
break; | |
default: | |
mtx.PreRotate(90); | |
resizedBitmap = Bitmap.CreateBitmap(resizedBitmap, 0, 0, resizedBitmap.Width, resizedBitmap.Height, mtx, false); | |
mtx.Dispose(); | |
mtx = null; | |
break; | |
} | |
return resizedBitmap; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment