package com.util;
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 org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import cn.da.posting.util.StringUtil;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.Binarizer;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
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.Result;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
public class QrCodeUtil {
private static final Log logger = LogFactory.getLog(QrCodeUtil.class);
/**
* 生成二维码图像
*
* @throws WriterException
* @throws IOException
*/
public static Boolean createQrCode(String qcImageFile, String content, String charset) {
//String content = "weixin://wxpay/bizpayurl?sr=XXXXX";
try {
int width = 200; // 图像宽度
int height = 200; // 图像高度
String format = "png";// 图像类型
Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
hints.put(EncodeHintType.CHARACTER_SET, charset);
BitMatrix bitMatrix;
bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width,
height, hints);
// 生成矩阵
File file = new File(qcImageFile);
MatrixToImageWriter.writeToFile(bitMatrix, format, file);// 输出图像
return true;
} catch (WriterException e) {
logger.error("二维码生成错误", e);
} catch (IOException e) {
logger.error("二维码生成错误", e);
}
return false;
}
/**
* 解析二维码图像
*/
public static String parseQrCode(String qcImageFile) {
BufferedImage image;
try {
image = ImageIO.read(new File(qcImageFile));
LuminanceSource source = new BufferedImageLuminanceSource(image);
Binarizer binarizer = new HybridBinarizer(source);
BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
Map<DecodeHintType, Object> hints = new HashMap<DecodeHintType, Object>();
hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
Result result = new MultiFormatReader().decode(binaryBitmap, hints);// 对图像进行解码
return result.getText();
} catch (IOException e) {
logger.error("二维码解析错误", e);
} catch (NotFoundException e) {
logger.error("二维码解析错误", e);
}
return StringUtil.EMPTY_STRING;
}
}
QrCodeUtil.zip_QRCODE_qrcode utild_二维码生成
版权申诉
120 浏览量
2022-09-24
22:06:23
上传
评论
收藏 1KB ZIP 举报
我虽横行却不霸道
- 粉丝: 76
- 资源: 1万+
最新资源
- 数据文件之数据库业务监控
- 6.1 07.静态路由及默认路由基本配置.topo
- 计算机网络期末考判断题题库
- NX二次开发UF-CAM-ask-clear-plane-usage 函数介绍
- Windows服务监控工具 ManageEngine-ServiceMonitor 运维工具监控MySQL, MSSQL
- java基于 ssm + jsp 考研指导平台系统源码 带毕业论文
- AD域管理工具 ManageEngine-ADManager-Plus-Tools 运维工具监控Windows AD域控
- 计算机网络期末考选择题题库
- 机器学习课程设计猫狗图像识别检测分类项目源码+数据集+说明文档.zip
- Windows管理工具 任务管理器、获取计算机上安装的哪些软件、远程关机/重启计算机
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