1.gradle導(dǎo)入
// https://mvnrepository.com/artifact/com.google.zxing/core
compile group: 'com.google.zxing', name: 'core', version: '3.3.3'
2.代碼
package com.ligeit.ec.qrcode.common;
import com.github.binarywang.utils.qrcode.BufferedImageLuminanceSource;
import com.google.zxing.*;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import javax.imageio.ImageIO;
import java.io.File;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class QRCodeUtil {
????//二維碼顏色
????private static final int BLACK = 0xFF000000;
????//二維碼顏色
????private static final int WHITE = 0xFFFFFFFF;
????/**
????* ZXing 方式生成二維碼
????* @param text 二維碼內(nèi)容
????* @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();
????????//設(shè)置編碼字符集
????????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 < codeWidth; i++) {
????????????????for (int j = 0; j < codeHeight; j++) {
????????????????????//4吁断、循環(huán)將二維碼內(nèi)容定入圖片
????????????????????image.setRGB(i, j, encode.get(i, j) ? BLACK : WHITE);
????????????????}
????????????}
????????????File outPutImage = new File(outPutPath);
????????????//如果圖片不存在創(chuàng)建圖片
????????????if(!outPutImage.exists()) outPutImage.createNewFile();
????????????//5鸟辅、將二維碼寫入圖片
????????????ImageIO.write(image, imageType, outPutImage);
????????} catch (WriterException e) {
????????????e.printStackTrace();
????????????System.out.println("二維碼生成失敗");
????????} catch (IOException e) {
????????????e.printStackTrace();
????????????System.out.println("生成二維碼圖片失敗");
????????}
? ? }
????/**
????* 二維碼解析
????* @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 BufferedImageLuminanceSource(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;
????}
????public static void main(String[] args) {
????????String pathname = "public/uploads/qrcode/";
????????File uploadDir = new File(pathname);
????????uploadDir.mkdirs();
????????//ZXingCode 二維碼生成測試
????????QRCodeUtil.zxingCodeCreate("http://blog.csdn.net/u014266877", 300, 300, "public/uploads/qrcode/zxingcode.jpg", "jpg");
????????/**
????????* ZxingCode 二維碼解析
????????* String zxingAnalyze = QRCodeUtil.zxingCodeAnalyze("E://zxingcode.jpg").toString();
????????*/
????????System.out.println("success");
????}
}