在实际项目中,可能须要用户从相册中选择图片而后进行相应的处理。可是不知道你们有没有发现这样一种状况,就是手机里看是竖着的,可是上传到微博或者哪里的时候确实横着的。一种状况是你拿手机竖着拍照得话,照片就是横着的,虽然在手机里看是竖着的。(可能有点抽象,遇到此状况的同窗应该深有感触) windows
那么咱们在客户端中应该如何处理这种状况呢?一种想法是获取图片的角度,若是是90°,就把照片翻转过来,再进行相应的操做。那这样就涉及到2个问题ide
1. 如何获取相册中照片的角度spa
2. 如何翻转已有的照片(流、或者Bitmap或者WriteableBitmap)code
查看了系统的API,并无对相片的角度提供支持,可是咱们能够使用ExifLib开源库去作。blog
下述的方法就是获取选取图片的角度的图片
/// <summary> /// get angle of photo /// </summary> /// <param name="stream">photo stream</param> /// <param name="filename">photo name</param> /// <returns>angle of the photo</returns> public static int GetAngle(Stream stream, string filename) { ExifLib.ExifOrientation _orientation; int _angle = 0; stream.Position = 0; JpegInfo info = ExifReader.ReadJpeg(stream, filename); if (info!=null) { _orientation = info.Orientation; switch (info.Orientation) { case ExifOrientation.TopLeft: case ExifOrientation.Undefined: _angle = 0; break; case ExifOrientation.TopRight: _angle = 90; break; case ExifOrientation.BottomRight: _angle = 180; break; case ExifOrientation.BottomLeft: _angle = 270; break; } } return _angle; }
获取到角度后,若是角度是90°,便是反的,咱们须要将其纠正过来,能够使用以下的方法:get
private Stream RotateStream(Stream stream, int angle) { stream.Position = 0; if (angle % 90 != 0 || angle < 0) throw new ArgumentException(); if (angle % 360 == 0) return stream; BitmapImage bitmap = new BitmapImage(); bitmap.SetSource(stream); WriteableBitmap wbSource = new WriteableBitmap(bitmap); WriteableBitmap wbTarget = null; if (angle % 180 == 0) { wbTarget = new WriteableBitmap(wbSource.PixelWidth, wbSource.PixelHeight); } else { wbTarget = new WriteableBitmap(wbSource.PixelHeight, wbSource.PixelWidth); } for (int x = 0; x < wbSource.PixelWidth; x++) { for (int y = 0; y < wbSource.PixelHeight; y++) { switch (angle % 360) { case 90: wbTarget.Pixels[(wbSource.PixelHeight - y - 1) + x * wbTarget.PixelWidth] = wbSource.Pixels[x + y * wbSource.PixelWidth]; break; case 180: wbTarget.Pixels[(wbSource.PixelWidth - x - 1) + (wbSource.PixelHeight - y - 1) * wbSource.PixelWidth] = wbSource.Pixels[x + y * wbSource.PixelWidth]; break; case 270: wbTarget.Pixels[y + (wbSource.PixelWidth - x - 1) * wbTarget.PixelWidth] = wbSource.Pixels[x + y * wbSource.PixelWidth]; break; } } } MemoryStream targetStream = new MemoryStream(); wbTarget.SaveJpeg(targetStream, wbTarget.PixelWidth, wbTarget.PixelHeight, 0, 100); return targetStream; }
Demo源代码下载string
原文Handling picture orientation in CameraCaptureTask in Windows Phone 7it