概述
前言
本文实现 Spring 环境前端访问 <img>,访问后台,展示出一个二维码
出于简化的考虑,本文的二维码在后台固定内容,大小等参数。
1、之前写过一篇文章:关于二维码生成的3种方式
但是其中关于在线生成二维码的方式不是Spring的,所以写本文算是以后可以直接使用代码了
http://blog.csdn.net/bestcxx/article/details/53116996
2、Spring 基本环境搭建
(MAVEN)Spring MVC 入门之页面跳转
3、pom.xml 增加zxing依赖
<!-- 字符串生成二维码 -->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.3.0</version>
</dependency>
4、前台页面
logoFlag=yes 则二维码会额外增加logo,logo是20乘以20
<br><br>
<img alt="纯二维码测试" src="<%=basePath %>/qrCode/qrCode?logoFlag=no">
<br><br>
<img alt="二维码增加图标测试" src="<%=basePath %>/qrCode/qrCode?logoFlag=yes">
5、后台Controller
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Hashtable;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
@Controller
@RequestMapping("/qrCode")
public class RqCodeController {
/**
*
* @param request
* @param response
* @param logoFlag 是否需要为二维码增加logo yes:增加,no:不增加
* @throws IOException
*/
@GetMapping("/qrCode")
public void getQrCode(HttpServletRequest request,HttpServletResponse response,String logoFlag) throws IOException{
response.setContentType("image/jpeg");
OutputStream out = response.getOutputStream();
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);
String url="http://www.bestcxx.cn";
int width = 180;
int height = 180;
String format = "png";
String logoPath="D://1.PNG";//logo的地址
Hashtable hints= new Hashtable();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
BitMatrix bitMatrix;
try {
bitMatrix = new MultiFormatWriter().encode(url, BarcodeFormat.QR_CODE, width, height,hints);
//去白边
int[] rec = bitMatrix.getEnclosingRectangle();
int resWidth = rec[2] + 1;
int resHeight = rec[3] + 1;
BitMatrix resMatrix = new BitMatrix(resWidth, resHeight);
resMatrix.clear();
for (int i = 0; i < resWidth; i++) {
for (int j = 0; j < resHeight; j++) {
if (bitMatrix.get(i + rec[0], j + rec[1])) {
resMatrix.set(i, j);
}
}
}
BufferedImage image = toBufferedImage(resMatrix);
//增加图标
if("yes".equals(logoFlag)){//标志位为yes 则增加图标
BufferedImage logo = ImageIO.read(new File(logoPath));
Graphics2D g = image.createGraphics();
// logo宽高
width =20;
height =20;
// logo起始位置,此目的是为logo居中显示
int x = (image.getWidth() - width) / 2;
int y = (image.getHeight() - height) / 2;
g.drawImage(logo, x, y, null);
g.dispose();
}
ImageIO.write(image, "PNG", out);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static BufferedImage toBufferedImage(BitMatrix matrix) {
int BLACK = 0xFF000000;
int WHITE = 0xFFFFFFFF;
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;
}
}
6、效果
+2017-11-28+
我们可以将这个方法优化为工具类,不直接对外开放,有修改,这样可以提供二维码在线展示或者下载
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.Hashtable;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
/**
* 生成二维码的工具类
* @author Administrator
*
*/
public class RqCodeUtil {
/**
*
* @param request
* @param response
* @param logoFlag 是否需要为二维码增加logo yes:增加,no:不增加
* @param typeFlag 1-图片下载 ,2-图片在线展示
* @param fielName 图片名称
* @throws IOException
*/
public void getQrCode(HttpServletRequest request,HttpServletResponse response,
String logoFlag,String url,String typeFlag,String fielName) throws IOException{
if("1".equals(typeFlag)){
response.setContentType("application/octet-stream"); //图片下载
}else{
response.setContentType("image/jpeg"); //图片展示
}
OutputStream out = response.getOutputStream();
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);
response.addHeader("Content-Disposition","attachment;filename="+new String((fielName+new Date().getTime()+".png").getBytes()));//设置header,文件名字
int width = 180;
int height = 180;
String format = "png";
String logoPath="D://1.PNG";//logo的地址
Hashtable hints= new Hashtable();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
BitMatrix bitMatrix;
try {
bitMatrix = new MultiFormatWriter().encode(url, BarcodeFormat.QR_CODE, width, height,hints);
//去白边
int[] rec = bitMatrix.getEnclosingRectangle();
int resWidth = rec[2] + 1;
int resHeight = rec[3] + 1;
BitMatrix resMatrix = new BitMatrix(resWidth, resHeight);
resMatrix.clear();
for (int i = 0; i < resWidth; i++) {
for (int j = 0; j < resHeight; j++) {
if (bitMatrix.get(i + rec[0], j + rec[1])) {
resMatrix.set(i, j);
}
}
}
BufferedImage image = toBufferedImage(resMatrix);
//增加图标
if("yes".equals(logoFlag)){//标志位为yes 则增加图标
BufferedImage logo = ImageIO.read(new File(logoPath));
Graphics2D g = image.createGraphics();
// logo宽高
width =20;
height =20;
// logo起始位置,此目的是为logo居中显示
int x = (image.getWidth() - width) / 2;
int y = (image.getHeight() - height) / 2;
g.drawImage(logo, x, y, null);
g.dispose();
}
ImageIO.write(image, "PNG", out);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static BufferedImage toBufferedImage(BitMatrix matrix) {
int BLACK = 0xFF000000;
int WHITE = 0xFFFFFFFF;
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;
}
}
+20190313如果要指定图片名称+
response.setHeader("Content-Disposition", "attachment;filename=名字.jpg");
最后
以上就是感动饼干为你收集整理的Spring 使用展示二维码标签的全部内容,希望文章能够帮你解决Spring 使用
展示二维码标签所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复