import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import javax.imageio.ImageIO;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
public class QRCodeTool {
public static void main(String[] args) throws WriterException {
try {
generateQRCode(400, 400, "http://www.baidu.com/", "C:\\Users\\tang\\Desktop\\QR1.png", "C:\\Users\\tang\\Desktop\\20170705100835.jpg");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 生成二维码
*
* @param qrW
* 二维码宽度
* @param qrH
* 二维码高度
* @param qrContext
* 二维码内容
* @param qrPath
* 保存二维码的文件路径
* @param logoPath
* logo文件路径,不需要logo可以传null
*/
public static boolean generateQRCode(int qrW, int qrH, String qrContext, String qrPath, String logoPath) {
try {
QRCodeWriter writer = new QRCodeWriter();
Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);// 选择H档容错度,即使30%的图案被遮挡,也可以被正确扫描,这是保证之后添加LOGO图标的关键
BitMatrix matrix = writer.encode(qrContext, BarcodeFormat.QR_CODE, qrW, qrH, hints);
BufferedImage qrImg = MatrixToImageWriter.toBufferedImage(matrix);
if (logoPath != null) {
BufferedImage logoImg = ImageIO.read(new File(logoPath));
int logoW = logoImg.getWidth(null) > qrImg.getWidth() * 2 / 10 ? (qrImg.getWidth() * 2 / 10) : logoImg.getWidth(null);
int logoH = logoImg.getHeight(null) > qrImg.getHeight() * 2 / 10 ? (qrImg.getHeight() * 2 / 10) : logoImg.getWidth(null);
int logoX = (qrImg.getWidth() - logoW) / 2;
int logoY = (qrImg.getHeight() - logoH) / 2;
Graphics2D g = qrImg.createGraphics();
g.drawImage(logoImg, logoX, logoY, logoW, logoH, null);
g.dispose();
qrImg.flush();
}
ImageIO.write(qrImg, qrPath.substring(qrPath.lastIndexOf(".") + 1), new File(qrPath));
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}
生成后的二维码: