今天接到一个任务:生成一个二维码接口传递给php端,通过扫描二维码连接到网站。
虽然很早接触到二维码,但是一直没有研究过,本文只是记录这次生成二维码的过程,留待本人查看,望大家交流学习!
看了几个帖子,大部分都是用的google的二维码jar
1.首先要下载ZXing的jar包,maven项目,也可以直接用maven repository 下载已经编译好的jar包
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.2.1</version>
</dependency>
2.第二步,我们需要一个工具类MatrixToImageWriter来处理我们的需求
直接上代码了
package com.echin.tool;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import javax.imageio.ImageIO;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
public class MatrixToImageWriter {
private static final int BLACK = 0xFF000000;
private static final int WHITE = 0xFFFFFFFF;
private MatrixToImageWriter() {}
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;
}
public static void writeToFile(BitMatrix matrix, String format, File file)
throws IOException {
BufferedImage image = toBufferedImage(matrix);
if (!ImageIO.write(image, format, file)) {
throw new IOException("Could not write an image of format " + format + " to " + file);
}
}
public static void writeToStream(BitMatrix matrix, String format, OutputStream stream)
throws IOException {
BufferedImage image = toBufferedImage(matrix);
if (!ImageIO.write(image, format, stream)) {
throw new IOException("Could not write an image of format " + format);
}
}
public static void main(String[] args) {
try {
String content = "岳茂叶Happy Birthday!";
String path = "E:\\upload";
MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
Map hints = new HashMap();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
BitMatrix bitMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, 400, 400,hints);
File file1 = new File(path,"二维码.jpg");
MatrixToImageWriter.writeToFile(bitMatrix, "jpg", file1);
} catch (Exception e) {
e.printStackTrace();
}
}
}
3.根据不同的需求,调用不同的方法
(1)writeToFile:生成本地的文件二维码
(2)writeToStream:以文件流的格式传递到页面啊,客户端等
4.下面一个是我自己传递到页面的controller
@RequestMapping("/qrcode")
@ResponseBody
public void list(String content,HttpServletResponse response){
response.setContentType("image/jpeg");//设置相应类型,告诉浏览器输出的内容为图片
response.setHeader("Pragma", "No-cache");//设置响应头信息,告诉浏览器不要缓存此内容
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expire", 0);
try {
// String content = "Happy Birthday!";
MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
Map hints = new HashMap();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
BitMatrix bitMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, 200, 200,hints);
MatrixToImageWriter.writeToStream(bitMatrix, "JPEG", response.getOutputStream());
} catch (Exception e) {
e.printStackTrace();
}
}
5.大功告成,这时候可以访问自己的ip,看下自己的二维码了,扫描下验证是不是自己要的内容