Windows 8 应用程序中的 CameraCaptureUI 拍摄镜像照片
CameraCaptureUI in Windows 8 application takes a mirrored photo
我在我的 windows 8 应用程序中编写了拍照服务。我使用了 CameraCaptureUI API。它有效,但结果是,拍摄的图像是镜像的:左边在右边,反之亦然。
我在 CameraCapture 的 PhotoSettings 中搜索,并在互联网上搜索,但没有。
我在想也许解决方案是旋转我从相机流中保存的位图图像。这是我到目前为止尝试过的方法,但没有用:/
public async Task<IPhoto> TakePhotoAsync(string filename)
{
var dialog = new CameraCaptureUI();
dialog.PhotoSettings.CroppedAspectRatio = new Size(4, 3);
dialog.PhotoSettings.Format = CameraCaptureUIPhotoFormat.Png;
StorageFile file = await dialog.CaptureFileAsync( CameraCaptureUIMode.Photo );
if (file != null)
{
var stream = await file.OpenAsync(FileAccessMode.Read);
//create decoder and encoder
BitmapDecoder dec = await BitmapDecoder.CreateAsync(stream);
BitmapEncoder enc = await BitmapEncoder.CreateForTranscodingAsync(stream, dec);
enc.BitmapTransform.Rotation = BitmapRotation.Clockwise180Degrees;
await enc.FlushAsync();
var bitmap = new BitmapImage();
bitmap.SetSource(stream);
return new WPhotos(file);
}
return null;
}
提前感谢您的宝贵时间和帮助:)
如果你想翻转你从流中保存的图像,试试这个:
How to Verticaly Flip an BitmapImage
基本上你应该使用 ScaleTransform
而不是 Rotation
。
将 CameraCaptureUI
拍摄的照片的方向标准化的另一种解决方案是将其解码为 SoftwareBitmap
,然后重新编码:
var captureUI = new CameraCaptureUI();
captureUI.PhotoSettings.Format = CameraCaptureUIPhotoFormat.Jpeg;
captureUI.PhotoSettings.MaxResolution = CameraCaptureUIMaxPhotoResolution.SmallVga;
var photo = await captureUI.CaptureFileAsync(CameraCaptureUIMode.Photo);
if (photo == null) { return false; }
var photoprops = await photo.Properties.GetImagePropertiesAsync();
if (photoprops.Orientation != PhotoOrientation.Normal)
{
using (var readStream = await photo.OpenAsync(FileAccessMode.Read))
{
var decoder = await BitmapDecoder.CreateAsync(readStream);
using (var sb = await decoder.GetSoftwareBitmapAsync())
using (var memStream = new Windows.Storage.Streams.InMemoryRandomAccessStream())
{
BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, memStream);
encoder.SetSoftwareBitmap(sb);
encoder.IsThumbnailGenerated = false;
await encoder.FlushAsync();
// photo data with normalised orientation available in memStream
}
}
}
我在我的 windows 8 应用程序中编写了拍照服务。我使用了 CameraCaptureUI API。它有效,但结果是,拍摄的图像是镜像的:左边在右边,反之亦然。 我在 CameraCapture 的 PhotoSettings 中搜索,并在互联网上搜索,但没有。
我在想也许解决方案是旋转我从相机流中保存的位图图像。这是我到目前为止尝试过的方法,但没有用:/
public async Task<IPhoto> TakePhotoAsync(string filename)
{
var dialog = new CameraCaptureUI();
dialog.PhotoSettings.CroppedAspectRatio = new Size(4, 3);
dialog.PhotoSettings.Format = CameraCaptureUIPhotoFormat.Png;
StorageFile file = await dialog.CaptureFileAsync( CameraCaptureUIMode.Photo );
if (file != null)
{
var stream = await file.OpenAsync(FileAccessMode.Read);
//create decoder and encoder
BitmapDecoder dec = await BitmapDecoder.CreateAsync(stream);
BitmapEncoder enc = await BitmapEncoder.CreateForTranscodingAsync(stream, dec);
enc.BitmapTransform.Rotation = BitmapRotation.Clockwise180Degrees;
await enc.FlushAsync();
var bitmap = new BitmapImage();
bitmap.SetSource(stream);
return new WPhotos(file);
}
return null;
}
提前感谢您的宝贵时间和帮助:)
如果你想翻转你从流中保存的图像,试试这个:
How to Verticaly Flip an BitmapImage
基本上你应该使用 ScaleTransform
而不是 Rotation
。
将 CameraCaptureUI
拍摄的照片的方向标准化的另一种解决方案是将其解码为 SoftwareBitmap
,然后重新编码:
var captureUI = new CameraCaptureUI();
captureUI.PhotoSettings.Format = CameraCaptureUIPhotoFormat.Jpeg;
captureUI.PhotoSettings.MaxResolution = CameraCaptureUIMaxPhotoResolution.SmallVga;
var photo = await captureUI.CaptureFileAsync(CameraCaptureUIMode.Photo);
if (photo == null) { return false; }
var photoprops = await photo.Properties.GetImagePropertiesAsync();
if (photoprops.Orientation != PhotoOrientation.Normal)
{
using (var readStream = await photo.OpenAsync(FileAccessMode.Read))
{
var decoder = await BitmapDecoder.CreateAsync(readStream);
using (var sb = await decoder.GetSoftwareBitmapAsync())
using (var memStream = new Windows.Storage.Streams.InMemoryRandomAccessStream())
{
BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, memStream);
encoder.SetSoftwareBitmap(sb);
encoder.IsThumbnailGenerated = false;
await encoder.FlushAsync();
// photo data with normalised orientation available in memStream
}
}
}