C#でバーコードのデコードを行う方法。
WindowsでWebカメラを使用して、QRコードやバーコードの認識を行う方法をメモ。
C#対応のライブラリはいくつかあるが、比較的使いやすい物は「ZXing」と思われる。
Webカメラから取り込まれたものはPictureBox1に描画されているものとする。
公式サイトはこちら。
C#のコードはSVNにて配布されている。
Command-line access
Use this command to anonymously check out the latest project source code:
# Non-members may check out a read-only working copy anonymously over HTTP.
svn checkout http://zxing.googlecode.com/svn/trunk/ zxing-read-only
ソースファイルフォルダ「csharp」をすべてダウンロード。
Microsoft Visual C# 2010 Expressでプロジェクト変換ウィザードで、コンバート。
リビルドしDLLを作成する。
using com.google.zxing; //リーダ初期化 Reader reader = new com.google.zxing.MultiFormatReader(); //結果の入れ物 com.google.zxing.Result result = null; //PictureBoxのimageから持ってきている。 Bitmap bmp = new Bitmap(pictureBox1.Image); //リーダ用画像変換 LuminanceSource source = new RGBLuminanceSource(bmp, bmp.Width, bmp.Height); BinaryBitmap binaryBitmap = new BinaryBitmap(new com.google.zxing.common.GlobalHistogramBinarizer(source)); // デコードを実行 result = reader.decode(binaryBitmap); string OutputText = result.Text; //JANやQRのデコードしたものが入る。
手元にあるWebカメラが悪いのか、認識が悪い。
"RGBLuminanceSource"あたり無理矢理過ぎるのかな。