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

Not sure how to scan a code 128 barcode #561

Closed 1PV1 closed 1 year ago

1PV1 commented 1 year ago

I'm using canvas to crop the image so it has only the barcode, I tryed with the code from https://github.com/zxing-js/library#readme but I'm getting NotFoundException: No MultiFormat Readers were able to detect the code.

Code:

const cropedcanvas = createCanvas(384, 99);
const cropedctx = cropedcanvas.getContext('2d');
cropedctx.drawImage(canvas, 0, 312, 384, 99, 0, 0, 384, 99);
const hints = new Map();
hints.set(DecodeHintType.POSSIBLE_FORMATS, [BarcodeFormat.CODE_128]);
const reader = new MultiFormatReader();
const luminanceSource = new RGBLuminanceSource(new Uint8ClampedArray(cropedcanvas.toBuffer()), cropedcanvas.width, cropedcanvas.height);
const binaryBitmap = new BinaryBitmap(new HybridBinarizer(luminanceSource));
const code = reader.decode(binaryBitmap, hints);
console.log(code)

Image: image