question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

nodejs not working \w sample code

See original GitHub issue

https://github.com/zxing-js/library/issues/148#issuecomment-479674044

it does not work with the latest version

i keep receiving: ‘(node:27418) UnhandledPromiseRejectionWarning: Error: SOI not found’ all time

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

const readFile = path => new Promise((resolve, reject) => fs.readFile(path, (err, content) => {
  if(err) return reject(err);
  else return resolve(content);
}))

readFile('Qr-1.png').then(jpegData => {
  const rawImageData = jpeg.decode(jpegData);


  const hints = new Map();
  const formats = [BarcodeFormat.QR_CODE, BarcodeFormat.DATA_MATRIX];

  hints.set(DecodeHintType.POSSIBLE_FORMATS, formats);
  hints.set(DecodeHintType.TRY_HARDER, true);

  const reader = new MultiFormatReader();

  reader.setHints(hints);



  const len = rawImageData.width * rawImageData.height;

  const luminancesUint8Array = new Uint8Array(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);

  console.log(luminanceSource)

  const binaryBitmap = new BinaryBitmap(new HybridBinarizer(luminanceSource));


  const decoded = reader.decode(binaryBitmap);

  console.log(decoded)
})

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
odahcamcommented, Nov 2, 2020

Well, first of all, are you using JPEG files?

0reactions
prdpjngdcommented, Mar 3, 2021

it might be. I tried lots of images. Some do work, others don’t. And I don’t see a strong relationship between image formats

  • Can use Jimp at the place of jpeg-js … this problem Might be resolved
  • Works with .jpg, .png…
const Jimp=require('Jimp')
const {BarcodeFormat,
    MultiFormatReader,
    DecodeHintType,
    RGBLuminanceSource, 
    BinaryBitmap, 
    HybridBinarizer} = require('@zxing/library')



async function decode(url) {
	return new Promise((resolve, reject) => {
		const webImgUrl = url
		Jimp.read(webImgUrl)
			.then((webImg) => {
				
					const len = webImg.bitmap.width * webImg.bitmap.height
					const luminancesUint8Array = new Uint8Array(len)
					for (let i = 0; i < len; i++) {
						luminancesUint8Array[i] = ((webImg.bitmap.data[i * 4] + webImg.bitmap.data[i * 4 + 1] * 2 + webImg.bitmap.data[
							i *
							4 + 2]) / 4) & 0xFF
					}
					const hints = new Map();
					const formats = [BarcodeFormat.QR_CODE, BarcodeFormat.DATA_MATRIX,BarcodeFormat.PDF_417,BarcodeFormat.AZTEC];

					hints.set(DecodeHintType.POSSIBLE_FORMATS, formats);

					const reader = new MultiFormatReader();

					reader.setHints(hints);

					const luminanceSource = new RGBLuminanceSource(luminancesUint8Array, webImg.bitmap.width, webImg.bitmap.height)
					const binaryBitmap = new BinaryBitmap(new HybridBinarizer(luminanceSource))
					const decoded = reader.decode(binaryBitmap)
					console.log(decoded)
					//did.push(decoded.text)
			}).catch(e => {
				console.log(e, reject)
				resolve('')
			})
	})
}
decode('5.jpg')
Read more comments on GitHub >

github_iconTop Results From Across the Web

15 Common Error Codes in Node.js and How to Fix Them
15 Common Error Codes in Node.js and How to Fix Them ... Here's an example of how the error often appears in the...
Read more >
Node.js - Basic example not working - Stack Overflow
I have been trying to figure out why i cant get even the most basic Node.js application to run, all day. I have...
Read more >
Build Node.js Apps with Visual Studio Code
The Visual Studio Code editor has great support for writing and debugging Node.js applications. This tutorial takes you from Hello World to a...
Read more >
6 reasons your Node.js apps are failing - IBM Developer
Poor performance; Crash or abort in native code ; Unexpected application behavior or functional issue. The approach you take to diagnose the problem...
Read more >
Top 10 Most Common Node.js Developer Mistakes - Toptal
js problems can have devastating effects on your program. Some may be the cause of frustration while you're trying to implement the simplest...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found