首页 技术 正文
技术 2022年11月15日
0 收藏 682 点赞 4,249 浏览 2240 个字

前端:

<image src=”{{img_usrl}}” style=”width:100%;height:104px;” bindlongtap=”saveImg”></image>

js部分:在onLoad中请求

//我的userid 值为:239a3c37-3c2e-4a9d-be74-557638b23b63
this.setData({ img_usrl: getApp().getBaseUrl() + “/icon/” + userid });

java后台:

@RequestMapping("icon/{cateogry}")
public void getQrcodeImg(@PathVariable("cateogry") String cateogry,
HttpServletRequest request,
HttpServletResponse response){ try {
//cateogry的值为前端请求的值:239a3c37-3c2e-4a9d-be74-557638b23b63
String url = "固定写死的url路径?id参数="+cateogry;
BufferedImage image = QRCodeUtli.getqrcode(url);
response.setContentType("image/png"); OutputStream stream = response.getOutputStream();
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(image, "png", out);
stream.write(out.toByteArray());
stream.flush();
stream.close();
} catch (Exception e) {
e.printStackTrace();
}
}

工具QRCodeUtli类:

package com.early.api.util;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.Map;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
public class QRCodeUtli { private static final int BLACK = 0xFF000000;
private static final int WHITE = 0xFFFFFFFF; public static BufferedImage getqrcode(String code) {
int width = 400; // 图像宽度
int height = 400; // 图像高度
Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
hints.put(EncodeHintType.MARGIN, 0);//设置白边宽度,取值0~4
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
try {
BitMatrix bitMatrix = new MultiFormatWriter().encode(code,
BarcodeFormat.QR_CODE, width, height, hints);
BufferedImage image = toBufferedImage(bitMatrix);
return image;
} catch (WriterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public static BufferedImage toBufferedImage(BitMatrix matrix) {
int width = matrix.getWidth();
int height = matrix.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, matrix.get(x, y) ? BLACK : WHITE);
}
}
return image;
}
}

pom文件需要引用的包:

<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.3.1</version>
</dependency>

<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.3.0</version>
</dependency>

 
 
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,489
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,904
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,737
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,490
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,128
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,291