上期弄好了相机的预览功能,现在可以进行拍照了。对上期有疑惑的点这里。
MediaCapture的教程是用UWP写的,教程里拍照是用MediaCapture.CapturePhotoToStorageFileAsync方法,老实说我研究了好久都没搞明白WPF什么创建IStorageFile或者IRandomAccessStream对象,最终放弃了使用CapturePhotoToStorageFileAsync,选择了低快门的PrepareLowLagPhotoCaptureAsync方法。代码如下:
private async void Photo_Click(object sender, RoutedEventArgs e)
{if (Capture is null) return;//控制设备低快门拍照。其实教程用的是CapturePhotoToStorageFileAsync,但那是UWP的内容,我没找到在WPF上使用的方法。var iep = ImageEncodingProperties.CreateUncompressed(MediaPixelFormat.Nv12);var ahc = await Capture.PrepareLowLagPhotoCaptureAsync(iep);var res = await ahc.CaptureAsync();var bmp = res.Frame.SoftwareBitmap;await ahc.FinishAsync();//读取缓冲区using var m = bmp.LockBuffer(BitmapBufferAccessMode.Read);using var n = m.CreateReference();var buf = new Windows.Storage.Streams.Buffer(n.Capacity);var data = new byte[n.Capacity];bmp.CopyToBuffer(buf);using var reader = DataReader.FromBuffer(buf);reader.ReadBytes(data);//保存文件var sfd = new SaveFileDialog() {Filter = "图片|*.jpg;",FileName = $"{DateTimeOffset.Now.Ticks}.jpg",DefaultDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures)};if (sfd.ShowDialog() != true) return;//using var mat = new Mat(bmp.PixelHeight * 3 / 2, bmp.PixelWidth, MatType.CV_8UC1, data); 不知道啥时候起不能这么写了,吐槽一下。using var nv12 = new Mat(bmp.PixelHeight * 3 / 2, bmp.PixelWidth, MatType.CV_8UC1);Marshal.Copy(data, 0, nv12.Data, data.Length);using var bgr = new Mat();Cv2.CvtColor(nv12, bgr, ColorConversionCodes.YUV2BGR_NV12);bgr.SaveImage(sfd.FileName);
}
一般来说到这一步就算结束了,但考虑到很多时候领导都会提这么个需求:延时要低,清晰度要高,偏偏机器性能还不行。这种时候我们开发就需要在预览时用较低像素的画面糊弄一下,然后拍照时输出高像素的图片。
之前初始化MediaCapture时使用的是只读模式,现在需要改成控制模式:
var capture = new MediaCapture();
var settings = new MediaCaptureInitializationSettings()
{SourceGroup = device,SharingMode = MediaCaptureSharingMode.ExclusiveControl,//改了这里。换成可控制设备。StreamingCaptureMode = StreamingCaptureMode.Video,MemoryPreference = MediaCaptureMemoryPreference.Cpu,
};
await capture.InitializeAsync(settings);
然后拍照(调用PrepareLowLagPhotoCaptureAsync)之前加上这两句:
var max = Capture.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.Photo).OfType<VideoEncodingProperties>().OrderByDescending(p => p.Width * p.Height).First();
await Capture.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.Photo, max);
上面是正统方法,但这么写的话,从点击按钮到弹出保存文件的窗口要花上大半秒,延时非常明显。我仔细研究后发现,不需要更改设备,只需要设置传入的ImageEncodingProperties的宽高就好。我也不确定是不是真的变清晰了,肉眼看不出来。
var max = Capture.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.Photo).OfType<VideoEncodingProperties>().OrderByDescending(p => p.Width * p.Height).First();
//await Capture.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.Photo, max);
//控制设备低快门拍照。其实教程用的是CapturePhotoToStorageFileAsync,但那是UWP的内容,我没找到在WPF上使用的方法。
var iep = ImageEncodingProperties.CreateUncompressed(MediaPixelFormat.Nv12);
iep.Width = max.Width;
iep.Height = max.Height;
var ahc = await Capture.PrepareLowLagPhotoCaptureAsync(iep);
可以看到,预览和照片是两种不同的像素
收工,下面贴出完整的代码:
using Microsoft.Win32;
using OpenCvSharp;
using OpenCvSharp.WpfExtensions;
using System.DirectoryServices.ActiveDirectory;
using System.IO;
using System.Reflection.PortableExecutable;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Windows.Graphics.Imaging;
using Windows.Media.Capture;
using Windows.Media.Capture.Frames;
using Windows.Media.MediaProperties;
using Windows.Storage.Streams;
using Window = System.Windows.Window;namespace VideoCapture
{/// <summary>/// Interaction logic for MainWindow.xaml/// </summary>public partial class MainWindow : Window{private MediaFrameReader? FrameReader = null;private MediaCapture? Capture = null;public MainWindow(){InitializeComponent();}protected override void OnSourceInitialized(EventArgs e){base.OnSourceInitialized(e);Task.Run(CaptureInit);}private async Task CaptureInit(){//查找摄像头设备,这里仅作示例,只取第一个。实际开发请根据实际情况变通。var device = (await MediaFrameSourceGroup.FindAllAsync()).FirstOrDefault();if (device is null) return;//初始化 MediaCapture 对象var capture = new MediaCapture();var settings = new MediaCaptureInitializationSettings(){SourceGroup = device,SharingMode = MediaCaptureSharingMode.ExclusiveControl,//控制设备。StreamingCaptureMode = StreamingCaptureMode.Video,MemoryPreference = MediaCaptureMemoryPreference.Cpu,//这里不要用auto,不然可能会读不到帧数据。};await capture.InitializeAsync(settings);//获取预览流foreach (MediaFrameSource source in capture.FrameSources.Values){MediaFrameSourceKind kind = source.Info.SourceKind;if (kind != MediaFrameSourceKind.Color) continue;//为了方便,这里只要Nv12的,实际开发可根据情况变通if (source.CurrentFormat.Subtype.ToUpper() != "NV12"){//查找指定的格式foreach(var f in source.SupportedFormats){if (f.Subtype.ToUpper() != "NV12") continue;//尝试设置格式try { await source.SetFormatAsync(f); }catch { continue; }}}//创建帧数据读取设备MediaFrameReader frameReader = await capture.CreateFrameReaderAsync(source, MediaEncodingSubtypes.Nv12);frameReader.FrameArrived += Reader_FrameArrived;MediaFrameReaderStartStatus status = await frameReader.StartAsync();if (status == MediaFrameReaderStartStatus.Success){FrameReader = frameReader;Capture = capture;break;}//没能正确创建,将其释放掉await frameReader.StopAsync();frameReader.Dispose();}}/// <summary>/// 帧数据缓冲区/// </summary>Windows.Storage.Streams.Buffer FrameBuf { get; set; }/// <summary>/// 帧数据/// </summary>public byte[] FrameData { get; private set; }/// <summary>/// NV12帧数据/// </summary>Mat FrameNV12 { get; set; }/// <summary>/// Bgr帧数据/// </summary>Mat FrameBgr { get; set; }/// <summary>/// 捕获帧/// </summary>private void Reader_FrameArrived(MediaFrameReader sender, MediaFrameArrivedEventArgs args){var frame = sender.TryAcquireLatestFrame()?.VideoMediaFrame?.SoftwareBitmap;if (frame is null) return;//创建缓冲if (FrameNV12 is null || FrameNV12.Width != frame.PixelWidth || FrameNV12.Height != frame.PixelHeight){using var lockbuf = frame.LockBuffer(Windows.Graphics.Imaging.BitmapBufferAccessMode.Read);if (lockbuf is null) return;using var n = lockbuf.CreateReference();var t = lockbuf.GetPlaneDescription(0);FrameBuf = new Windows.Storage.Streams.Buffer(n.Capacity);FrameNV12 = new Mat(frame.PixelHeight * 3 / 2, frame.PixelWidth, MatType.CV_8UC1);FrameBgr = new Mat();}//读取数据frame.CopyToBuffer(FrameBuf);if (FrameData is null || FrameData.Length != FrameBuf.Length) FrameData = new byte[FrameBuf.Length];using var reader = DataReader.FromBuffer(FrameBuf);reader.ReadBytes(FrameData);//NV12转Rbg24Marshal.Copy(FrameData, 0, FrameNV12.Data, FrameData.Length);Cv2.CvtColor(FrameNV12, FrameBgr, ColorConversionCodes.YUV2BGR_NV12);Dispatcher.InvokeAsync(() => RendererFrame(FrameBgr, frame.PixelWidth, frame.PixelHeight));}/// <summary>/// 渲染帧/// </summary>private void RendererFrame(Mat bgr, int width, int height){if (bgr is null || width <= 0 || height <= 0 || FrameBgr.Width != width || FrameBgr.Height != height) return;if (FrameImage.Source is not WriteableBitmap bmp || bmp.PixelWidth != width || bmp.PixelHeight != height){bmp = new WriteableBitmap(width, height, 96, 96, PixelFormats.Bgr24, default);FrameImage.Source = bmp;}WriteableBitmapConverter.ToWriteableBitmap(bgr, bmp);//显示当前预览像素,可删除Rel.Text = $"{width}x{height}";}/// <summary>/// 拍照/// </summary>private async void Photo_Click(object sender, RoutedEventArgs e){if (Capture is null) return;//预览可以使用较小的清晰度以保证流畅,但照片需要设置最高的像素。var max = Capture.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.Photo).OfType<VideoEncodingProperties>().OrderByDescending(p => p.Width * p.Height).First();//await Capture.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.Photo, max);//控制设备低快门拍照。其实教程用的是CapturePhotoToStorageFileAsync,但那是UWP的内容,我没找到在WPF上使用的方法。var iep = ImageEncodingProperties.CreateUncompressed(MediaPixelFormat.Nv12);iep.Width = max.Width;iep.Height = max.Height;var ahc = await Capture.PrepareLowLagPhotoCaptureAsync(iep);var res = await ahc.CaptureAsync();var bmp = res.Frame.SoftwareBitmap;await ahc.FinishAsync();//读取缓冲区using var m = bmp.LockBuffer(BitmapBufferAccessMode.Read);using var n = m.CreateReference();var buf = new Windows.Storage.Streams.Buffer(n.Capacity);var data = new byte[n.Capacity];bmp.CopyToBuffer(buf);using var reader = DataReader.FromBuffer(buf);reader.ReadBytes(data);//保存文件var sfd = new SaveFileDialog() {Filter = "图片|*.jpg;",FileName = $"{DateTimeOffset.Now.Ticks}.jpg",DefaultDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures)};if (sfd.ShowDialog() != true) return;//using var mat = new Mat(bmp.PixelHeight * 3 / 2, bmp.PixelWidth, MatType.CV_8UC1, data); 不知道啥时候起不能这么写了,吐槽一下。using var nv12 = new Mat(bmp.PixelHeight * 3 / 2, bmp.PixelWidth, MatType.CV_8UC1);Marshal.Copy(data, 0, nv12.Data, data.Length);using var bgr = new Mat();Cv2.CvtColor(nv12, bgr, ColorConversionCodes.YUV2BGR_NV12);bgr.SaveImage(sfd.FileName);}}
}