willglynn / ruby-zbar

Ruby bindings for the ZBar barcode recognition library
MIT License
83 stars 24 forks source link

JPEG datastream contains no image #2

Closed karlkoch23 closed 11 years ago

karlkoch23 commented 13 years ago

zbar is bulid with ImageMagick but method _fromjpeg fails with "JPEG datastream contains no image". The standalone application zbarimg processes jpeg images without problems. The gem works with PGM images.

willglynn commented 13 years ago

1 was a similar issue. Are you using Ruby 1.9, and if so, are you reading the image in binary mode?

JPEG works for me, so I need more information about your environment, a snippet of code demonstrating the issue, and/or a sample image to help identify the problem.

willglynn commented 13 years ago

Whoops, didn't mean to close. That's what I get for replying on my phone :-)

karlkoch23 commented 13 years ago

I use rvm and tried it with ruby 1.8.7 and 1.9.2. The binary mode option is only avaliable for windows system (as far as I know). I use OSX 10.6.8.

The code is taken from the example:

ZBar::Image.from_jpeg(File.read('/Users/mamu/Desktop/Produkte/IMG_0305.JPG')).process

sberryman commented 12 years ago

I have the same problem. Were you able to fix it?

sberryman commented 12 years ago

I ended up just using RMagick directly to read the image, convert it to PGM and then using Image.from_pgm method of zbar to process the image.

require 'zbar'
require 'RMagick'

# load the image via rmagick
input = Magick::Image.read('test.jpg').first

# convert to PGM
input.format = 'PGM'

# load the image from a string
image = ZBar::Image.from_pgm(input.to_blob)

image.process.each do |result|
  puts "Code: #{result.data} - Type: #{result.symbology} - Quality: #{result.quality}"
end
lawso017 commented 11 years ago

I ran into this exact problem as well after installing newer builds of ghostscript/imagemagick.

At the moment I'm thinking the Zbar binary may be compiled against an older version of necessary jpg libraries, as jpgs being generated by ghostscript from a pdf are now failing with the above error message:

JPEG datastream contains no image ArgumentError: conversion failed

They are valid jpgs as far as I can see, however. My first attempt at compiling zbar from the current source didn't fix the issue. Am going to poke around some more.

willglynn commented 11 years ago

Could you upload a sample image?

lawso017 commented 11 years ago

Will do... so a fresh compile on the box (either from the ZBar 0.1.0 tarball or the tip) did not fix the problem.

Looking into the dependencies, both Ghostscript and ZBar are referencing the same libjpeg8 library (I'm running Ubuntu 12.04 LTS).

Now I'm wondering if there might be a problem with ruby-zbar, since the file IS recognized if I simply use zbarimg (the included utility program) from the command line:

zbarimg upload-page-000.jpg
CODE-128:inv-1350130212610132-1
scanned 1 barcode symbols from 1 images in 1.8 seconds

Here is the image:

upload-page-000

lawso017 commented 11 years ago

Here is the same file generated on my dev box (OSX) using ImageMagick 6.7.5-7 and Ghostscript 9.05 via:

convert -density 400 input_file.pdf output_file.jpg

osx-upload-page-000

This .jpg is recognized just fine:

irb(main):032:0> osx=File.open('/Users/williamlawson/Desktop/osx-upload-page-000.jpg', 'rb')
=> File:/Users/williamlawson/Desktop/osx-upload-page-000.jpg
irb(main):033:0> z=ZBar::Image.from_jpeg(osx).process
=> ZBar::Symbol:0x007f80519fcd50 @symbology="CODE-128", @data="inv-1350130212610132-1", @addon="", @quality=54

But the one generated on the Ubuntu box using ImageMagick 6.6.9-7 and Ghostscript 9.05 (pasted above) is not:

irb(main):034:0> u12=File.open('/Users/williamlawson/u12-upload-page-000.jpg', 'rb')
=> File:/Users/williamlawson/u12-upload-page-000.jpg
irb(main):035:0> z=ZBar::Image.from_jpeg(u12).process
JPEG datastream contains no image
ArgumentError: conversion failed
    from /Users/williamlawson/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/zbar-0.1.1/lib/zbar/image.rb:86:in `convert'
    from /Users/williamlawson/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/zbar-0.1.1/lib/zbar/image.rb:26:in `from_jpeg'
    from (irb):35

I am not a jpg expert -- but it would appear there is something different between these two files. Will keep digging on this end...

lawso017 commented 11 years ago

This is interesting -- I've forked the project and am attempting to run tests:

  2) Error:
