生成二维码(只适用于字符串)

package com.pbids.sanqin.util;

import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageConfig;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import org.junit.Test;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.nio.file.Path;
import java.util.Hashtable;


public class GeneratingCodeUtil {

/**
* 生成二维码
* @param url
* @param resourcePath
* @return
* @throws Exception
*/
public static Path encode(String url, String resourcePath) throws Exception {
int width = 100;
int height = 100;
String format = "png";
Hashtable hints = new Hashtable();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
hints.put(EncodeHintType.MARGIN, 1);
try {
BitMatrix bitMatrix = new MultiFormatWriter().encode(url, BarcodeFormat.QR_CODE, width, height, hints);
String fileurl = resourcePath + "qrcode";

File img = new File(fileurl);
if (img.exists()) {

} else {
img.mkdir();
}
MatrixToImageConfig config = new MatrixToImageConfig(0, 16777215);
fileurl += "/" + System.currentTimeMillis() + "_" + RandowUtil.genRandomNum(5) + ".png";
Path filepath = new java.io.File(fileurl).toPath();
MatrixToImageWriter.writeToPath(bitMatrix, format, filepath, config);
return filepath;
} catch (WriterException e) {
e.printStackTrace();
}
return null;
}


public void decode() throws Exception {
MultiFormatReader formatReader = new MultiFormatReader();
File file = new File("D:/new.png");
BufferedImage image = null;
try {
image = ImageIO.read(file);
} catch (IOException e) {
e.printStackTrace();
}
BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));
Hashtable hints = new Hashtable();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
Result result = formatReader.decode(binaryBitmap, hints);
System.err.println("解析结果:" + result.toString());
System.out.println(result.getBarcodeFormat());
System.out.println(result.getText());
}

/**
* @return
* @Description: 根据图片地址转换为base64编码字符串
* @Author:
* @CreateTime:
*/
public static String getImageStr(String imgFile) {
InputStream inputStream = null;
byte[] data = null;
try {
inputStream = new FileInputStream(imgFile);
data = new byte[inputStream.available()];
inputStream.read(data);
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
// 加密
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(data);
}

/**
* @param imgStr base64编码字符串
* @param path 图片路径-具体到文件
* @return
* @Description: 将base64编码字符串转换为图片
* @Author:
* @CreateTime:
*/
public static boolean generateImage(String imgStr, String path) {
if (imgStr == null) {
return false;
}
BASE64Decoder decoder = new BASE64Decoder();
try {
// 解密
byte[] b = decoder.decodeBuffer(imgStr);
// 处理数据
for (int i = 0; i < b.length; ++i) {
if (b[i] < 0) {
b[i] += 256;
}
}
OutputStream out = new FileOutputStream(path);
out.write(b);
System.out.println("解码成功");
out.flush();
out.close();
return true;
} catch (Exception e) {
return false;
}
}


/* public static void main(String[] args) {
String strImg = getImageStr("D:/bg.png");
System.out.println(strImg);
generateImage(strImg, "D:/aa/bg.png");
}*/
}

须要导入的maven
<!--生成二维码的Jar包--><dependency>    <groupId>com.google.zxing</groupId>    <artifactId>core</artifactId>    <version>3.0.0</version></dependency><dependency>    <groupId>com.google.zxing</groupId>    <artifactId>javase</artifactId>    <version>3.0.0</version></dependency>