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



我有一个简单的照片捕获功能,用于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.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());
                }                    
            }

最新更新