1.説明
Apache FOP、Barcode4j および ZXing で、QRコードをPDFに出力する方法を説明します。
Barcode4jで、QRコードを生成する場合には、CVSサーバから、ソースファイルをダウンロードしてコンパイルする必要があります。注意事項として、ZXing で日本語を出力するために、Barcode4jの ECIUtil.java を修正する必要があります。修正箇所は下記に示してあります。QRコードに設定できるオプションは、Barcode4j QR Codeに説明があります。
Apache FOP と Barcode4j の連携は、バーコード付きPDFの作成のブログ記事を参考にして下さい。
CVSの設定 Barcode4J CVS
Host: barcode4j.cvs.sourceforge.net
Repository path: /cvsroot/barcode4j
User: anonymous
Password: パスワードはありません。
CVSサーバからダウンロードした、zxing-core-1.7.jarをクラスパスに追加して使用してください。
※最新バージョン2.1のZXingのライブラリィを使用する場合
Encoder クラスにおいてメソッドの引数と返却値が変わったため、Barcode4jのjavaプログラムの修正が必要です。
com.google.zxing.qrcode.encoder.Encoder
バージョン 1.7
void encode(String, ErrorCorrectionLevel, Hashtable, QRCode)
バージョン 2.1
QRCode encode(String, ErrorCorrectionLevel, Map)
※Barcode4jのjavaプログラムの修正クラス
org.krysalis.barcode4j.impl.qr.QRCodeBean
calcDimensions()
org.krysalis.barcode4j.impl.qr.QRLogicImpl
generateBarcodeLogic()
修正内容は、ソースをみればすごく簡単です。
2.出力結果
液晶ディスプレイ自体の解像度が、紙の印刷物に比較して低いため、PDF表示を150%で表示して下さい。スマートフォンにインストールされている、バーコードリーダで読み取ることが確認できると思います。
図 1. Apache FOPで出力したPDF(クリックするPDFが表示されます。)
図 2. qr_code.fo
32行目で、ZXingで使用するエンコード名「shift_jis」を明示的に指定しています。また33行目で、誤り訂正能力を「H」に指定しています。デフォルトは、「L」です。
<?xml version="1.0" encoding="UTF-8"?> <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format" language="ja"> <fo:layout-master-set> <fo:simple-page-master page-width="210mm" page-height="297mm" margin-top="0mm" margin-left="0mm" margin-right="0mm" margin-bottom="0mm" master-name="PageMaster"> <fo:region-body margin-top="15mm" margin-left="15mm" margin-right="15mm" margin-bottom="15mm" /> <fo:region-before extent="15mm" /> <fo:region-after extent="15mm" /> <fo:region-start extent="15mm" /> <fo:region-end extent="15mm" /> </fo:simple-page-master> </fo:layout-master-set> <fo:page-sequence master-reference="PageMaster"> <fo:flow flow-name="xsl-region-body"> <fo:block font-size="xx-large" font-family="MS 明朝">初めてのQRコード</fo:block> <fo:block-container absolute-position="absolute" top="15mm" left="0mm" width="125mm" height="117.3mm"> <fo:block font-family="MS 明朝">QRコード内容は、「http://okulejp.com」です。</fo:block> <fo:block> <fo:instream-foreign-object> <barcode xmlns="http://barcode4j.krysalis.org/ns" message="http://okulejp.com"> <qr/> </barcode> </fo:instream-foreign-object> </fo:block> </fo:block-container> <fo:block-container absolute-position="absolute" top="35mm" left="0mm" width="125mm" height="117.3mm"> <fo:block font-family="MS 明朝">QRコード内容は、「日本語テスト」です。</fo:block> <fo:block> <fo:instream-foreign-object> <barcode xmlns="http://barcode4j.krysalis.org/ns" message="日本語テスト"> <qr> <encoding>shift_jis</encoding> <ec-level>H</ec-level> </qr> </barcode> </fo:instream-foreign-object> </fo:block> </fo:block-container> <fo:block-container absolute-position="absolute" top="270mm" left="0mm" width="125mm" height="117.3mm"> <fo:block font-size="small" font-family="MS 明朝">*QRコード® は、株式会社デンソーウェーブの登録商標です。</fo:block> </fo:block-container> </fo:flow> </fo:page-sequence> </fo:root>
図 3. ECIUtil.javaの修正例。
53行目で、ZXingで使用するエンコード名「shift_jis」を使用できるように追加します。
/* * Copyright 2012 Jeremias Maerki, Switzerland * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /* $Id: ECIUtil.java,v 1.1 2012/01/27 14:36:35 jmaerki Exp $ */ package org.krysalis.barcode4j.tools; import java.util.Map; /** * Utilities around ECI (extended channel interpretation). * * @version $Id: ECIUtil.java,v 1.1 2012/01/27 14:36:35 jmaerki Exp $ */ public class ECIUtil { /** ECI code for 8-bit binary data */ public static final int ECI_BINARY = 899; private static final Map encodingToECI = new java.util.HashMap(); static { mapEncoding("Cp437", 2); mapEncoding("ISO-8859-1", 3); mapEncoding("ISO-8859-2", 4); mapEncoding("ISO-8859-3", 5); mapEncoding("ISO-8859-4", 6); mapEncoding("ISO-8859-5", 7); mapEncoding("ISO-8859-6", 8); mapEncoding("ISO-8859-7", 9); mapEncoding("ISO-8859-8", 10); mapEncoding("ISO-8859-9", 11); mapEncoding("ISO-8859-10", 12); mapEncoding("ISO-8859-11", 13); mapEncoding("ISO-8859-13", 15); mapEncoding("ISO-8859-14", 16); mapEncoding("ISO-8859-15", 17); mapEncoding("ISO-8859-16", 18); mapEncoding("SJIS", 20); mapEncoding("shift_jis", 30); mapEncoding("Cp1250", 21); mapEncoding("Cp1251", 22); mapEncoding("Cp1252", 23); mapEncoding("Cp1256", 24); mapEncoding("UnicodeBigUnmarked", 25); mapEncoding("UTF-8", 26); mapEncoding("US-ASCII", 27); mapEncoding("Big5", 28); mapEncoding("GB18030", 29); } private static void mapEncoding(String encoding, int eci) { encodingToECI.put(encoding, new Integer(eci)); } /** * Returns the ECI code for a given encoding. * @param encoding the encoding * @return the corresponding ECI code */ public static int getECIForEncoding(String encoding) { Integer code = (Integer)encodingToECI.get(encoding); if (code != null) { return code.intValue(); } else { return -1; //undefined } } }
3.システムの応用例
QRコードには、全角文字が、最大1,817文字を保存できます。スキャナーで、CSV形式のデータを格納したQRコードの内容を読み込んで、入力画面に自動的に読み込むことができます。そうすることで、入力する際の間違えをなくしたり、入力する手間を省いたりすることができます。また暗号データを格納したQRコードを使用して、会員カードによるログイン認証にも応用できます。もちろん商品などの在庫管理にも応用できます。
4.参考及び注意事項
QRコードドットコム-QRコードの特長のご紹介。
攻略! ツール・ド・プログラミング34回 ZXingとは
QRコード® は、株式会社デンソーウェーブの登録商標です。
©中條勝徳 and okulejp.com, 2012.