iTextとzxingによるpdf 417 QRコードの生成と読み取り
前のいくつかのブログでは、zxingというオープンソースツールがQRコードピクチャを生成して読み取ることについて言及していますが、学習の観点から見ると、段落を閉じることができます.実際の生産環境では、zxingを適用してQRコードを生成および読み取りますが、いくつかの問題があります.スキャンガンを使用してzxingで生成された中国語付きQRコード画像を読み取る場合、中国語の文字化けし問題がある. zxingで生成されたQRコードピクチャは、記憶情報量が大きくなるにつれてピクチャも大きくなる(この点、iTextというツールで生成されたQRコードピクチャの長さと幅が相対的に固定され、iTextを選択してQRコードを生成する要因でもある).
これらの問題を解決するために、振り回された後、iTextツールを使用してQRコードピクチャを生成する代替解決策が見つかった.iTextはクラス読取QRコードの内容を提供していないため、ここではzxingを使用し、特に中国語の乱コードの問題に注意した.コードは次のとおりです.
キークラス:BufferedImage ByteArrayOutputStream
問題の1つ:
iTextで生成された画像も情報量が大きくなるにつれて大きくなり、画像のスケーリング技術を用いて画像のフォーマットを固定することを解決する.
これらの問題を解決するために、振り回された後、iTextツールを使用してQRコードピクチャを生成する代替解決策が見つかった.iTextはクラス読取QRコードの内容を提供していないため、ここではzxingを使用し、特に中国語の乱コードの問題に注意した.コードは次のとおりです.
package test;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import javax.imageio.ImageIO;
import org.apache.commons.lang.StringUtils;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;
import com.itextpdf.text.pdf.BarcodePDF417;
/**
* : 1、 2、 3、
*/
public class TwoDimBarCodeService {
/**
* :
* @param strInfo
* @param encode , :GBK,UTF-8
* @param imgFileExt
* @return byte[] byte
* @throws Exception
*/
public byte[] generatePdf417Image(String strInfo, String encode,
String imgFileExt) throws Exception {
if (StringUtils.isBlank(strInfo)) {
throw new Exception(" !");
}
if (StringUtils.isBlank(encode)) {
encode = "UTF-8";
}
BarcodePDF417 barcodePDF417 = new BarcodePDF417();
barcodePDF417.setText(strInfo.getBytes(encode));
Image pdfImg = barcodePDF417.createAwtImage(Color.black, Color.white);
BufferedImage img = new BufferedImage((int) pdfImg.getWidth(null),
(int) pdfImg.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics g = img.getGraphics();
g.drawImage(pdfImg, 0, 0, Color.white, null);
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(img, imgFileExt, os);
byte[] buffs = os.toByteArray();
os.close();
return buffs;
}
/**
* : byte
* @param imgBuff byte[]
* @param encode , :GBK,UTF-8
* @return
* @throws Exception
*/
public String readInfoFromPdf417Image(byte[] imgBuff, String encode)
throws Exception {
if (imgBuff == null || imgBuff.length < 1) {
throw new Exception(" !");
}
InputStream is = new ByteArrayInputStream(imgBuff);
BufferedImage image = ImageIO.read(is);
LuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Result decodedValue = new MultiFormatReader().decode(bitmap);
if (decodedValue == null) {
return "";
}
String resultText = decodedValue.getText();
resultText = StringUtils.trimToEmpty(resultText);
byte[] b = resultText.getBytes("ISO-8859-1");
return new String(b, encode);
}
public static void main(String[] args) throws Exception {
TwoDimBarCodeService service = new TwoDimBarCodeService();
String strInfo = "GB0626-2005^JH-201403050005^XX ^ ^[2014]0005^^ ^ 5^ ^20140305^^^20140305^^";
byte[] buff = service.generatePdf417Image(strInfo, "GBK", "jpg");
OutputStream os = new FileOutputStream(new File("d:/test.jpg"));
os.write(buff);
os.flush();
os.close();
System.err.println(" :"
+ service.readInfoFromPdf417Image(buff, "GBK"));
}
}
キークラス:BufferedImage ByteArrayOutputStream
問題の1つ:
iTextで生成された画像も情報量が大きくなるにつれて大きくなり、画像のスケーリング技術を用いて画像のフォーマットを固定することを解決する.