EddieLa / JOB

A Barcode scanner capapable of reading Code128, Code93, Code39, Standard/Industrial 2 of 5, Interleaved 2 of 5, Codabar and EAN-13 barcodes in javascript.
http://eddiela.github.io/JOB
600 stars 204 forks source link

Image always rescaled? #29

Open Kujako opened 9 years ago

Kujako commented 9 years ago

Somewhere in the deep dark depths of the code any image processed is being rescaled to 320x240. This is causing moire patterns in some barcodes rendering them un-decodable. Is there a way to disable this, or is it there for some specific reason?

EddieLa commented 9 years ago

The reason for the scaling is simply that it in most cases it's a good compromise between speed and accuracy. It would however be reasonably easy to implement some way to scale the image down to an appropriate size but maintaining a correct height/width ratio.

I'll try to fix it when I some time available.

Kujako commented 9 years ago

That makes sense. I think it's a landscape vs portrait issue when taking the photo on a mobile device.

Kujako commented 9 years ago

Part of the issue I think is that photos taken with iOS devices are coming back with sceneCaptureType as undefined. I've kludged around this with the following code added to JOBDecodeImage

if((sceneCaptureType===undefined) && (image.width > image.height && (orientation == 8 || orientation == 6))) { JOB.ScanCanvas.width = 640; JOB.ScanCanvas.height = 480; }

EwaRvr commented 9 years ago

While I am not sure how sensitive the algorithm is, instead of hardcoding both width and height, maybe only set the largest size, and then calculate the appropriate 2nd size to avoid some more moire patterns and skewing, while extending Kujako's solution for still unfound scenarios.

e.g. something like (updated JOBDecodeImage including Kujako's addition):

if(orientation == 8 || orientation == 6) {
    if(sceneCaptureType == "Landscape"  && image.width > image.height) {
        orientation = 1;
        JOB.ScanCanvas.width = 640;
        JOB.ScanCanvas.height = image.height/(image.width / 640);
    } else {
        JOB.ScanCanvas.width = image.width / (image.height / 640);
        JOB.ScanCanvas.height = 640;
    }
} else {
    JOB.ScanCanvas.width = 640;
    JOB.ScanCanvas.height = image.height / (image.width / 640);
}
Kujako commented 9 years ago

The problem is twofold, first the sceneCaptureType value is undefined under iOS (no idea why). Second, the image dimensions do not evenly divide by 640. But that is more or less the route I'm taking. Also increasing the resolution to improve accuracy and auto-cropping to the middle 40% of the image to improve performance. Many things....

EddieLa commented 9 years ago

Though auto-cropping to the middle 40% makes the assumption that the barcode is located in the middle which I'm not really a fan of but whatever works.

seangates commented 9 years ago

@Kujako @EwaRvr I'm sure that @EddieLa would allow you to fork/fix then create a pull request with your updates. He just pulled in mine. :+1:

I'm liking this project more and more as I work with it. :smile: