zxing-js / library

Multi-format 1D/2D barcode image processing library, usable in JavaScript ecosystem.
https://zxing-js.github.io/library/
Apache License 2.0
2.5k stars 546 forks source link

Unable to scan PDF_417 codes... #527

Open rlyle opened 2 years ago

rlyle commented 2 years ago

Hi, trying to scan the back of a drivers license, the picture is certainly clear and large enough, but it does nothing but dump this error into the logs...

index.js:1 deocdeFromVideoDevice error: ChecksumException at ChecksumException.getChecksumInstance (http://localhost:3000/static/js/vendors~main.chunk.js:91679:12) at PDF417ScanningDecoder.createDecoderResultFromAmbiguousValues (http://localhost:3000/static/js/vendors~main.chunk.js:116280:70) at PDF417ScanningDecoder.createDecoderResult (http://localhost:3000/static/js/vendors~main.chunk.js:116219:34) at PDF417ScanningDecoder.decode (http://localhost:3000/static/js/vendors~main.chunk.js:115963:34) at PDF417Reader.decode (http://localhost:3000/static/js/vendors~main.chunk.js:112829:100) at PDF417Reader.decode (http://localhost:3000/static/js/vendors~main.chunk.js:112776:31) at BrowserCodeReader.decodeBitmap (http://localhost:3000/static/js/vendors~main.chunk.js:86999:24) at BrowserCodeReader.decodeFromCanvas (http://localhost:3000/static/js/vendors~main.chunk.js:87008:17) at loop (http://localhost:3000/static/js/vendors~main.chunk.js:87775:28)

I've been trying for hours to scan a PDF_417 off the back of my license, I don't think it's a blurry image or anything like that, just simply think something is wrong with the checksum calculations for this bar code format.

Can anyone else scan a PDF_417 with this library?

rlyle commented 2 years ago

P.S. none of the sample PDF_417 in the live demo are working either..

image

rlyle commented 2 years ago

P.S. I did see this other type of error pop up as well, while trying to scan..

index.js:1 deocdeFromVideoDevice error: NotFoundException at NotFoundException.getNotFoundInstance (http://localhost:3000/static/js/vendors~main.chunk.js:93051:12) at PDF417ScanningDecoder.adjustCodewordCount (http://localhost:3000/static/js/vendors~main.chunk.js:116158:74) at PDF417ScanningDecoder.createDecoderResult (http://localhost:3000/static/js/vendors~main.chunk.js:116179:27) at PDF417ScanningDecoder.decode (http://localhost:3000/static/js/vendors~main.chunk.js:115963:34) at PDF417Reader.decode (http://localhost:3000/static/js/vendors~main.chunk.js:112829:100) at PDF417Reader.decode (http://localhost:3000/static/js/vendors~main.chunk.js:112776:31) at BrowserCodeReader.decodeBitmap (http://localhost:3000/static/js/vendors~main.chunk.js:86999:24) at BrowserCodeReader.decodeFromCanvas (http://localhost:3000/static/js/vendors~main.chunk.js:87008:17) at loop (http://localhost:3000/static/js/vendors~main.chunk.js:87775:28)

ozzmaster commented 2 years ago

I have the same issue.

I would also like to use this PDF417 scan via the camera and not just an image. Is this possible?

Thank you so much for a great library!

ozzmaster commented 2 years ago

The PDF417 bar code scanner is working, but another bar code I am trying to scan is encrypted. Is it possible to retrieve the raw data from the scan, instead of letting zxing decode the data to JSON? I have a different library that can decrypt the data.

bea314 commented 2 years ago

I have the same problem. I made it work unencrypted with this code:

const fs     = require('fs');
const jpeg    = require('jpeg-js');
const { MultiFormatReader, BarcodeFormat, RGBLuminanceSource, BinaryBitmap, HybridBinarizer, DecodeHintType, PDF417Reader } = require('@zxing/library');

async function ServiceTest(req, res) {

  var path = '{your file's path}/test01.png';

  let pathData = await fs.readFileSync(path);
  let decoded;

  let rawImageData = await jpeg.decode(pathData) ;

  const hints = new Map();
  const formats = [BarcodeFormat.PDF_417];
  hints.set(DecodeHintType.POSSIBLE_FORMATS, formats);
  hints.set(DecodeHintType.TRY_HARDER, true);
  hints.set(DecodeHintType.PURE_BARCODE, true);
  hints.set(DecodeHintType.PURE_BARCODE, true);
  const reader = new PDF417Reader(hints);

  const len = rawImageData.width * rawImageData.height

  const luminancesUint8Array = new Uint8ClampedArray(len);

  for (let i = 0; i < len; i++) {
    luminancesUint8Array[i] = ((rawImageData.data[i * 4] + rawImageData.data[i * 4 + 1] * 2 + rawImageData.data[i * 4 + 2]) / 4) & 0xff;
  }

  const luminanceSource = new RGBLuminanceSource(luminancesUint8Array, rawImageData.width, rawImageData.height);
  const binaryBitmap    = new BinaryBitmap(new HybridBinarizer(luminanceSource));

  const detection = reader.decode(binaryBitmap);

  console.log(detection.getText());
  console.log(detection.getBarcodeFormat());
  console.log(detection.putAllMetadata());
}

However, when I try to read an encrypted PDF417, I get some errors.

If i use "PDF417Reader": image

If i use "MultiFormatReader": image

Did you find a solution?

WhatTheChad commented 2 years ago

@rlyle - Were you ever able to get the back of drivers license scan working?

I have the same use case and am getting the same error. I've tried all the way up to 600dpi quality scan in both black and white and color using the ZXing.BrowserPDF417Reader or the ZXing.MultiFormatReader

drborges commented 1 year ago

I was having the same issue and this suggested workaround got me going.

rlyle commented 1 year ago

@rlyle - Were you ever able to get the back of drivers license scan working?

I have the same use case and am getting the same error. I've tried all the way up to 600dpi quality scan in both black and white and color using the ZXing.BrowserPDF417Reader or the ZXing.MultiFormatReader

Nope, we just gave up on supporting PC scanning.. which was ok for us, as our primary users are on iOS and android.

matias-facenote commented 1 year ago

For me the reason it was not working was that I was passing a UInt8ClampedArray. Apparently, it only works with Int32Array so the workaround was doing new Int32Array(clampedArray.buffer)

github-actions[bot] commented 6 months ago

Stale issue message

PaulMColeman commented 3 months ago

P.S. none of the sample PDF_417 in the live demo are working either..

image

Okay, I have been having the same issue. The Current Demo DOES work, but only on the second press. I have not found a way to make it work on a single press. First Decode gives an "N" and console errors described above. Second press DOES work correctly.

I found that if you go back to Version 0.18.6 the Demo Works correctly, and as expected for Decoding PDF417. Something happened in 0.19.0 and on, and has not worked since then, but I couldn't tell you what.

so for now, I'm going to go with 18.6 and hope for the best with my PDF417 Project.

Good Luck all.

werthdavid commented 3 months ago

Did a Git Bisect... The error comes from changes made here: https://github.com/zxing-js/library/pull/420

werthdavid commented 3 months ago

I will try to fix that without breaking the feature from https://github.com/zxing-js/library/pull/420 ...

werthdavid commented 3 months ago

The feature #420 was introduced to scan inverted codes. Now what happens is that for every scan, it is first tried by inverting the colors and the next scan it is tried with original colors. You can see that in the online example, if you press once, nothing is found, the second time it gets decoded correctly. The feature was intended for continuous video scan where there are several scans per second so it is not a problem... I will try to change the behavior so that the invert code is by default only applied in continuous scan mode. any thoughts?

werthdavid commented 3 months ago

please take a look at the PR. some feedback would be nice before merging

werthdavid commented 3 months ago

Plz test v0.21.3 guys and let me know if that helped

BryanPaoli commented 2 months ago

Hi!

I'm currently trying to decode PDF 417 with the latest version (0.21.3) and still get: Error decoding the barcode: NotFoundException2: No MultiFormat Readers were able to detect the code. When I use a random file from the internet, it does work, but with a driver's license it doesn't.

I did test the same images on https://zxing.org/w/decode.jspx and all of my images are decoded correctly.

I tested it with PDF417Reader(), which seems to detect the barcode in an image, but it only worked with one of my examples. I'm not sure if the issue is related to image quality, as the one that worked was actually one of the more blurred images.

This is what I have right now, not sure if I'm doing something wrong. I'd appreciate any help:

const handleDecode = async (e: Event) => {
    const image = document.getElementById('decode') as HTMLImageElement;
    if (!image) {
      console.error("Image element not found");
      return;
    }

    const codeReader = new BrowserMultiFormatReader();

    codeReader.hints = new Map()
        .set(DecodeHintType.TRY_HARDER, true)
        .set(DecodeHintType.PURE_BARCODE, true)
        .set(DecodeHintType.POSSIBLE_FORMATS, [BarcodeFormat.PDF_417, BarcodeFormat.UPC_E]);

    try {
      console.log("Decoding image...");
      const result = await codeReader.decodeFromImage(image);
      console.log("Decoded result outside:", result);
    } catch (error) {
      console.error("Error decoding the barcode:", error);
    }
  };