快捷搜索:  as  test  1111  test aNd 8=8  test++aNd+8=8  as++aNd+8=8  as aNd 8=8

和记娱乐安卓下载和:【Windows Phone实用开发教程】32、照片角度处理示例



在实际项目中,可能必要用户从相册中选择图片然落和记娱乐安卓下载和后行响应的处置惩罚。然则不知道大年夜家有没有发明这样一种环境,便是手机里看是竖着的,然则上传到微博或者哪里的时刻确凿横着的。一种环境是你拿手机竖着摄影得话,照片便是横着的,虽然在手机里看是竖着的。(可能有点抽象,碰到此环境的同砚应该深有感触)

那么我们在客户端中应该若何处置惩罚这种环境呢?一种设法主见是获取图片的角度,假如是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 Exi和记娱乐安卓下载和fOrientation.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°,等于反的,我们必要将其矫正过来,可以应用如下的措施:

private Stream RotateStream(Stream stream, int angle)

{stream.Position = 0;

if (angle % 90 != 0 || angle 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

{for (int y = 0; y

{switch (angle % 360和记娱乐安卓下载和)

{case 90:

wbTarget.Pixels[(wbSo和记娱乐安卓下载和urce.PixelHeight - y - 1) + x * wbTarget.PixelWidth] = wbSource.Pixels[x + y * wbSource.PixelWidth];break;

case 180:wbTarget.Pixels[(wbSource.PixelWidth - x - 1) + (wbSource.Pixel和记娱乐安卓下载和Height - 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源代码下载

原文Handling picture orientation in CameraCaptureTask in Windows Phone 7

免责声明:以上内容源自网络,版权归原作者所有,如有侵犯您的原创版权请告知,我们将尽快删除相关内容。

您可能还会对下面的文章感兴趣: