ZXing.NET从HTML5视频中解码PDF417条码

时间:2019-11-19 20:37:20

标签: javascript c# jquery html zxing

我正在尝试通过ZXing.NET使用jQuery / JavaScript来解码视频源中的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方法。

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

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

所以我开始认为这是导致我的条形码无法扫描的摄像机的分辨率。...另一方面,当我将BarcodeFormat更改为QR_CODE并扫描QR码时,它可以工作,但不适用于PDF417条形码。我在做什么错了?

1 个答案:

答案 0 :(得分:1)

我遇到了一些类似该问题的情况,在这种情况下,由于图像重建效果良好,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];
}