简体   繁体   中英

c# UWP Camera preview

I created a camera capture with the code below:

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

But, it opens new window to capture Photo, and another window to crop it.

I want to access my camera directly and show it's preview in my xaml window. Any suggestion?

I want to access my camera directly and show it's preview in my xaml window. Any suggestion?

You could also make a camera capture with MediaCapture class, and it is used to capture audio, video, and images from a camera. For how-to guidance for displaying the camera preview. see Display the camera preview . To quickly get started capturing photos, audio, or video, see Basic photo, video, and audio capture with MediaCapture .

If you want to capture a photo to a file, you could use the following code to realize it.

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

And this is relative code sample .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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