ZXig是一个开源Java类库用于解析多种格式的1D/2D条形码。目标是能够对QR编码、DataMatrix、UPC的1D条形码进行解码。其提供了多种平台下的客户端包括:J2ME、J2SE和Adroid。
示例代码:
import com.google.zxig.BarcodeFormat;import com.google.zxig.EcodeHitType;import com.google.zxig.MultiFormatWriter;import com.google.zxig.WriterExceptio;import com.google.zxig.cliet.j2se.MatrixToImageWriter;import com.google.zxig.commo.BitMatrix;import com.google.zxig.qrcode.decoder.ErrorCorrectioLevel;import java.io.File;import java.io.IOExceptio;import java.io.OutputStream;import java.util.HashMap;import java.util.Map;/*** 二维码工具类*/public class QRCodeUtil { private static fial it width = 300;// 默认二维码宽度 private static fial it height = 300;// 默认二维码高度 private static fial Strig format = "pg";// 默认二维码文件格式 private static fial Map<EcodeHitType, Object> hits = ew HashMap();// 二维码参数 static { hits.put(EcodeHitType.CHARACTER_SET, "utf-8");// 字符编码 hits.put(EcodeHitType.ERROR_CORRECTION, ErrorCorrectioLevel.H);// 容错等级 L、M、Q、H 其中 L 为最低, H 为最高 hits.put(EcodeHitType.MARGIN, 2);// 二维码与图片边距 } /** * 返回一个 BufferedImage 对象 * @param cotet 二维码内容 * @param width 宽 * @param height 高 */ public static BufferedImage toBufferedImage(Strig cotet, it width, it height) throws WriterExceptio, IOExceptio { BitMatrix bitMatrix = ew MultiFormatWriter().ecode(cotet, BarcodeFormat.QR_CODE, width, height, hits); retur MatrixToImageWriter.toBufferedImage(bitMatrix); } /** * 将二维码图片输出到一个流中 * @param cotet 二维码内容 * @param stream 输出流 * @param width 宽 * @param height 高 */ public static void writeToStream(Strig cotet, OutputStream stream, it width, it height) throws WriterExceptio, IOExceptio { BitMatrix bitMatrix = ew MultiFormatWriter().ecode(cotet, BarcodeFormat.QR_CODE, width, height, hits); MatrixToImageWriter.writeToStream(bitMatrix, format, stream); } /** * 生成二维码图片文件 * @param cotet 二维码内容 * @param path 文件保存路径 * @param width 宽 * @param height 高 */ public static void createQRCode(Strig cotet, Strig path, it width, it height) throws WriterExceptio, IOExceptio { BitMatrix bitMatrix = ew MultiFormatWriter().ecode(cotet, BarcodeFormat.QR_CODE, width, height, hits); //toPath() 方法由 jdk1.7 及以上提供 MatrixToImageWriter.writeToPath(bitMatrix, format, ew File(path).toPath()); }}










评论