library icon indicating copy to clipboard operation
library copied to clipboard

How does it work in nodejs? The code for demo has been invalid

Open greenking19 opened this issue 1 year ago • 4 comments
trafficstars

How does it work in nodejs? The code for de mo has been invalid I run in Nodejs after prompting "NotFoundException: No MultiFormat Readers were able to detect the code." What do I need to do? 🤯 Are there any other demo codes? 🤔️

/**
 * zxing-js reader🧐
 * @param {string} filePath file path 📃
 * @returns {string | null}
 */
export const zxingJsReader = async (filePath) => {
  const hints = new Map();
  const formats = [BarcodeFormat.QR_CODE, BarcodeFormat.DATA_MATRIX /*, ...*/];

  hints.set(DecodeHintType.POSSIBLE_FORMATS, formats);

  const reader = new MultiFormatReader();
  let fileBuffer = fs.readFileSync(filePath);
  //   const byteArray = new ByteArray(fileBuffer);
  const byteArray = new Uint8Array(fileBuffer);

  const luminanceSource = new RGBLuminanceSource(byteArray, 400, 400);
  const binaryBitmap = new BinaryBitmap(new HybridBinarizer(luminanceSource));

  try {
    const result = reader.decode(binaryBitmap, hints);
    return result;
  } catch (error) {
    console.log(error);
    return null;
  }
};

greenking19 avatar May 21 '24 08:05 greenking19

whats the error when you run it in node?

werthdavid avatar May 22 '24 13:05 werthdavid

Same issue here, I believe this library is just out-of-date. Unfortunate since there isn't many alternatives. :-(

brianwalczak avatar Jun 05 '24 20:06 brianwalczak

It is in maintenance mode. If I could find anybody to take over I'd be glad

werthdavid avatar Jun 05 '24 20:06 werthdavid

I think this code cannot work. As far as I understand, with the following line const byteArray = new Uint8Array(fileBuffer); you pack the whole file's contents into a byte array. However, this doesn't abstract away the file encoding (jpg, png, whatever). The RGBLuminanceSource expects the pure rgb information of the image without encoding specifics. One way to solve this is go via the file browser code readers, however that only works if you're not using nodejs server side without any dom tree. Otherwise, I think, you'd have to use some library (e.g. opencv) to get to the pixel info independent of the file encoding (i.e., opencv abstracts away the file encoding specifics and easily lets you access the pixel bytes, you can then pack those into a Uint8Array). Is that correct @werthdavid

auldo avatar Sep 13 '24 10:09 auldo