Guest User

Untitled

a guest
Feb 20th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. public async void OnPictureTaken(byte[] data, Android.Hardware.Camera camera)
  2. {
  3. var targetWidth = 1080;
  4. var cameraParams = camera.GetParameters();
  5. var ratio = ((decimal)cameraParams.PictureSize.Height) / cameraParams.PictureSize.Width;
  6.  
  7. var w = cameraParams.PictureSize.Width >= targetWidth
  8. ? targetWidth
  9. : cameraParams.PictureSize.Width;
  10.  
  11. var h = cameraParams.PictureSize.Width >= targetWidth
  12. ? (int)Math.Floor(targetWidth * ratio)
  13. : cameraParams.PictureSize.Height;
  14.  
  15. Java.IO.File sdDir = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryPictures);
  16. Java.IO.File pictureFileDir = new Java.IO.File(sdDir, "RDCCameraImages");
  17.  
  18. if (!pictureFileDir.Exists() && !pictureFileDir.Mkdirs())
  19. {
  20. return;
  21. }
  22. String photoFile = "Picture_" + "aaa.jpg";
  23. String imageFilePath = pictureFileDir.Path + Java.IO.File.Separator + photoFile;
  24. Java.IO.File pictureFile = new Java.IO.File(imageFilePath);
  25.  
  26. FileOutputStream fos = new FileOutputStream(pictureFile);
  27. fos.Write(data);
  28. fos.Close();
  29.  
  30. ExifInterface exif;
  31.  
  32. ExifInterface ei = new ExifInterface(pictureFile.AbsolutePath);
  33. int orientation = ei.GetAttributeInt(ExifInterface.TagOrientation, (int)Android.Media.Orientation.Normal);
  34.  
  35. var imageStream = new System.IO.MemoryStream();
  36. using (var bm = BitmapFactory.DecodeByteArray(data, 0, data.Length))
  37. using (var scaled = Bitmap.CreateScaledBitmap(bm, w, h, false))
  38. {
  39. Bitmap rotatedBitmap = null;
  40. switch (orientation)
  41. {
  42.  
  43. case (int)Android.Media.Orientation.Rotate90:
  44. rotatedBitmap = rotateImage(scaled, 90);
  45. break;
  46.  
  47. case (int)Android.Media.Orientation.Rotate180:
  48. rotatedBitmap = rotateImage(scaled, 180);
  49. break;
  50.  
  51. case (int)Android.Media.Orientation.Rotate270:
  52. rotatedBitmap = rotateImage(scaled, 270);
  53. break;
  54.  
  55. case (int)Android.Media.Orientation.Normal:
  56. rotatedBitmap = scaled;
  57. break;
  58. default:
  59. rotatedBitmap = scaled;
  60. break;
  61. }
  62. await scaled.CompressAsync(Bitmap.CompressFormat.Jpeg, 90, imageStream);
  63. await imageStream.FlushAsync();
  64. }
  65. }
Add Comment
Please, Sign In to add comment