Skip to content

Commit

Permalink
Better output, builds on OSX.
Browse files Browse the repository at this point in the history
  • Loading branch information
Vishnu Gopal committed May 22, 2009
1 parent bf0fc04 commit 2258ad9
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 43 deletions.
35 changes: 21 additions & 14 deletions README
Original file line number Original file line Diff line number Diff line change
@@ -1,45 +1,51 @@


= barcodescanner = barcodescanner



== Description == 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 == Features/Problems


=== Require Libraries === 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. and make, make install.


Omit --prefix=/opt/local if you're building on non-OSX/Macports

== Synopsis == Synopsis


require 'rubygems' require 'rubygems'
require 'barcodescanner' require 'barcodescanner'


# take from test # take from test
expected = '9784087204254' expected =
result = BarcodeScanner.process_image_file("#{expected}.jpg") [{:type=>"EAN-13", :data=>"1920222007009"},
assert_equal(expected, result) {: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 " === "cannot open shared object file: No such file or directory "


Expand All @@ -49,5 +55,6 @@ and make, make install.
== Copyright == Copyright


Author:: koyachi <rtk2106@gmail.com> Author:: koyachi <rtk2106@gmail.com>
Modified by:: Vishnu Gopal <g.vishnu@gmail.com>
Copyright:: Copyright (c) 2008 koyachi Copyright:: Copyright (c) 2008 koyachi
License:: License::
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ spec = Gem::Specification.new do |s|
s.rubyforge_project = RUBYFORGE_PROJECT s.rubyforge_project = RUBYFORGE_PROJECT
s.bindir = "bin" s.bindir = "bin"
s.require_paths = %w[lib ext] s.require_paths = %w[lib ext]
s.requirements << 'libmagick, libzebra' s.requirements << 'libmagick, libzbar'
s.extensions << 'ext/extconf.rb' s.extensions << 'ext/extconf.rb'
#s.autorequire = "" #s.autorequire = ""
s.test_files = Dir["test/*_test.rb"] s.test_files = Dir["test/*_test.rb"]
Expand Down
72 changes: 52 additions & 20 deletions ext/Init_barcodescanner.cpp
Original file line number Original file line Diff line number Diff line change
@@ -1,13 +1,13 @@
// BarcodeScanner // BarcodeScanner
// zebra binding for ruby // zbar binding for ruby
// //
// 2008-11-10 t.koyachi // 2008-11-10 t.koyachi


#include "ruby.h" #include "ruby.h"
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <Magick++.h> #include <Magick++.h>
#include <zebra.h> #include <zbar.h>


typedef VALUE RubyType(...); typedef VALUE RubyType(...);


Expand All @@ -17,36 +17,68 @@ VALUE
brcdscnr_s_process_image_file(VALUE self, VALUE filename) brcdscnr_s_process_image_file(VALUE self, VALUE filename)
{ {
const char* fn = StringValuePtr(filename); const char* fn = StringValuePtr(filename);
// そのうちclass変数にしたい
zebra::Processor* processor = new zebra::Processor(false, NULL, false); // create a reader
// zebra::Processor* processor = new zebra::Processor(false); zbar::ImageScanner scanner;

// configure the reader
scanner.set_config(zbar::ZBAR_NONE, zbar::ZBAR_CFG_ENABLE, 1);


Magick::Image image; // read image file
image.read(fn); Magick::Image image(fn);
image.modifyImage();

// convert to gray
Magick::Blob scan_data; image.modifyImage();
Magick::Blob scan_data;
image.write(&scan_data, "GRAY", 8); image.write(&scan_data, "GRAY", 8);
unsigned width = image.columns();
// obtain image data
unsigned width = image.columns();
unsigned height = image.rows(); unsigned height = image.rows();

// image error
if (scan_data.length() != width * height) { if (scan_data.length() != width * height) {
rb_raise(rb_eException, "scan_data.length(%d) != width(%d) * height(%d)", rb_raise(rb_eException, "scan_data.length(%d) != width(%d) * height(%d)",
scan_data.length(), width, height); scan_data.length(), width, height);
} }


zebra::Image zimage(width, height, "Y800", // wrap image data
scan_data.data(), scan_data.length()); const void *raw = scan_data.data();
processor->process_image(zimage);
zbar::Image zimage(width, height, "Y800",
raw, scan_data.length());

// scan the image for barcodes
int n = scanner.scan(zimage);


std::string result; // support multiple values
for (zebra::Image::SymbolIterator sym = zimage.symbol_begin(); VALUE return_array = rb_ary_new();

std::string type, data;
for (zbar::Image::SymbolIterator sym = zimage.symbol_begin();
sym != zimage.symbol_end(); sym != zimage.symbol_end();
++sym) ++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 #ifdef __cplusplus
Expand Down
12 changes: 7 additions & 5 deletions ext/extconf.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,21 +1,23 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
require 'mkmf' require 'mkmf'


%w[Magick++ Magick zebra].each do |header| %w[Magick++ zbar].each do |header|
dir_config(header) dir_config(header)
end 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 = 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 $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 = [] depends = []
%w[stdc++ Magick++ Magick zebra].each do |lib| %w[stdc++ Magick++ zbar].each do |lib|
depends << have_library(lib) depends << have_library(lib)
end end


%w[string zebra].each do |header| %w[string zbar].each do |header|
depends << have_header("#{header}.h") depends << have_header("#{header}.h")
end end
if depends.all? then if depends.all? then
Expand Down
Binary file removed test/9784087204254.jpg
Binary file not shown.
6 changes: 4 additions & 2 deletions test/barcodescanner_test.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ def teardown
end end


def test_1 def test_1
expected = '9784087204254' expected =
result = BarcodeScanner.process_image_file("#{expected}.jpg") [{: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) assert_equal(expected, result)
end end
end end
Binary file added test/test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion test/test_helper.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,2 +1,3 @@
require 'test/unit' require 'test/unit'
require File.dirname(__FILE__) + '/../lib/barcodescanner' require 'rubygems'
require 'barcodescanner'

0 comments on commit 2258ad9

Please sign in to comment.