test: ZBar should read a barcode from a JPEG blob. (TestZBar):
ArgumentError: conversion failed
    /Volumes/develop/ruby-zbar/lib/zbar/image.rb:86:in `convert'
    /Volumes/develop/ruby-zbar/lib/zbar/image.rb:26:in `from_jpeg'
    /Volumes/develop/ruby-zbar/test/test_zbar.rb:32:in `block in <class:TestZBar>'
    /Users/williamlawson/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/thoughtbot-shoulda-2.11.1/lib/shoulda/context.rb:382:in `call'
    /Users/williamlawson/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/thoughtbot-shoulda-2.11.1/lib/shoulda/context.rb:382:in `block in create_test_from_should_hash'

  3) Error:
test: ZBar should read a barcode from a JPEG file. (TestZBar):
ArgumentError: conversion failed
    /Volumes/develop/ruby-zbar/lib/zbar/image.rb:86:in `convert'
    /Volumes/develop/ruby-zbar/lib/zbar/image.rb:26:in `from_jpeg'
    /Volumes/develop/ruby-zbar/test/test_zbar.rb:38:in `block (2 levels) in <class:TestZBar>'
    /Volumes/develop/ruby-zbar/test/test_zbar.rb:37:in `open'
    /Volumes/develop/ruby-zbar/test/test_zbar.rb:37:in `block in <class:TestZBar>'
    /Users/williamlawson/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/thoughtbot-shoulda-2.11.1/lib/shoulda/context.rb:382:in `call'
    /Users/williamlawson/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/thoughtbot-shoulda-2.11.1/lib/shoulda/context.rb:382:in `block in create_test_from_should_hash'

... all of my tests fail, but the JPG-related tests fail for the same reasons. The other tests give a different error message:

  1) Error:
test: ZBar should be able to re-use a processor. (TestZBar):
ArgumentError: invalid byte sequence in UTF-8
    /Volumes/develop/ruby-zbar/lib/zbar/image.rb:43:in `gsub'
    /Volumes/develop/ruby-zbar/lib/zbar/image.rb:43:in `from_pgm'
    /Volumes/develop/ruby-zbar/test/test_zbar.rb:24:in `block in <class:TestZBar>'
    /Users/williamlawson/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/thoughtbot-shoulda-2.11.1/lib/shoulda/context.rb:382:in `call'
    /Users/williamlawson/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/thoughtbot-shoulda-2.11.1/lib/shoulda/context.rb:382:in `block in create_test_from_should_hash'

###

  4) Error:
test: ZBar should read a barcode from a PGM file. (TestZBar):
ArgumentError: invalid byte sequence in UTF-8
    /Volumes/develop/ruby-zbar/lib/zbar/image.rb:43:in `gsub'
    /Volumes/develop/ruby-zbar/lib/zbar/image.rb:43:in `from_pgm'
    /Volumes/develop/ruby-zbar/test/test_zbar.rb:15:in `block (2 levels) in <class:TestZBar>'
    /Volumes/develop/ruby-zbar/test/test_zbar.rb:14:in `open'
    /Volumes/develop/ruby-zbar/test/test_zbar.rb:14:in `block in <class:TestZBar>'
    /Users/williamlawson/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/thoughtbot-shoulda-2.11.1/lib/shoulda/context.rb:382:in `call'
    /Users/williamlawson/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/thoughtbot-shoulda-2.11.1/lib/shoulda/context.rb:382:in `block in create_test_from_should_hash'

  5) Error:
