serratus / quaggaJS

An advanced barcode-scanner written in JavaScript
https://serratus.github.io/quaggaJS/
MIT License
5.03k stars 977 forks source link

Using Node returns codeResult of undefined #395

Open joseph-animasds opened 4 years ago

joseph-animasds commented 4 years ago

My code: ` const express = require('express') const app = express() const port = 3000 const sharp = require('sharp'); const Quagga = require('quagga').default

//pdftoppm test.pdf test -png -f 1 -singlefile -rx 300 -ry 300

app.get('/', (req, res) => { let originalImage = 'pdf/test.png';

// file name for cropped image
let outputImage = 'croppedImage/croppedImage.png';

sharp(originalImage).extract({ width: 1114, height: 105, left: 789, top: 3189 }).toFile(outputImage)
    .then(new_file_info => {
        console.log("Image cropped and saved", new_file_info);
        Quagga.decodeSingle({
            decoder: {
                readers: ["code_128_reader"] // List of active readers
            },
            locate: true,
            src: "croppedImage/croppedImage.png",
            numOfWorkers: 0,  // Needs to be 0 when used within node
            inputStream: {
                size: 800  // restrict input-size to be 800px in width (long-side)
            },

        }, function (result) {
            if (result.codeResult) {
                console.log("result", result.codeResult.code);
            } else {
                console.log("not detected");
            }
        });
    }).catch(err => console.log(err, "something failed"))

})

app.listen(port, () => console.log(listening on ${port})) ` and the error: image

ericblade commented 4 years ago

Hi! Could you supply the source images? Also, could you check it against https://github.com/ericblade/quagga2 and if it's still broken in that version, file there?

Thanks!

joseph-animasds commented 4 years ago

croppedImage

joseph-animasds commented 4 years ago

I also tried with quagga2 and with the same image I get same error.

ericblade commented 4 years ago

so is that the pdf input, or the input to sharp, or the input to quagga? just to clarify. and that is a code 128? i don't instantly recognize barcode types :-) Do you know what that should translate to when successfully read?

ericblade commented 4 years ago

ok, so... i'm not real sure exactly how to "fix" this.. as a person, i can clearly see the edges of the barcode.. i've had mixed success however feeding that file to other barcode scanner softwares.

What I have had success doing, however, is in bumping the file size up -- i took it into gimp, and pasted it, centered, into a new 1200x200 file -- and then quagga is able to read it using an image width of 1600 (i suspect it might need more white space padding than it has, but i have not yet tried putting in more in the source image)

I'm using the following settings on the example page https://serratus.github.io/quaggaJS/examples/file_input.html

image

As is, your inputStream.size parameter is too small -- you may also be able to solve this by resizing the image down to 800 or fewer pixels width . . . but i suspect you'll also still need to add whitespace around it.

ericblade commented 4 years ago

fwiw, setting the tester to 1280px with the 1200x200 image almost works, the tester gives a green box around it, seeming to indicate that it has found something it thinks should be a barcode, but not a good result.

i was also able to add this image to the test suite and get it to run from node, using the same settings, however increasing the inputstream size to 1600 in node currently seems to break several of the other code 128 test images.

joseph-animasds commented 4 years ago

thanks @ericblade