这篇内容分享了JAVA实现二维码生成的实例代码,对此有需要的朋友们可以测试参考下。
package common; import java.awt.Color; import java.awt.Graphics2D; import java.awt.Image; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.HashMap; import java.util.Map; import javax.imageio.ImageIO; import jp.sourceforge.qrcode.QRCodeDecoder; import jp.sourceforge.qrcode.exception.DecodingFailedException; import com.google.zxing.BarcodeFormat; import com.google.zxing.Binarizer; import com.google.zxing.BinaryBitmap; import com.google.zxing.EncodeHintType; import com.google.zxing.luminanceSource; import com.google.zxing.MultiFormatReader; import com.google.zxing.MultiFormatWriter; import com.google.zxing.NotFoundException; import com.google.zxing.WriterException; import com.google.zxing.common.BitMatrix; import com.google.zxing.common.HybridBinarizer; import com.swetake.util.Qrcode; /** * 二维码生成工具类 * @author Cloud * @data 2016-12-15 * QRCode */ public class QRCodeUtil { //二维码颜色 private static final int BLACK = 0xFF000000; //二维码颜色 private static final int WHITE = 0xFFFFFFFF; /** * ZXing 方式生成二维码 * @param text 二维码内容 * @param width 二维码宽 * @param height 二维码高 * @param outPutPath 二维码生成保存路径 * @param imageType 二维码生成格式 */ public static void zxingCodeCreate(String text, int width, int height, String outPutPath, String imageType){ Map his = new HashMap(); //设置编码字符集 his.put(EncodeHintType.CHaraCTER_SET, "utf-8"); try { //1、生成二维码 BitMatrix encode = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, his); //2、获取二维码宽高 int codeWidth = encode.getWidth(); int codeHeight = encode.getHeight(); //3、将二维码放入缓冲流 BufferedImage image = new BufferedImage(codeWidth, codeHeight, BufferedImage.TYPE_INT_RGB); for (int i = 0; i 二维码解析 * @param analyzePath 二维码路径 * @return * @throws IOException */ @SuppressWarnings({ "rawtypes", "unchecked" }) public static Object zxingCodeAnalyze(String analyzePath) throws Exception{ MultiFormatReader formatReader = new MultiFormatReader(); Object result = null; try { File file = new File(analyzePath); if (!file.exists()) { return "二维码不存在"; } BufferedImage image = ImageIO.read(file); luminanceSource source = new luminanceSourceUtil(image); Binarizer binarizer = new HybridBinarizer(source); BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer); Map hints = new HashMap(); hints.put(EncodeHintType.CHaraCTER_SET, "UTF-8"); result = formatReader.decode(binaryBitmap, hints); } catch (NotFoundException e) { e.printstacktrace(); } return result; } /** * QRCode 方式生成二维码 * @param content 二维码内容 * @param imgPath 二维码生成路径 * @param version 二维码版本 * @param isFlag 是否生成logo图片 为NULL不生成 */ public static void QRCodeCreate(String content, String imgPath, int version, String logoPath){ try { Qrcode qrcodeHandler = new Qrcode(); //设置二维码排错率,可选L(7%) M(15%) Q(25%) H(30%),排错率越高可存储的信息越少,但对二维码清晰度的要求越小 qrcodeHandler.setQrcodeErrorCorrect('M'); //N代表数字,A代表字符a-Z,B代表其他字符 qrcodeHandler.setQrcodeEncodemode('B'); //版本1为21*21矩阵,版本每增1,二维码的两个边长都增4;所以版本7为45*45的矩阵;最高版本为是40,是177*177的矩阵 qrcodeHandler.setQrcodeVersion(version); //根据版本计算尺寸 int imgSize = 67 + 12 * (version - 1) ; byte[] contentBytes = content.getBytes("gb2312"); BufferedImage bufImg = new BufferedImage(imgSize , imgSize ,BufferedImage.TYPE_INT_RGB); Graphics2D gs = bufImg.createGraphics(); gs.setBackground(Color.WHITE); gs.clearRect(0, 0, imgSize , imgSize); // 设定图像颜色 > BLACK gs.setColor(Color.BLACK); // 设置偏移量 不设置可能导致解析出错 int pixoff = 2; // 输出内容 > 二维码 if (contentBytes.length > 0 && contentBytes.length QRCode二维码解析 * @param codePath 二维码路径 * @return 解析结果 */ public static String QRCodeAnalyze(String codePath) { File imageFile = new File(codePath); BufferedImage bufImg = null; String decodedData = null; try { if(!imageFile.exists()) return "二维码不存在"; bufImg = ImageIO.read(imageFile); QRCodeDecoder decoder = new QRCodeDecoder(); decodedData = new String(decoder.decode(new ImageUtil(bufImg)), "gb2312"); } catch (IOException e) { System.out.println("Error: " + e.getMessage()); e.printstacktrace(); } catch (DecodingFailedException dfe) { System.out.println("Error: " + dfe.getMessage()); dfe.printstacktrace(); } return decodedData; } } 3、最后贴测试代码: package test; import java.awt.image.BufferedImage; import java.io.InputStream; import java.net.URL; import javax.imageio.ImageIO; import common.ImageUtil; import common.QRCodeUtil; import jp.sourceforge.qrcode.QRCodeDecoder; /** * 二维码生成测试类 * @author Cloud * @data 2016-11-21 * QRCodeTest */ public class QRCodeTest { public static void main(String[] args) throws Exception { /** * QRcode 二维码生成测试 * QRCodeUtil.QRCodeCreate("http://blog.csdn.net/u014266877", "E://qrcode.jpg", 15, "E://icon.png"); */ /** * QRcode 二维码解析测试 * String qrcodeAnalyze = QRCodeUtil.QRCodeAnalyze("E://qrcode.jpg"); */ /** * ZXingCode 二维码生成测试 * QRCodeUtil.zxingCodeCreate("http://blog.csdn.net/u014266877", 300, 300, "E://zxingcode.jpg", "jpg"); */ /** * ZxingCode 二维码解析 * String zxingAnalyze = QRCodeUtil.zxingCodeAnalyze("E://zxingcode.jpg").toString(); */ System.out.println("success"); } }