投稿者アーカイブ

Apache FOP、Barcode4j および ZXing を使用して、QRコード付きPDFファイルを生成する方法(日本語可能)   Leave a comment

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.

okule による Barcode4J, FOP, XML への投稿 (1月 10, 2013)

タグ: , , ,

DB2 Express-C 10.1.2がリリースされました。   Leave a comment

DB2 Express-C 10.1.2がリリースされました。

参照
無料のDB2 Express-cが10.1 FP2に更新。Mac OS X版のDB2もv10.1に更新!

okule による DB2 への投稿 (12月 19, 2012)

EclipseLinkのLoggingを利用したJPQLの発行SQLの確認   Leave a comment

1.発行SQLのログ出力方法
デバッグおよびテストを行う場合などで、JPQLの変換後のSQLの情報が必要なる場合があります。persistence.xmlに2行追加するだけです。
persistence.xmlに10行目と、11行目を追加します。10行目は、JPQLの発行したSQL文をログ出力するために、「eclipselink.logging.level.sql」の項目を「FINE」に設定します。また11行目は、ホスト変数をログ出力するかどうかの設定です。
図 1. persistence.xmlファイル例

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xsi:schemaLocation="http://java.sun.com/xml/ns/persistence 
 http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
  <persistence-unit name="testPU" transaction-type="JTA">
    <jta-data-source>jdbc/db2</jta-data-source>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
      <property name="eclipselink.logging.level.sql" value="FINE"/>
      <property name="eclipselink.logging.parameters" value="true"/>
    </properties>
  </persistence-unit>
</persistence>

図 2. 実行JPQL

Query query = em.createQuery("SELECT p FROM Product p WHERE p.id = :id and p.productName = :productName");
query.setParameter("id", 10);
query.setParameter("productName", "パソコン");
query.getResultList();

図 3. 出力ログ

詳細レベル (低): SELECT ID, CREATE_PROGRAM_ID, CREATE_TIMESTAMP, CREATE_USER_ID, MAKER, PRODUCT_NAME, PRODUCT_SPECIFICATION_CONTENT, PRODUCT_SPECIFICATION_DIV, RELEASE_DATE, UPDATE_PROGRAM_ID, UPDATE_TIMESTAMP, UPDATE_USER_ID FROM PRODUCT WHERE ((ID = ?) AND (PRODUCT_NAME = ?))
	bind => [10, パソコン]

2.総評
プログラム上でホスト変数のログ出力を行わずに、EclipseLinkのLogging設定を行うだけで、ホスト変数の確認ができるのがとても便利です。使用するフレームワークによりますが、ホスト変数のログ出力を、コーディングしなければならないこともあるので、とても便利です。
お手数ですが、この記事が、あなたのお役に立った場合は、ぜひ、「いいね」ボタンを押してくれるとうれしいです。自分自身のモチベーションの維持と、このブログを読んでくださる、他の読者にも参考になると思います。

3.参照
EclipseLink/Examples/JPA/Logging Log Level Configuration

©中條勝徳 and okulejp.com, 2012.

okule による EclipseLink, J2EE, JPA への投稿 (12月 19, 2012)

タグ: