c#UWP相机预览

时间:2018-01-14 15:47:10

标签: c# camera uwp

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

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

但是,它会打开一个新窗口来捕捉Photo,另一个窗口可以裁剪它。

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

1 个答案:

答案 0 :(得分:0)

  

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

您还可以使用MediaCapture类进行相机捕捉,并用于从相机捕捉音频,视频和图像。有关显示相机预览的操作指南。见Display the camera preview。要快速开始捕获照片,音频或视频,请参阅Basic photo, video, and audio capture with 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();
    }
}

这是相对的code sample