我正在尝试使用WPF-MediaKit VideoCaptureElement
来预览网络摄像头视频,如果一个人在一个帧中,则解码二维码,因此我需要访问由网络摄像头录制的每个帧。根据文档,它需要EnableSampleGrabbing
选项和NewVideoSample
事件处理程序。
至于现在我的代码看起来像这样。
public partial class QrScannerControl : UserControl
{
public delegate void QrResultDelegate(string qr);
public QrResultDelegate OnQrDeoded { get; set; }
private QRCodeReader _qrCodeReader = new QRCodeReader();
public QrScannerControl()
{
InitializeComponent();
VideoCapElement.EnableSampleGrabbing = true;
VideoCapElement.UseYuv = false;
VideoCapElement.NewVideoSample += (sender, args) =>
{
var binBitmap = new BinaryBitmap(new HybridBinarizer(new BitmapLuminanceSource(args.VideoFrame)));
BitMatrix bm = binBitmap.BlackMatrix;
Detector detector = new Detector(bm);
DetectorResult detectorResult = detector.detect();
var retStr = detectorResult.Points.Aggregate("Found at points ", (current, point) => current + (point.ToString() + ", "));
Console.WriteLine(retStr);
DebugLabel.Content = retStr;
var result = _qrCodeReader.decode(binBitmap);
if (result == null) return;
OnQrDeoded?.Invoke(result.Text);
};
}
}
但事件永远不会发生。