天天看點

Windows Phone開發技巧:照片角度處理

在Windows phone實際項目中,可能需要使用者從相冊中選擇圖檔然後進行相應的處理。但是不知道大家有沒有發現這樣一種情況,就是手機裡看是豎着的,但是上傳到微網誌或者哪裡的時候确實橫着的。一種情況是你拿手機豎着拍照得話,照片就是橫着的,雖然在手機裡看是豎着的。(可能有點抽象,遇到此情況的同學應該深有感觸)

  那麼我們在用戶端中應該如何處理這種情況呢?一種想法是擷取圖檔的角度,如果是90°,就把照片翻轉過來,再進行相應的操作。那這樣就涉及到2個問題

  1. 如何擷取相冊中照片的角度

  2. 如何翻轉已有的照片(流、或者Bitmap或者WriteableBitmap)

  檢視了系統的API,并沒有對相片的角度提供支援,但是我們可以使用ExifLib開源庫去做。

  下述的方法就是擷取選取圖檔的角度的

  ///

  /// get angle of photo

  /// photo stream

  /// photo name

  /// angle of the photo

  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;

  case ExifOrientation.BottomRight:

  _angle = 180;

  case ExifOrientation.BottomLeft:

  _angle = 270;

  }

  return _angle;

  擷取到角度後,如果角度是90°,即是反的,我們需要将其糾正過來,可以使用如下的方法:

  private Stream RotateStream(Stream stream, int angle)

  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];

  case 180:

  wbTarget.Pixels[(wbSource.PixelWidth - x - 1) + (wbSource.PixelHeight - y - 1) * wbSource.PixelWidth] = wbSource.Pixels[x + y * wbSource.PixelWidth];

  case 270:

  wbTarget.Pixels[y + (wbSource.PixelWidth - x - 1) * wbTarget.PixelWidth] = wbSource.Pixels[x + y * wbSource.PixelWidth];

  MemoryStream targetStream = new MemoryStream();

  wbTarget.SaveJpeg(targetStream, wbTarget.PixelWidth, wbTarget.PixelHeight, 0, 100);

  return targetStream;

本文轉自 wws5201985 51CTO部落格,原文連結:http://blog.51cto.com/wws5201985/751291,如需轉載請自行聯系原作者

繼續閱讀