最近项目用到了二维码,就学习整理了一下,二维码主要用到了zxing。框架使用的是SSM,另一篇博客有相关配置说明,如需了解请移步https://blog.csdn.net/w_q2018/article/details/102912420
Controller
package com.qrcode.controller;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
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.WriterException;
import com.google.zxing.common.BitMatrix;
import com.qrcode.bean.Bean;
import com.qrcode.service.Service;
import com.qrcode.util.MatrixToImageWriter;
@Controller
public class Controller {
@Resource(name="Service")
private Service service;
@RequestMapping("download")
public void download(HttpServletRequest request, HttpServletResponse response){
//通过id查询
List list = service.queryIdList();
if(list != null && list.size()>0){
ZipOutputStream zos = null;
try {
String downloadFilename = "download";//文件的名称
downloadFilename = URLEncoder.encode(downloadFilename, "UTF-8");//转换中文否则可能会产生乱码
response.setContentType("application/octet-stream");// 指明response的返回对象是文件流
response.setHeader("Content-Disposition", "attachment;filename=" + downloadFilename+".zip");// 设置在下载框默认显示的文件名
zos = new ZipOutputStream(response.getOutputStream());
for(Bean info:list ){
zos.putNextEntry(new ZipEntry(info.getId()+".png"));//命名
getBarCodeImgByUrl(info.getUrl()+info.getId(), zos);//拼接了url
}
zos.flush();
zos.close();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (WriterException e) {
e.printStackTrace();
} finally{
if(zos != null){
try {
zos.flush();
zos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
public static void getBarCodeImgByUrl(String url,OutputStream os) throws WriterException,IOException{
//二维码参数
int width = 400; // 图像宽度
int height = 400; // 图像高度
String format = "png";// 图像类型
Map hints = new HashMap();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
BitMatrix bitMatrix;
bitMatrix = new MultiFormatWriter().encode(url,BarcodeFormat.QR_CODE, width, height, hints);
MatrixToImageWriter.writeToStream(bitMatrix, format, os);
}
}
Service
package com.qrcode.service;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.qrcode.bean.Bean;
import com.qrcode.dao.Dao;
@Service("Service ")
public class Service {
@Resource
private Dao dao;
public List queryIdList(){
return dao.queryIdList();
}
}
Dao
package com.qrcode.dao;
import java.util.List;
import com.qrcode.bean.Bean;
public interface Dao {
public List queryIdList();
}
Bean
package com.qrcode.bean;
public class Bean {
private long id;
private String url;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
@Override
public String toString() {
return "Bean [id=" + id + ", url=" + url + "]";
}
}
xml
util
package com.qrcode.util;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import javax.imageio.ImageIO;
import java.util.Hashtable;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
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) throws Exception {
String text = "http://www.baidu.com"; // 二维码内容
int width = 300; // 二维码图片宽度
int height = 300; // 二维码图片高度
String format = "jpg";// 二维码的图片格式
Hashtable hints = new Hashtable();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); // 内容所使用字符集编码
BitMatrix bitMatrix = new MultiFormatWriter().encode(text,
BarcodeFormat.QR_CODE, width, height, hints);
// 生成二维码
File outputFile = new File("d:" + File.separator + "new.jpg");
MatrixToImageWriter.writeToFile(bitMatrix, format, outputFile);
}
}