二維碼工具類
package zj.bar.util;
import java.awt.BasicStroke;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Shape;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Hashtable;
import javax.imageio.ImageIO;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.Result;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import zj.bar.bean.BufferedImageLuminanceSource;
import zj.bar.bean.QRCodeToString;
import zj.bar.bean.StringToQRCode;
import zj.common.exception.ServiceException;
/**
* 二維碼工具類
*
*/
public class QRCodeUtil {
/**
* 創建圖片
*
* @param param_content
* @param imgPath
* @param needCompress
* @return
*/
public static void createImage(StringToQRCode P_bean) {
try {
Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
hints.put(EncodeHintType.CHARACTER_SET, P_bean.param_charset);
hints.put(EncodeHintType.MARGIN, 1);
BitMatrix bitMatrix = new MultiFormatWriter().encode(P_bean.param_content, BarcodeFormat.QR_CODE, P_bean.param_qrcode_size, P_bean.param_qrcode_size, hints);
int width = bitMatrix.getWidth();
int height = bitMatrix.getHeight();
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
}
}
// 插入圖片
P_bean.rtn_stream_cover_img = image;
if (P_bean.param_file_logo == null || !P_bean.param_file_logo.exists()) {
return;
}
insertImage(P_bean);
} catch (Exception e) {
throw new ServiceException(e);
}
}
/**
* 插入LOGO
*
* @param source
* 二維碼圖片
* @param imgPath
* LOGO圖片地址
* @param needCompress
* 是否壓縮
* @throws Exception
*/
public static void insertImage(StringToQRCode P_bean) {
try {
if (!P_bean.param_file_logo.exists()) {
System.err.println("" + P_bean.param_file_logo + " 該文件不存在!");
return;
}
Image src = ImageIO.read(P_bean.param_file_logo);
int width = src.getWidth(null);
int height = src.getHeight(null);
if (P_bean.param_compress) { // 壓縮LOGO
if (width > P_bean.param_width) {
width = P_bean.param_width;
}
if (height > P_bean.param_height) {
height = P_bean.param_height;
}
Image image = src.getScaledInstance(width, height, Image.SCALE_SMOOTH);
BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = tag.getGraphics();
g.drawImage(image, 0, 0, null); // 繪制縮小后的圖
g.dispose();
src = image;
}
// 插入LOGO
Graphics2D graph = P_bean.rtn_stream_cover_img.createGraphics();
int x = (P_bean.param_qrcode_size - width) / 2;
int y = (P_bean.param_qrcode_size - height) / 2;
graph.drawImage(src, x, y, width, height, null);
Shape shape = new RoundRectangle2D.Float(x, y, width, width, 6, 6);
graph.setStroke(new BasicStroke(3f));
graph.draw(shape);
graph.dispose();
} catch (Exception e) {
throw new ServiceException(e);
}
}
/**
* 生成二維碼(內嵌LOGO)
*
* @param param_content
* 內容
* @param imgPath
* LOGO地址
* @param destPath
* 存放目錄
* @param needCompress
* 是否壓縮LOGO
* @throws Exception
*/
public static void encode(StringToQRCode P_bean) {
try {
createImage(P_bean);
mkdirs(P_bean.rtn_file_dest);
// String fileName = new Random().nextInt(99999999) + ".jpg";
ImageIO.write(P_bean.rtn_stream_cover_img, P_bean.param_format_name, P_bean.rtn_file_dest);
} catch (Exception e) {
throw new ServiceException(e);
}
}
/**
* 當文件夾不存在時,mkdirs會自動創建多層目錄,區別于mkdir.(mkdir如果父目錄不存在則會拋出異常)
*
* @author lanyuan Email: mmm333zzz520@163.com
* @date 2013-12-11 上午10:16:36
* @param destPath
* 存放目錄
*/
public static void mkdirs(File file) {
// 當文件夾不存在時,mkdirs會自動創建多層目錄,區別于mkdir.(mkdir如果父目錄不存在則會拋出異常)
if (file.isDirectory()) {
if (!file.exists()) {
file.mkdirs();
}
} else {
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
}
}
/**
* 解析二維碼
*
* @param file
* 二維碼圖片
* @return
* @throws Exception
*/
public static void decode(QRCodeToString P_bean) {
try {
if (P_bean.param_file == null) {
return;
}
BufferedImage image = ImageIO.read(P_bean.param_file);
if (image == null) {
return;
}
BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();
hints.put(DecodeHintType.CHARACTER_SET, P_bean.param_charset);
Result result = new MultiFormatReader().decode(bitmap, hints);
P_bean.rtn_content = result.getText();
} catch (Exception e) {
throw new ServiceException(e);
}
}
public static void main(String[] args) {
try {
StringToQRCode P_string_to_qrcode = new StringToQRCode();
P_string_to_qrcode.param_content = "weixin://wxpay/bizpayurl?pr=dQpQGGrzz";
P_string_to_qrcode.rtn_file_dest = new File("E:/www.dlhighland.cn/pay/www.dlhighland.cn.jpg");
encode(P_string_to_qrcode);
System.out.println("生成圖片對象:" + P_string_to_qrcode.rtn_file_dest);
System.out.println("生成圖片流:" + P_string_to_qrcode.rtn_stream_cover_img);
QRCodeToString P_qrcode_to_string = new QRCodeToString();
P_qrcode_to_string.param_file = P_string_to_qrcode.rtn_file_dest;
decode(P_qrcode_to_string);
System.out.println("返回內容:" + P_qrcode_to_string.rtn_content);
} catch (Exception e) {
e.printStackTrace();
}
}
}二維碼類StringToQRCode
/**
* @Description QRCode.java
* @author 張軍
* @date 2022年2月18日 上午1:51:07
* @version V1.0
*/
package zj.bar.bean;
import java.awt.image.BufferedImage;
import java.io.File;
/**
* 二維碼類
*
* @version 1.00 (2014.09.15)
* @author SHNKCS 張軍 {@link <a target=_blank href="http://www.dlhighland.cn">張軍個人網站</a> <a target=_blank href="http://user.qzone.qq.com/360901061/">張軍QQ空間</a>}
*/
public class StringToQRCode {
public String param_charset = "utf-8";
public String param_format_name = "JPG";
/** 二維碼尺寸 **/
public int param_qrcode_size = 300;
/** LOGO寬度 **/
public int param_width = 60;
/** LOGO高度 **/
public int param_height = 60;
/** 二維碼圖片內容 **/
public String param_content;
/** LOGO圖片地址 **/
public File param_file_logo;
/** 是否壓縮 **/
public boolean param_compress = true;
/** logo覆蓋插入 **/
public BufferedImage rtn_stream_cover_img;
/** 二維碼圖片 **/
public File rtn_file_dest;
}二維碼類QRCodeToString
/**
* @Description QRCode.java
* @author 張軍
* @date 2022年2月18日 上午1:51:07
* @version V1.0
*/
package zj.bar.bean;
import java.io.File;
/**
* 二維碼類
*
* @version 1.00 (2014.09.15)
* @author SHNKCS 張軍 {@link <a target=_blank href="http://www.dlhighland.cn">張軍個人網站</a> <a target=_blank href="http://user.qzone.qq.com/360901061/">張軍QQ空間</a>}
*/
public class QRCodeToString {
public String param_charset = "utf-8";
/** 二維碼圖片 **/
public File param_file;
/** 二維碼圖片內容 **/
public String rtn_content;
}
本文為張軍原創文章,轉載無需和我聯系,但請注明來自張軍的軍軍小站,個人博客http://www.dlhighland.cn
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

