Permalink
Browse files

Better output, builds on OSX.

  • Loading branch information...
1 parent bf0fc04 commit 2258ad91e04e51a3d8afa20dc0d451f12ac4fc53 @vishnugopal vishnugopal committed May 22, 2009
View
35 README
@@ -1,45 +1,51 @@
= barcodescanner
-
== Description
-Retrievee barcode value from barcode image file
+Retrieve barcode value from barcode image file
-== Installation
+== New in this version
-=== Archive Installation
+* Build cleanly on OSX/Macports
+* Returns a key-value hash rather than concatenated output
- rake install
+== Installation
-=== Gem Installation
+=== Archive Installation
- gem install barcodescanner
+Checkout code from the repository first, and then:
+ rake install
+ rake test
== Features/Problems
=== Require Libraries
-ImageMagick, zebra(http://zebra.sourceforge.net/)
+ImageMagick, zbar(http://zbar.sourceforge.net/)
-==== install zebra
+==== install zbar
-if you install zebra to non-x machine,
+if you install zbar to non-x machine,
- ./configure --without-gtk --withouut-qt
+ ./configure --prefix=/opt/local --disable-video --without-gtk --without-qt
and make, make install.
+Omit --prefix=/opt/local if you're building on non-OSX/Macports
+
== Synopsis
require 'rubygems'
require 'barcodescanner'
# take from test
- expected = '9784087204254'
- result = BarcodeScanner.process_image_file("#{expected}.jpg")
- assert_equal(expected, result)
+ expected =
+ [{:type=>"EAN-13", :data=>"1920222007009"},
+ {:type=>"ISBN-10", :data=>"4087204251"}]
+ result = BarcodeScanner.process_image_file("test.png")
+ assert_equal(expected, result)
=== "cannot open shared object file: No such file or directory "
@@ -49,5 +55,6 @@ and make, make install.
== Copyright
Author:: koyachi <rtk2106@gmail.com>
+Modified by:: Vishnu Gopal <g.vishnu@gmail.com>
Copyright:: Copyright (c) 2008 koyachi
License::
View
@@ -56,7 +56,7 @@ spec = Gem::Specification.new do |s|
s.rubyforge_project = RUBYFORGE_PROJECT
s.bindir = "bin"
s.require_paths = %w[lib ext]
- s.requirements << 'libmagick, libzebra'
+ s.requirements << 'libmagick, libzbar'
s.extensions << 'ext/extconf.rb'
#s.autorequire = ""
s.test_files = Dir["test/*_test.rb"]
@@ -1,13 +1,13 @@
// BarcodeScanner
-// zebra binding for ruby
+// zbar binding for ruby
//
// 2008-11-10 t.koyachi
#include "ruby.h"
#include <stdio.h>
#include <string.h>
#include <Magick++.h>
-#include <zebra.h>
+#include <zbar.h>
typedef VALUE RubyType(...);
@@ -17,36 +17,68 @@ VALUE
brcdscnr_s_process_image_file(VALUE self, VALUE filename)
{
const char* fn = StringValuePtr(filename);
- // そのうちclass変数にしたい
- zebra::Processor* processor = new zebra::Processor(false, NULL, false);
-// zebra::Processor* processor = new zebra::Processor(false);
+
+ // create a reader
+ zbar::ImageScanner scanner;
+
+ // configure the reader
+ scanner.set_config(zbar::ZBAR_NONE, zbar::ZBAR_CFG_ENABLE, 1);
- Magick::Image image;
- image.read(fn);
- image.modifyImage();
-
- Magick::Blob scan_data;
+ // read image file
+ Magick::Image image(fn);
+
+ // convert to gray
+ image.modifyImage();
+ Magick::Blob scan_data;
image.write(&scan_data, "GRAY", 8);
- unsigned width = image.columns();
+
+ // obtain image data
+ unsigned width = image.columns();
unsigned height = image.rows();
+
+ // image error
if (scan_data.length() != width * height) {
rb_raise(rb_eException, "scan_data.length(%d) != width(%d) * height(%d)",
scan_data.length(), width, height);
}
- zebra::Image zimage(width, height, "Y800",
- scan_data.data(), scan_data.length());
- processor->process_image(zimage);
+ // wrap image data
+ const void *raw = scan_data.data();
+
+ zbar::Image zimage(width, height, "Y800",
+ raw, scan_data.length());
+
+ // scan the image for barcodes
+ int n = scanner.scan(zimage);
- std::string result;
- for (zebra::Image::SymbolIterator sym = zimage.symbol_begin();
+ // support multiple values
+ VALUE return_array = rb_ary_new();
+
+ std::string type, data;
+ for (zbar::Image::SymbolIterator sym = zimage.symbol_begin();
sym != zimage.symbol_end();
++sym)
- {
- result += sym->get_data();
+ {
+ type = sym->get_type_name();
+ data = sym->get_data();
+
+ VALUE hash_for_type_and_data = rb_hash_new();
+ rb_hash_aset(hash_for_type_and_data,
+ ID2SYM(rb_intern("type")), rb_str_new2(type.c_str()));
+
+ rb_hash_aset(hash_for_type_and_data,
+ ID2SYM(rb_intern("data")), rb_str_new2(data.c_str()));
+
+ rb_ary_push(return_array, hash_for_type_and_data);
}
- delete processor;
- return rb_str_new2(result.c_str());
+
+ // clean up
+ zimage.set_data(NULL, 0);
+
+ // return data
+ //return rb_str_new2(result.c_str());
+
+ return return_array;
}
#ifdef __cplusplus
View
@@ -1,21 +1,23 @@
# -*- coding: utf-8 -*-
require 'mkmf'
-%w[Magick++ Magick zebra].each do |header|
+%w[Magick++ zbar].each do |header|
dir_config(header)
end
-$CFLAGS = ENV['CFLAGS'].to_s + ' ' + `Magick-config --cflags`.chomp
+$CFLAGS = ENV['CFLAGS'].to_s + ' ' + "-I/opt/local/include -I/usr/local/include"
$CPPFLAGS = ENV['CPPFLAGS'].to_s + ' ' + `Magick-config --cppflags`.chomp
+$CPPFLAGS = $CPPFLAGS + ' ' + "-I/opt/local/include -I/usr/local/include"
$LDFLAGS = ENV['LDFLAGS'].to_s + ' ' + `Magick-config --ldflags`.chomp
-$LOCAL_LIBS = ENV['LIBS'].to_s + ' ' + `Magick-config --libs`.chomp
+$LDFLAGS = $LDFLAGS + ' ' + "-L/opt/local/lib -L/usr/local/lib"
+$LOCAL_LIBS = ENV['LIBS'].to_s + ' ' + `Magick-config --libs`.chomp
depends = []
-%w[stdc++ Magick++ Magick zebra].each do |lib|
+%w[stdc++ Magick++ zbar].each do |lib|
depends << have_library(lib)
end
-%w[string zebra].each do |header|
+%w[string zbar].each do |header|
depends << have_header("#{header}.h")
end
if depends.all? then
View
Deleted file not rendered
@@ -9,8 +9,10 @@ def teardown
end
def test_1
- expected = '9784087204254'
- result = BarcodeScanner.process_image_file("#{expected}.jpg")
+ expected =
+ [{:type=>"EAN-13", :data=>"1920222007009"},
+ {:type=>"ISBN-10", :data=>"4087204251"}]
+ result = BarcodeScanner.process_image_file("#{File.expand_path(File.dirname(__FILE__))}/test.png")
assert_equal(expected, result)
end
end
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
@@ -1,2 +1,3 @@
require 'test/unit'
-require File.dirname(__FILE__) + '/../lib/barcodescanner'
+require 'rubygems'
+require 'barcodescanner'

0 comments on commit 2258ad9

Please sign in to comment.