ruby-zbar
ruby-zbar copied to clipboard
Cannot process jpeg image
I'm using the latest zbar from mercurial on sf.net. I can process a jpeg file using 'zbarimg bigstock-Barcode-18830351.jpg', but the following does not work:
require 'zbar'
jpeg = File.binread('bigstock-Barcode-18830351.jpg')
z = ZBar::Image.from_jpeg(jpeg)
r = z.process
z
is #<ZBar::Image:0x00000001c55778 @img=#<FFI::AutoPointer address=0x000000021b8710>>
, r
is []
, and no errors are generated.
I'm using http://www.geeknewscentral.com/wp-content/uploads/2012/12/bigstock-Barcode-18830351.jpg as the test image.
Following these same steps, I get:
> r = z.process
=> [#<ZBar::Symbol:0x007fd63a869c60 @symbology="EAN-13", @data="9788679912077", @addon="", @quality=835, @location=[[819, 28], [463, 29], [130, 29], ...]>]
Are you sure the zbar
gem is loading the right libzbar.so
? Try setting the ZBAR_LIB
environment variable to its full path.
I'm sure it is loading the right libzbar:
irb(main):007:0> ZBar.ffi_libraries.map{|x|x.instance_variable_get(:@name)}
=> ["/usr/lib64/libzbar.so.0.2.0", "/usr/lib64/libzbar.so.0"]
[user@ip-10-0-0-119 ~]$ ldd `which zbarimg` | grep zbar
libzbar.so.0 => /usr/lib64/libzbar.so.0 (0x00007f625b520000)
If I run convert bigstock-Barcode-18830351.jpg bigstock-Barcode-18830351.pgm
and then use ZBar::Image.from_pgm
, it processes the pgm image and decodes the barcode fine. So there is only a problem with jpegs.
Almost a year later, I have this same problem. In my case, I'm on CentOS, and rebuilt the rpm from source, applying the patch provided here.
However, running the image directly through the zbarimg
binary DOES work.
@pzb Did you ever resolve this, or just end up converting to PGM?
It's may be a JPEG quality issue.
I got a .png QR Code image cropped from a scanner generated image, and zbarimg can read it. While when I use imagemagick convert it to .jpg, then zbar failed to read it. Convert it to .pgm, zbar reads it.
Finally, I convert it to .jpg via command: convert crop.png -quality 90 crop.jpg, then zbar can read the jpg file now.
bogon: ~/Downloads/VouchPic ]
stone$ zbarimg crop2.png
QR-Code:01,01,3400182130,00993210,1635.85,20180814,,4A08,
scanned 1 barcode symbols from 1 images in 0.01 seconds
bogon: ~/Downloads/VouchPic ]
stone$ convert crop2.png crop2.jpg
bogon: ~/Downloads/VouchPic ]
stone$ ruby -r zbar -e "p ZBar::Image.from_jpeg(File.binread('crop2.jpg')).process"
[]
bogon: ~/Downloads/VouchPic ]
stone$ convert crop2.png -quality 90 crop2.jpg
bogon: ~/Downloads/VouchPic ]
stone$ ruby -r zbar -e "p ZBar::Image.from_jpeg(File.binread('crop2.jpg')).process"
[#<ZBar::Symbol:0x00007ff6ce11cb10 @symbology="QR-Code", @data="01,01,3400182130,00993210,1635.85,20180814,,4A08,", @addon="", @quality=1, @location=[[20, 11], [17, 200], [211, 203], [212, 13]]>]
bogon: ~/Downloads/VouchPic ]
stone$