ZXing.NET解码HTML5视频中的PDF417条形码



我正在尝试将jQuery/JavaScript与ZXing.NET一起使用,以解码来自视频源的PDF417条形码。

这是我的HTML:

<video id="video" width="800" height="800"></video>
<canvas id="canvas" width="800" height="800"></canvas>

还有摄像头的jQuery和调用.NET方法调试条形码的代码:

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方法。

这是我调试PDF417条形码的.NET方法:

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选项:

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];
}

所以我开始认为是相机的分辨率导致我的条形码无法扫描。。。。另一方面,当我将条形码格式更改为QR_CODE并扫描二维码时,它可以工作,但PDF417条形码不可以。我做错了什么?

我有一些例子,比如这个问题,在明显良好的图像重建情况下,zxing无法按预期解码,我无法找出原因。

尝试放置PureBarcode = true将解决问题。

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];
}

最新更新