cozmo / jsQR

A pure javascript QR code reading library. This library takes in raw images and will locate, extract and parse any QR code found within.
https://cozmo.github.io/jsQR/
Apache License 2.0
3.73k stars 607 forks source link

Binarize function fails. #192

Open Chunguss opened 4 years ago

Chunguss commented 4 years ago

I am trying to use the libary function jsQR(). I checked my paramaters and can't find the issue. When i look at the error log is says the that my data size is not 4 width height, but if I calculate by hand it shouldn't fail.

Any suggestions?

cozmo commented 4 years ago

Do you have a reproduction case? The error is the very first check the code does, and it's literally data.length != width * height * 4 (https://github.com/cozmo/jsQR/blob/master/src/binarizer/index.ts#L27-L29) so...if you're calculating that and getting a different answer you and the JS runtime are doing basic math differently.

My guess is the data array you're passing in is malformed in some way, but hard to know without a reproduction case.

tommycarpi commented 3 years ago

I have a similar issue, I have a Base64 string from an Image I load from the iOS Library. I'm converting to Uint8ClampedArray in this way

let clampedArray = Uint8ClampedArray.from(Base64.atob(img.assets[0].base64!), c => c.charCodeAt(0))

but if I print the size of the resulted array it's far less than the mathematic calculation of 4 width height. width: 646 height: 508 resulted array size: 25015

What am I missing?

danimoh commented 3 years ago

@tommycarpi the base64 string you got is probably the bytes of the loaded image (e.g. a png image), not the pixel data. To get the pixel data, you have to for example draw the image on a canvas. If you are interested in a lib that uses jsQR and provides this functionality, https://github.com/nimiq/qr-scanner might be interesting for you.

tommycarpi commented 3 years ago

Thanks! I'll give it a go