繁体   English   中英

C#UWP相机预览

[英]c# UWP Camera preview

我使用以下代码创建了一个摄像机捕获:

CameraCaptureUI capture = new CameraCaptureUI();
capture.PhotoSettings.Format = CameraCaptureUIPhotoFormat.Jpeg;
capture.PhotoSettings.CroppedAspectRatio = new Windows.Foundation.Size(3, 5);
capture.PhotoSettings.MaxResolution = CameraCaptureUIMaxPhotoResolution.HighestAvailable;
storeFile = await capture.CaptureFileAsync(CameraCaptureUIMode.Photo);
if (storeFile != null)
{
    BitmapImage bimage = new BitmapImage();
    stream = await storeFile.OpenAsync(FileAccessMode.Read);
    bimage.SetSource(stream);
    captureImage.Source = bimage;
}

但是,它将打开新窗口以捕获照片,并打开另一个窗口以对其进行裁剪。

我想直接访问我的相机并在xaml窗口中显示它的预览。 有什么建议吗?

我想直接访问我的相机并在xaml窗口中显示它的预览。 有什么建议吗?

您还可以使用MediaCapture类进行摄像机捕获,该类用于捕获摄像机的音频,视频和图像。 有关如何显示相机预览的指导。 请参阅显示相机预览 要快速开始捕获照片,音频或视频,请参阅使用MediaCapture捕获基本的照片,视频和音频

如果要将照片捕获到文件中,可以使用以下代码实现。

var myPictures = await Windows.Storage.StorageLibrary.GetLibraryAsync(Windows.Storage.KnownLibraryId.Pictures);
StorageFile file = await myPictures.SaveFolder.CreateFileAsync("photo.jpg", CreationCollisionOption.GenerateUniqueName);

using (var captureStream = new InMemoryRandomAccessStream())
{
    await mediaCapture.CapturePhotoToStreamAsync(ImageEncodingProperties.CreateJpeg(), captureStream);

    using (var fileStream = await file.OpenAsync(FileAccessMode.ReadWrite))
    {
        var decoder = await BitmapDecoder.CreateAsync(captureStream);
        var encoder = await BitmapEncoder.CreateForTranscodingAsync(fileStream, decoder);

        var properties = new BitmapPropertySet {
            { "System.Photo.Orientation", new BitmapTypedValue(PhotoOrientation.Normal, PropertyType.UInt16) }
        };
        await encoder.BitmapProperties.SetPropertiesAsync(properties);

        await encoder.FlushAsync();
    }
}

这是相对的代码示例

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM