当前位置:网站首页>微信支付二维码生成
微信支付二维码生成
2022-06-22 12:22:00 【漂泊的猎人】
微信支付其二维码是根据支付相关信息,到微信服务器申请支付二维码,然后将二维码内容显示在网站,这样用户就可以对订单进行支付下单。
1、生成图片
public static BufferedImage drawImage(String content, String imgPath, boolean needCompress) throws Exception {
Hashtable<EncodeHintType, Object> hints = new Hashtable<>();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
hints.put(EncodeHintType.MARGIN, 1);
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE,
hints);
bitMatrix = deleteWhite(bitMatrix);
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);
}
}
return image;
}
2、插入图片
private static void insertImage(BufferedImage source, String imgPath, boolean needCompress) throws Exception {
File file = new File(imgPath);
if (!file.exists()) {
System.err.println("" + imgPath + " 该文件不存在!");
return;
}
Image src = ImageIO.read(new File(imgPath));
int width = src.getWidth(null);
int height = src.getHeight(null);
if (needCompress) {
// 压缩LOGO
if (width > WIDTH) {
width = WIDTH;
}
if (height > HEIGHT) {
height = 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 = source.createGraphics();
int x = (QRCODE_SIZE - width) / 2;
int y = (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();
}
3、图片转换为Base64
try(ByteArrayOutputStream outputStream = new ByteArrayOutputStream();) {
BufferedImage image = QRCodeUtil.createImage(codeUrl , null, false);
ImageIO.write(image, "jpg", outputStream);
String base64Str = "data:image/jpeg;base64,"+new BASE64Encoder().encode(outputStream.toByteArray());
} catch (Exception e) {
log.error(e.getMessage(),e);
}
边栏推荐
- 【游戏】周瑜技巧
- 动作捕捉系统用于地下隧道移动机器人定位与建图
- 助力金融信息化创新,巨杉数据库近期持续中标50余家金融客户
- Tianyi cloud digital government smart data center has passed the certification
- Final of the 13th Blue Bridge Cup embedded design and development project
- SAP-ABAP-如何用WEBAPI的方式调用外部接口
- Flutter混合开发练习——Evenet&Method Channel协作加载大图
- Sap-abap- how to transfer material master data, supplier master data, work orders, purchase orders and other information to external systems in real time - implicit enhancement.
- V4l2 pixel format and its corresponding meaning
- 通过 postgis 制作 按照米制的矩形边框
猜你喜欢

Tis tutorial 01- installation

Flutter:剥离StatefulWidget——简化页面开发、跳转以及传值

webrtc入门:11.Kurento中使用RtpEndpoint拉取rtp流在直播中做集群

Tianyi cloud digital government smart data center has passed the certification

SNC processing failed SAP Router证书重新生成

修复flutter_webview_plugin在页面滑出时web图层残留的问题

关于 GIN 的路由树

Jushan database was invited to attend Kunpeng developers' annual event 2022 to jointly build a domestic digital base

SAP-ABAP- 如何查找表,字段有哪些关联表

2022-6-21os review group linking method
随机推荐
Fluent: split statefulwidget -- simplify page development, jump and value transfer
推荐一款M1芯片电脑快速搭建集群的虚拟机软件
【Qt】QFileInfo获取文件名的各个组成部分
【mysql】用sql求中位数
Final of the 13th Blue Bridge Cup embedded design and development project
Flutter之CustomPaint 绘制贝塞尔曲线图表(三)
SAP-ABAP- 如何查找表,字段有哪些关联表
Getting started with fluent Animation: inner and outer reverse ring loading animation
Zcu102 PL end running water lamp
关于 GIN 的路由树
Getting started with webrtc: 11 In kurento, rtendpoint is used to pull RTP streams for clustering in live broadcast
odps sql的执行流程不是从上到下执行吗
2022-6-21os review group linking method
JAXB元素详解
SAP fi financial statement version setting
Parallels Desktop 17.1.4pd虚拟机
帝云CMS升级PHP8注意事项
About buildreoot compilation -- the function is configured but not compiled into the kernel
SAP-ABAP-如何实时传输物料主数据,供应商主数据,工单,采购订单等信息给外部系统-隐式增强。
Fault tolerant heap shim applied to current process. This is usually due to previous crashes