ZXing.NET从HTML5视频解码PDF417条码 [英] ZXing.NET decode PDF417 Barcode from HTML5 Video
问题描述
我正在尝试将jQuery/JavaScript与ZXing.NET结合使用,以解码视频源中的PDF417条码.
I am trying to use jQuery/JavaScript with ZXing.NET to decode a PDF417 barcode from a video source.
这是我的HTML:
<video id="video" width="800" height="800"></video>
<canvas id="canvas" width="800" height="800"></canvas>
还有相机的jQuery和调用.NET方法调试条形码的代码:
And the jQuery for the camera and the code that calls an .NET method to debug the barcode:
var video = document.getElementById('video');
if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
const hdConstraints = {
video: { width: { min: 1280 }, height: { min: 720 } }
};
navigator.mediaDevices.getUserMedia({ video: true }).then(function (stream) {
video.srcObject = stream;
video.play();
});
}
$("#video").on("playing", function () {
setInterval(function () { scanBarcode() }, 500);
});
function scanBarcode() {
var video = document.getElementById('video');
var canvas = document.getElementById('canvas');
var canvas_context = canvas.getContext('2d');
canvas_context.drawImage(video, 0, 0, 640, 480);
var image = document.getElementById("canvas").toDataURL("image/png");
image = image.replace('data:image/png;base64,', '');
$.post("Home/OnScan", { imageData: image }, function (data, status) {
console.log(data);
});
}
如您所见,我正在获取画布的图像并将其传递给我的.NET方法.
As you can see there I am getting the image of the canvas and passing it to my .NET method.
这是我的.NET方法来调试PDF417条码:
And here is my .NET method to debug the PDF417 barcode:
public JsonResult OnScan(string imageData)
{
BitmapImage bitmapImage = new BitmapImage();
byte[] byteBuffer = Convert.FromBase64String(imageData);
Bitmap bmp;
using (var ms = new MemoryStream(byteBuffer))
{
bmp = new Bitmap(ms);
}
BarcodeReader reader = new BarcodeReader();
DecodingOptions options = new DecodingOptions
{
TryHarder = true,
PossibleFormats = new List<BarcodeFormat> { BarcodeFormat.PDF_417 }
};
reader.Options = options;
var result = reader.Decode(bmp);
return Json(result.Text, JsonRequestBehavior.AllowGet);
}
现在这仍然行不通,但是我记得当我第一次在Xamarin中这样做时,在添加CameraResolutionSelector选项之前,它也行不通了:
Now this still does not work, but I remembered when I first did this in Xamarin.Forms it also did not work until I add the CameraResolutionSelector option:
var options = new MobileBarcodeScanningOptions
{
TryHarder = true,
CameraResolutionSelector = HandleCameraResolutionSelectorDelegate,
PossibleFormats = new List<BarcodeFormat> { BarcodeFormat.PDF_417 },
};
这是HandleCameraResolutionSelectorDelegate
方法:
public CameraResolution HandleCameraResolutionSelectorDelegate(List<CameraResolution> availableResolutions)
{
//Don't know if this will ever be null or empty
if (availableResolutions == null || availableResolutions.Count < 1)
return new CameraResolution() { Width = 800, Height = 600 };
//Debugging revealed that the last element in the list
//expresses the highest resolution. This could probably be more thorough.
return availableResolutions[availableResolutions.Count - 1];
}
所以我开始认为这是导致我的条形码无法扫描的相机分辨率..另一方面,当我将BarcodeFormat更改为QR_CODE并扫描QR代码时,它可以工作,但不适用于PDF417条形码.我在做什么错了?
So I am starting to think it the resolution of the camera that is causing my barcode not to scan....on another note when I change BarcodeFormat to QR_CODE and scan a QR code it works, but not with a PDF417 Barcode. What am I doing wrong?
推荐答案
我遇到了一些类似该问题的实例,在这些实例中,由于图像重建效果良好,zxing无法按预期解码,因此我无法胜任找出原因.
I have some instances like the one of this issue, where with an apparently good image reconstruction, zxing can't decode as expected and i'm not competent to figure out why.
尝试放入PureBarcode = true
将解决此问题.
Try putting PureBarcode = true
will resolve the issue.
DecodingOptions options = new DecodingOptions
{
TryHarder = true,
PossibleFormats = new List<BarcodeFormat> { BarcodeFormat.PDF_417 },
PureBarcode = true,
AutoRotate = true,
TryInverted = true,
CameraResolutionSelector = HandleCameraResolutionSelectorDelegate
};
CameraResolution HandleCameraResolutionSelectorDelegate(List<CameraResolution> availableResolutions)
{
if (availableResolutions == null || availableResolutions.Count < 1)
return new CameraResolution () { Width = 800, Height = 600 };
return availableResolutions [availableResolutions.Count - 1];
}
这篇关于ZXing.NET从HTML5视频解码PDF417条码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!