[英]How to rotate camera view in CaptureElement?
I have a following problem.我有以下问题。 I wrote
StackPanel
with CaptureElement
:我用
CaptureElement
编写了StackPanel
:
<StackPanel>
<CaptureElement x:Name="PreviewElement"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Width="380"
Height="560"
Stretch="UniformToFill"/>
</StackPanel>
In xaml.cs file under that view:在该视图下的 xaml.cs 文件中:
protected override async void OnNavigatedTo(NavigationEventArgs e)
{
cameraCapture = new CameraCapture();
PreviewElement.Source = await cameraCapture.Initialize();
await cameraCapture.StartPreview();
}
And I have a class CameraCapture.cs in which I have all methods:我有一个类 CameraCapture.cs,其中包含所有方法:
public class CameraCapture
{
MediaCapture mediaCapture;
ImageEncodingProperties imgEncodingProperties;
public async Task<MediaCapture> Initialize(CaptureUse primaryUse = CaptureUse.Photo)
{
//Create media capture and INIT
var cameraID = await GetCameraID(Windows.Devices.Enumeration.Panel.Back);
mediaCapture = new MediaCapture();
await mediaCapture.InitializeAsync(new MediaCaptureInitializationSettings
{
StreamingCaptureMode = StreamingCaptureMode.Video,
PhotoCaptureSource = PhotoCaptureSource.Photo,
AudioDeviceId = string.Empty,
VideoDeviceId = cameraID.Id
});
mediaCapture.VideoDeviceController.PrimaryUse = primaryUse;
//Create photo encoding properties as JPEG and set the size that should be use for photo capturing
imgEncodingProperties = ImageEncodingProperties.CreateJpeg();
imgEncodingProperties.Width = 640;
imgEncodingProperties.Height = 480;
return mediaCapture;
}
public async Task StartPreview()
{
//Start preview stream
await mediaCapture.StartPreviewAsync();
}
private static async Task<DeviceInformation> GetCameraID(Windows.Devices.Enumeration.Panel desired)
{
DeviceInformation deviceID = (await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture))
.FirstOrDefault(x => x.EnclosureLocation != null && x.EnclosureLocation.Panel == desired);
if (deviceID != null) return deviceID;
else throw new Exception(string.Format("Camera of type {0} doesn't exist.", desired));
}
}
When I run application in portrait rotation I have camera view rotated by 90 degrees counterclockwise.当我以纵向旋转方式运行应用程序时,我的相机视图逆时针旋转了 90 度。 After I turn to Landscape view I have smaller window in which is camera view but rotation is good.
在我转向横向视图后,我有一个较小的窗口,其中是相机视图,但旋转很好。
Any ideas how to solve this problem?任何想法如何解决这个问题?
To rotate your preview you can use MediaCapture.SetPreviewRotation .要旋转预览,您可以使用MediaCapture.SetPreviewRotation 。 Additianally if you want to rotate the preview once the phone's orintation changes, then you can subscribe to SimpleOrientationSensor.OrientationChanged event.
另外,如果你想在手机的方向改变后旋转预览,那么你可以订阅SimpleOrientationSensor.OrientationChanged事件。 It may look for example like this:
它可能看起来像这样:
captureManager.SetPreviewRotation(VideoRotation.Clockwise90Degrees);
capturePreview.Source = captureManager;
await captureManager.StartPreviewAsync();
SimpleOrientationSensor sensor = SimpleOrientationSensor.GetDefault();
sensor.OrientationChanged += (s, arg) =>
{
switch (arg.Orientation)
{
case SimpleOrientation.Rotated90DegreesCounterclockwise:
captureManager.SetPreviewRotation(VideoRotation.None);
break;
case SimpleOrientation.Rotated180DegreesCounterclockwise:
case SimpleOrientation.Rotated270DegreesCounterclockwise:
captureManager.SetPreviewRotation(VideoRotation.Clockwise180Degrees);
break;
default:
captureManager.SetPreviewRotation(VideoRotation.Clockwise90Degrees);
break;
}
};
Please also remember to unsubscribe from sensor once you finish preview/dispose MediaCapture .还请记住在完成预览/处理MediaCapture 后取消订阅传感器。
I've found the best way to do this is to actually rotate the control or, for full-on camera applications where the camera preview is the main control, the entire page on the OnNavigatedTo event for the page with the CaptureElement:我发现最好的方法是实际旋转控件,或者对于相机预览是主要控件的完整相机应用程序,使用 CaptureElement 的页面的 OnNavigatedTo 事件上的整个页面:
DisplayInformation.AutoRotationPreferences = DisplayOrientations.Landscape;
This will look odd if the user keeps their device in portrait orientation, because your other controls may have the wrong orientation.如果用户将他们的设备保持在纵向,这看起来很奇怪,因为您的其他控件可能有错误的方向。 If you want to handle that, you can do as Romasz suggests, subscribe to the
OrientationChanged
event, and in there apply a RotateTransform
to each the buttons in your UI (except for the AppBar, which should right itself automatically) to counteract the device rotation and keep them right-side-up.如果你想处理这个问题,你可以按照 Romasz 的建议去做,订阅
OrientationChanged
事件,然后在你的 UI 中的每个按钮上应用RotateTransform
(AppBar 除外,它应该自动调整)以抵消设备旋转并保持它们正面朝上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.