我正在尝试使用 WPF-MediaKitVideoCaptureElement
预览网络摄像头视频,并在帧中解码 QR 码,因此我需要访问网络摄像头记录的每一帧,并且根据文档,它需要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);
};
}
}
但该事件永远不会触发。