test: ZBar should read the right barcode from a PGM blob. (TestZBar):
ArgumentError: invalid byte sequence in UTF-8
    /Volumes/develop/ruby-zbar/lib/zbar/image.rb:43:in `gsub'
    /Volumes/develop/ruby-zbar/lib/zbar/image.rb:43:in `from_pgm'
    /Volumes/develop/ruby-zbar/test/test_zbar.rb:7:in `block in <class:TestZBar>'
    /Users/williamlawson/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/thoughtbot-shoulda-2.11.1/lib/shoulda/context.rb:382:in `call'
    /Users/williamlawson/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/thoughtbot-shoulda-2.11.1/lib/shoulda/context.rb:382:in `block in create_test_from_should_hash'

5 tests, 0 assertions, 0 failures, 5 errors, 0 skips

This is the first time I've tried running tests, but something is not right in my environment.

willglynn commented 11 years ago

Those look like Ruby 1.9-related test suite failures, which wasn't really a factor last time I touched this code. I pushed some changes which make those issues go away on my machine.

Incidentally, "JPEG datastream contains no image" comes from jpeg_read_header() failing with JERR_NO_IMAGE. Not sure how that's getting printed, but it's definitely libjpeg output.

willglynn commented 11 years ago

Ah: I bet this is really this issue, where zbar's skip_input_data() function doesn't actually work. We should see if we can get a new upstream release with that patch applied.

lawso017 commented 11 years ago

You're right about the 1.9 test changes -- adding 'rb' mode selectors fixed the PGM tests. Good find on the zbar issue. Will see if I can figure out how to apply that patch.

lawso017 commented 11 years ago

Nice. I've verified that the patch fixed the issue in my Ubuntu environment as well. Thanks for your help.

willglynn commented 11 years ago

I haven't heard back on getting a new upstream release of zbar, but I did succeed in getting this patch into Homebrew. I'll leave this issue open just in case anyone else encounters this problem (until zbar 0.11 comes out), but I consider it solved.

jeremysenn commented 11 years ago

How do we go about getting this patch? I'm having the same issue on ubuntu.

Thanks!

willglynn commented 11 years ago

It's linked above, but I pulled it into a gist for easier application. Might be worth filing a Debian/Ubuntu bug since it doesn't seem like we'll get a new upstream release any time soon.

jeremysenn commented 11 years ago

Thanks for the response. However, I am uncertain how to go about applying that gist into the zbar ruby gem code. Where exactly is that patch code supposed to go?

Thanks for your help!

willglynn commented 11 years ago

The bug is in the zbar C library, not the zbar Ruby gem. You'll need to reinstall the libzbar0 package from source and apply the patch there. This guide outlines one method for applying patches in Ubuntu.

willglynn commented 11 years ago

Submitted as Ubuntu #1185157.

jeremysenn commented 11 years ago

Ok - thanks for the help!

On Tue, May 28, 2013 at 1:07 PM, Will Glynn notifications@github.comwrote:

Submitted as Ubuntu #1185157https://bugs.launchpad.net/ubuntu/+source/zbar/+bug/1185157 .

— Reply to this email directly or view it on GitHubhttps://github.com/willglynn/ruby-zbar/issues/2#issuecomment-18576379 .

willglynn commented 11 years ago

zbar-0.2.0 has been released. If your libzbar has this problem, you'll see the following on stderr the first time you call ZBar::Image.from_jpeg:

JPEG datastream contains no image
Your libzbar has a JPEG bug. Some images will fail to process correctly.
See: https://github.com/willglynn/ruby-zbar/blob/master/lib/zbar/jpeg.rb

This warning is purely informational; the gem will still happily pass your JPEG data to the library and see what happens. It might work, or it might not.

There's nothing more I can do here. Closing.

anaumov commented 5 years ago

updated link to the post "Build Ubuntu/Debian packages from source (and apply a patch)" http://pascal.bach.ch/2010/05/06/build-ubuntudebian-packages-from-source-and-apply-a-patch/

anaumov commented 5 years ago

inspired by @sberryman MiniMagick solution

  image = MiniMagick::Image.open(path)
  image.format('pgm')
  ZBar::Image.from_pgm(image.to_blob).process