更改媒体流属性分辨率后裁剪照片(Windows Mobile 10)

时间:2017-03-02 14:43:11

标签: c# bitmap uwp windows-10-mobile image-capture

我在C#中为Windows Phone 10制作了一个简单的照片捕捉功能。

捕获设置:

                _captureManager = new MediaCapture();

                await _captureManager.InitializeAsync(new MediaCaptureInitializationSettings
                {
                    StreamingCaptureMode = StreamingCaptureMode.Video,
                    PhotoCaptureSource = PhotoCaptureSource.Photo,
                    AudioDeviceId = string.Empty,
                    VideoDeviceId = cameraId
                });


                var resolutions = captureManager.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.Photo);

                // Here I choose same resolution as native camera does
                await _captureManager.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.Photo, resolutions[3]); 

我拍摄照片:

using (var imageStream = new InMemoryRandomAccessStream())
            {
                var format = ImageEncodingProperties.CreateJpeg();
                var capturefile = await _storeFolder.CreateFileAsync(photoName, CreationCollisionOption.GenerateUniqueName);

                await _captureManager.CapturePhotoToStreamAsync(format, imageStream); 

.....
}

然而,在CapturePhotoToStreamAsync上,我的图片被裁剪了。当我删除SetMediaStreamPropertiesAsync时不会发生这种情况,但它会选择与我的原生相机不同的分辨率。

有谁知道造成这个问题的原因是什么?

1 个答案:

答案 0 :(得分:0)

好的,我找到了答案,所以如果有人需要,请添加。我的相机分辨率为1.64 *。对于此分辨率,宽高比16:9(1.77 *)不起作用。因此,当我尝试设置它时,它会失败。由于此分辨率适用于原生相机,我认为应该有一些设置才能使其正常工作。

但我做了一个解决方法。 首先,我保存照片分辨率信息:

var _photoResolution = VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.Photo)

此后,在拍摄照片时,我使用BitmapDecoder将拍摄的图像更改为所需的分辨率:

 using (var imageStream = new InMemoryRandomAccessStream())
            {
                var photoName = $"myPhoto.jpg";
                var capturefile = await _storeFolder.CreateFileAsync(photoName, CreationCollisionOption.GenerateUniqueName);

                await _captureManager.CapturePhotoToStreamAsync(format, imageStream);

                var dec = await BitmapDecoder.CreateAsync(imageStream);
                var enc = await BitmapEncoder.CreateForTranscodingAsync(imageStream, dec);

                enc.BitmapTransform.ScaledHeight = _photoResolution.Height;
                enc.BitmapTransform.ScaledWidth = _photoResolution.Width;

                // bounds must be set for ScaledHeight and ScaledWidth transform would work, but I don't know why
                // based on device orientation, I am setting proper value for width and height
                var bounds = new BitmapBounds
                {
                    Height = ((deviceOrientation == Enums.DeviceOrientation.Portrait || deviceOrientation == Enums.DeviceOrientation.Flipped) ? _photoResolution.Width : _photoResolution.Height) - 1,
                    Width = ((deviceOrientation == Enums.DeviceOrientation.Portrait || deviceOrientation == Enums.DeviceOrientation.Flipped) ? _photoResolution.Height : _photoResolution.Width) - 1,
                    X = 1,
                    Y = 1
                };
                enc.BitmapTransform.Bounds = bounds;

                await enc.FlushAsync();

                using (var fileStream = await capturefile.OpenStreamForWriteAsync())
                {
                    await RandomAccessStream.CopyAsync(imageStream, fileStream.AsOutputStream());
                }                    
            }