<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>forever 5 years old blog &#187; barcode</title>
	<atom:link href="http://www.f5yo.net/archives/tag/barcode/feed" rel="self" type="application/rss+xml" />
	<link>http://www.f5yo.net</link>
	<description>今年も 5 歳になりました</description>
	<lastBuildDate>Wed, 29 Apr 2009 01:32:39 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>ja</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>libzebra を使って Objective-C でバーコードを読み取る</title>
		<link>http://www.f5yo.net/archives/143</link>
		<comments>http://www.f5yo.net/archives/143#comments</comments>
		<pubDate>Fri, 20 Mar 2009 04:29:05 +0000</pubDate>
		<dc:creator>ihara</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[barcode]]></category>
		<category><![CDATA[Objective-C]]></category>

		<guid isPermaLink="false">http://www.f5yo.net/?p=143</guid>
		<description><![CDATA[Obejctvie-C ほとんど関係ありませんが。
iPhone のアプリケーションでバーコードを読み取りたいなーと思って調べていると、Zebra っちゅうのがあったので（もうすぐ名前が変わるみたい）、これ使って実現で [...]]]></description>
			<content:encoded><![CDATA[<p>Obejctvie-C ほとんど関係ありませんが。</p>
<p>iPhone のアプリケーションでバーコードを読み取りたいなーと思って調べていると、<a href="http://zebra.sourceforge.net/">Zebra</a> っちゅうのがあったので（もうすぐ名前が変わるみたい）、これ使って実現できないかやってみました。</p>

MacPorts には zebra が用意されていないようなので、自分で<a href="http://sourceforge.net/project/showfiles.php?group_id=189236">ダウンロード</a>してきてコンパイルします。

<pre language="txt">
[Masahiro@masahiro.local:~/Documents] $ tar xzvf zebra-0.6.tar.gz
[Masahiro@masahiro.local:~/Documents] $ ./zebra-0.6
[Masahiro@masahiro.local:~/Documents/zebra-0.6] $ ./configure
...
configure: error: test for video support failed!
rebuild your kernel to include video4linux support or
configure --disable-video to skip building video support.
See `config.log' for more details.
</pre>

<p>なんつって怒られてしまいました。video4linux を無効にして再チャレンジ。</p>


<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">./configure --disable-video
...
No package 'ImageMagick++' found</pre></div></div>




<p>ImageMagick 入ってないと。MacPorts で ImageMagick インストールして再々チャレンジ。</p>


<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">./configure --disable-video
...
No package 'gtk+-2.0' found</pre></div></div>




<p>きりがないわ、っちゅうことで ./configure -h でオプション見て必要なさそうなものを外します。</p>


<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">./configure --prefix=/Users/Masahiro/tmp/libzebra --disable-video --without-gtk --without-qt --without-xv --without-xshm --without-x --without-python
...
please verify that the detected configuration matches your expectations:
------------------------------------------------------------------------
X                 --with-x=no
pthreads          --enable-pthread=yes
v4l               --enable-video=no
        =&gt; zebracam video scanner will *NOT* be built
jpeg              --with-jpeg=no
        =&gt; JPEG image conversions will *NOT* be supported
Magick++          --with-imagemagick=yes
Python            --with-python=no
GTK+              --with-gtk=no
        =&gt; the GTK+ widget will *NOT* be built
Qt4               --with-qt=no
        =&gt; the Qt4 widget will *NOT* be built</pre></div></div>




<p>自分のホームの tmp/libzebra に入れておくことにして、make &#038; make install でインストール。</p>

<p>で、次は Objective-C でこのライブラリを使ってバーコードを読み取るコードを書いてみます。テストで使うバーコードは<a href="http://apps.sourceforge.net/mediawiki/zebra/index.php?title=Troubleshooting#Size.2FResolution">ここ</a>から持ってきました。</p>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &lt;Foundation/Foundation.h&gt;</span>
<span style="color: #6e371a;">#import &lt;zebra.h&gt;</span>
&nbsp;
<span style="color: #6e371a;">#define fourcc(a, b, c, d) \</span>
    <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>uint32_t<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#40;</span>a<span style="color: #002200;">&#41;</span> | <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>uint32_t<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#40;</span>b<span style="color: #002200;">&#41;</span> &lt;&lt; <span style="color: #2400d9;">8</span><span style="color: #002200;">&#41;</span> | \
    <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>uint32_t<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#40;</span>c<span style="color: #002200;">&#41;</span> &lt;&lt; <span style="color: #2400d9;">16</span><span style="color: #002200;">&#41;</span> | <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>uint32_t<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#40;</span>d<span style="color: #002200;">&#41;</span> &lt;&lt; <span style="color: #2400d9;">24</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>
&nbsp;
<span style="color: #a61390;">void</span> dataHandler<span style="color: #002200;">&#40;</span>zebra_image_t<span style="color: #002200;">*</span> zimage, <span style="color: #a61390;">const</span> <span style="color: #a61390;">void</span><span style="color: #002200;">*</span> userdata<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
    NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;succeeded to scan barcode&quot;</span><span style="color: #002200;">&#41;</span>;
    <span style="color: #a61390;">const</span> zebra_symbol_t<span style="color: #002200;">*</span> symbol <span style="color: #002200;">=</span> zebra_image_first_symbol<span style="color: #002200;">&#40;</span>zimage<span style="color: #002200;">&#41;</span>;
    zebra_symbol_type_t type <span style="color: #002200;">=</span> zebra_symbol_get_type<span style="color: #002200;">&#40;</span>symbol<span style="color: #002200;">&#41;</span>;
    <span style="color: #a61390;">const</span> <span style="color: #a61390;">char</span><span style="color: #002200;">*</span> data <span style="color: #002200;">=</span> zebra_symbol_get_data<span style="color: #002200;">&#40;</span>symbol<span style="color: #002200;">&#41;</span>;
    NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;type: %d, data: %s&quot;</span>, type, data<span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">void</span> cleanupHandler<span style="color: #002200;">&#40;</span>zebra_image_t<span style="color: #002200;">*</span> zimage<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
    NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;cleanup called&quot;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
CGImageRef createCGImageFromData<span style="color: #002200;">&#40;</span><span style="color: #400080;">NSData</span><span style="color: #002200;">*</span> data<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
    CGImageRef imageRef <span style="color: #002200;">=</span> <span style="color: #a61390;">NULL</span>;
    CGImageSourceRef sourceRef <span style="color: #002200;">=</span> CGImageSourceCreateWithData<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>CFDataRef<span style="color: #002200;">&#41;</span>data, <span style="color: #a61390;">NULL</span><span style="color: #002200;">&#41;</span>;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>sourceRef<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        imageRef <span style="color: #002200;">=</span> CGImageSourceCreateImageAtIndex<span style="color: #002200;">&#40;</span>sourceRef, <span style="color: #2400d9;">0</span>, <span style="color: #a61390;">NULL</span><span style="color: #002200;">&#41;</span>;
        CFRelease<span style="color: #002200;">&#40;</span>sourceRef<span style="color: #002200;">&#41;</span>;
    <span style="color: #002200;">&#125;</span>
    <span style="color: #a61390;">return</span> imageRef;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">int</span> main<span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> argc, <span style="color: #a61390;">const</span> <span style="color: #a61390;">char</span> <span style="color: #002200;">*</span> argv<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
    <span style="color: #400080;">NSAutoreleasePool</span> <span style="color: #002200;">*</span> pool <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSAutoreleasePool</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">//</span>
    <span style="color: #11740a; font-style: italic;">// ファイルから画像を読み込みビットマップに変換する</span>
    <span style="color: #11740a; font-style: italic;">//</span>
    <span style="color: #400080;">NSData</span><span style="color: #002200;">*</span> data <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSData</span> alloc<span style="color: #002200;">&#93;</span> initWithContentsOfFile<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;/Users/Masahiro/Desktop/Zbar.114.png&quot;</span><span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
    CGImageRef image <span style="color: #002200;">=</span> createCGImageFromData<span style="color: #002200;">&#40;</span>data<span style="color: #002200;">&#41;</span>;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span>image<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;faild to execute createCGImageFromData&quot;</span><span style="color: #002200;">&#41;</span>;
        <span style="color: #a61390;">exit</span><span style="color: #002200;">&#40;</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#41;</span>;
    <span style="color: #002200;">&#125;</span>
    <span style="color: #a61390;">int</span> width <span style="color: #002200;">=</span> CGImageGetWidth<span style="color: #002200;">&#40;</span>image<span style="color: #002200;">&#41;</span>;
    <span style="color: #a61390;">int</span> height <span style="color: #002200;">=</span> CGImageGetHeight<span style="color: #002200;">&#40;</span>image<span style="color: #002200;">&#41;</span>;
    CGRect rect <span style="color: #002200;">=</span> CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">0</span>, width, height<span style="color: #002200;">&#41;</span>;
    <span style="color: #a61390;">int</span> bitsPerComponent <span style="color: #002200;">=</span> <span style="color: #2400d9;">8</span>;
    <span style="color: #a61390;">int</span> bytesPerRow <span style="color: #002200;">=</span> width;
    <span style="color: #a61390;">int</span> bitmapByteCount <span style="color: #002200;">=</span> bytesPerRow <span style="color: #002200;">*</span> height;
    <span style="color: #a61390;">void</span><span style="color: #002200;">*</span> bitmap <span style="color: #002200;">=</span> <span style="color: #a61390;">malloc</span><span style="color: #002200;">&#40;</span>bitmapByteCount<span style="color: #002200;">&#41;</span>;
    CGColorSpaceRef colorspace <span style="color: #002200;">=</span> CGColorSpaceCreateDeviceGray<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
    CGContextRef c <span style="color: #002200;">=</span> CGBitmapContextCreate<span style="color: #002200;">&#40;</span>bitmap,
                                           width,
                                           height,
                                           bitsPerComponent,
                                           bytesPerRow,
                                           colorspace,
                                           kCGImageAlphaNone<span style="color: #002200;">&#41;</span>;
    CGColorSpaceRelease<span style="color: #002200;">&#40;</span>colorspace<span style="color: #002200;">&#41;</span>;
    CGContextDrawImage<span style="color: #002200;">&#40;</span>c, rect, image<span style="color: #002200;">&#41;</span>;
    CGContextRelease<span style="color: #002200;">&#40;</span>c<span style="color: #002200;">&#41;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">//</span>
    <span style="color: #11740a; font-style: italic;">// バーコードを読み取る</span>
    <span style="color: #11740a; font-style: italic;">//</span>
    zebra_image_scanner_t<span style="color: #002200;">*</span> scanner <span style="color: #002200;">=</span> zebra_image_scanner_create<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
    zebra_image_scanner_set_data_handler<span style="color: #002200;">&#40;</span>scanner, dataHandler, <span style="color: #a61390;">NULL</span><span style="color: #002200;">&#41;</span>;
    zebra_image_t<span style="color: #002200;">*</span> zimage <span style="color: #002200;">=</span> zebra_image_create<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
    zebra_image_set_format<span style="color: #002200;">&#40;</span>zimage, fourcc<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">'Y'</span>, <span style="color: #bf1d1a;">'8'</span>, <span style="color: #bf1d1a;">'0'</span>, <span style="color: #bf1d1a;">'0'</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
    zebra_image_set_size<span style="color: #002200;">&#40;</span>zimage, width, height<span style="color: #002200;">&#41;</span>;
    zebra_image_set_data<span style="color: #002200;">&#40;</span>zimage, bitmap, bitmapByteCount, cleanupHandler<span style="color: #002200;">&#41;</span>;
    zebra_scan_image<span style="color: #002200;">&#40;</span>scanner, zimage<span style="color: #002200;">&#41;</span>;
    zebra_image_destroy<span style="color: #002200;">&#40;</span>zimage<span style="color: #002200;">&#41;</span>;
    zebra_image_scanner_destroy<span style="color: #002200;">&#40;</span>scanner<span style="color: #002200;">&#41;</span>;
&nbsp;
    <span style="color: #002200;">&#91;</span>pool drain<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">return</span> <span style="color: #2400d9;">0</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>




<p>エラー処理丁寧に入れていませんが、こんな感じ。Xcode でビルドするときに気をつけないといけないのは、ヘッダのパス（/Users/Masahiro/tmp/libzebra/include）を追加することと、ライブラリを追加すること（今回はスタティックライブラリを使うことにして /Users/Masahiro/tmp/libzebra/lib/libzebra.a）。Xcode でリンクするライブラリの追加はファイルをドラッグ＆ドロップなんですね。それくらいっすかね。ああ、あとプロジェクトをテスト用に Foundation Tool で作っていまして、CGImage を扱うために ApplicationServices フレームワークを追加してあげないといけませんでした。普通に Cocoa Application だったりすると必要ないのかもしれません。</p>

<p>で、実行してみると、</p>


<div class="wp_syntax"><div class="code"><pre class="txt" style="font-family:monospace;">2009-03-20 13:18:51.084 ObjC Test[98413:10b] succeeded to scan barcode
2009-03-20 13:18:51.086 ObjC Test[98413:10b] type: 13, data: 9876543210128
2009-03-20 13:18:51.087 ObjC Test[98413:10b] cleanup called</pre></div></div>




<p>こんな感じで、タイプが 13（zebra.h の zebra_symbol_type_e で ZEBRA_EAN13）、バーコードが 9876543210128 と読み取れております。素晴らしい。</p>]]></content:encoded>
			<wfw:commentRss>http://www.f5yo.net/archives/143/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
