最近开发遇到了一个批量打印二维码并生成zip包的需求,先记录下来

pom依赖

<!--添加二维码依赖包--><dependency><groupId>com.google.zxing</groupId><artifactId>core</artifactId><version>3.3.0</version></dependency>

controller层

/*** data为链接,用 , 分割* @param response* @param data* @throws Exception*/@RequestMapping("/download")public void download(HttpServletResponse response ,String data) throws Exception {ZipOutputStream zos = null;String  downloadFilename = "门票二维码";//转换中文否则可能会产生乱码downloadFilename = URLEncoder.encode(downloadFilename, "UTF-8");// 指明response的返回对象是文件流response.setContentType("application/octet-stream");// 设置在下载框默认显示的文件名response.setHeader("Content-Disposition", "attachment;filename=" + downloadFilename+".zip");zos = new ZipOutputStream(response.getOutputStream());String[] admissions = data.split(",");for (int i = 0; i<admissions.length; i++){zos.putNextEntry(new ZipEntry(admissions[i]+".png"));ImageIO.write(QRCodeUtil.encode(admissions[i],"D:/logo.png",true),"jpg",zos);}zos.flush();zos.close();}

utils

这个utils很多地方都能搜到。

public class QRCodeUtil {private static final String CHARSET = "utf-8";private static final String FORMAT_NAME = "JPG";// 二维码尺寸private static final int QRCODE_SIZE = 1000;// LOGO宽度private static final int WIDTH = 60;// LOGO高度private static final int HEIGHT = 60;private static BufferedImage createImage(String content, String imgPath, boolean needCompress) throws Exception {Hashtable 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);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);}}if (imgPath == null || "".equals(imgPath)) {return image;}// 插入图片QRCodeUtil.insertImage(image, imgPath, needCompress);return image;}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) { // 压缩LOGOif (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;}// 插入LOGOGraphics2D 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();}public static void encode(String content, String imgPath, String destPath, boolean needCompress) throws Exception {BufferedImage image = QRCodeUtil.createImage(content, imgPath, needCompress);mkdirs(destPath);// String file = new Random().nextInt(99999999)+".jpg";// ImageIO.write(image, FORMAT_NAME, new File(destPath+"/"+file));ImageIO.write(image, FORMAT_NAME, new File(destPath));}public static BufferedImage encode(String content, String imgPath, boolean needCompress) throws Exception {BufferedImage image = QRCodeUtil.createImage(content, imgPath, needCompress);return image;}public static void mkdirs(String destPath) {File file = new File(destPath);// 当文件夹不存在时,mkdirs会自动创建多层目录,区别于mkdir.(mkdir如果父目录不存在则会抛出异常)if (!file.exists() && !file.isDirectory()) {file.mkdirs();}}public static void encode(String content, String imgPath, String destPath) throws Exception {QRCodeUtil.encode(content, imgPath, destPath, false);}// 被注释的方法/** public static void encode(String content, String destPath, boolean* needCompress) throws Exception { QRCodeUtil.encode(content, null, destPath,* needCompress); }*/public static void encode(String content, String destPath) throws Exception {QRCodeUtil.encode(content, null, destPath, false);}public static void encode(String content, String imgPath, OutputStream output, boolean needCompress)throws Exception {BufferedImage image = QRCodeUtil.createImage(content, imgPath, needCompress);ImageIO.write(image, FORMAT_NAME, output);}public static void encode(String content, OutputStream output) throws Exception {QRCodeUtil.encode(content, null, output, false);}public static String decode(File file) throws Exception {BufferedImage image;image = ImageIO.read(file);if (image == null) {return null;}BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(image);BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));Result result;Hashtable hints = new Hashtable();hints.put(DecodeHintType.CHARACTER_SET, CHARSET);result = new MultiFormatReader().decode(bitmap, hints);String resultStr = result.getText();return resultStr;}public static String decode(String path) throws Exception {return QRCodeUtil.decode(new File(path));}

java批量生成二维码图片,并打包成zip相关推荐

  1. java如何批量生成二维码,并返回成压缩包形式?

    最近有点忙,现在稍微闲一些,就想写写最近遇到的一些业务. 业务:后台根据前端传入的需生成的数量,生成二维码图片并统一打包成压缩包返回. 效果图: 首先根据一定规则生成二维码字符串,然后返回成list( ...

  2. java springboot生成二维码图片

    java生成二维码图片 Maven依赖 <!--生成二维码--> <dependency><groupId>com.google.zxing</groupId ...

  3. Thinkphp5+ phpqrcode 在线批量生成二维码图片并保存在本地

    第一步: 首先需要先下载phpqrcode包,解压在项目中,我们只用到 phpqrcode.php 文件就可以; phpqrcode下载链接为: http://phpqrcode.sourceforg ...

  4. vue批量生成二维码并压缩打包下载(图片带标题)

    借鉴:原文~~~(但是有问题,在基础上改良) 效果图(生成的二维码带下面标题的): 代码如下: 先安装依赖(或者用命令:npm i jszip): yarn add jszip yarn add fi ...

  5. Excel怎么批量生成二维码图片?

    1.在excel中单元格中录入二维码信息 ​ 2.选中数据单元格区域,然后点击[图片工具]选项 3.点击[批量生成] ​ 4.在[保存到区域]选项中录入F2 ​ 5.最后鼠标点击[确定]即可完成 ​ ...

  6. C# 利用QRCode生成二维码图片

    网上生成二维码的组件是真多,可是真正好用的,并且生成速度很快的没几个,QRCode就是我在众多中找到的,它的生成速度快.但是网上关于它的使用说明,真的太少了,大都是千篇一律的复制粘贴.这是本要用它做了 ...

  7. 使用Java生成二维码图片(亲测)

    下面我来分享两种生成二维码图片的方法. 第一种,填入你扫描二维码要跳转的网址直接生成二维码 第一步:导入相关的包 1 <dependency> 2 <groupId>com.g ...

  8. java实现后台直接生成二维码图片并直接上传到七牛云上面

    java实现后台直接生成二维码图片并直接上传到七牛云上面 需求:后台是直接根据唯一核销码生成图片,然后的话直接上传到七牛云,数据库只是保存地址,一开始也想错了,想成了创建临时文件存储生成的图片再上传到 ...

  9. 使用Java生成二维码图片

    下面我来分享两种生成二维码图片的方法. 第一种,填入你扫描二维码要跳转的网址直接生成二维码 第一步:导入相关的包 1 <dependency> 2 <groupId>com.g ...

最新文章

  1. 伯乐:一个易用、强大的PyTorch推荐系统开源库
  2. Alpha 冲刺 —— 十分之八
  3. 灰度图像--图像增强 锐化基础
  4. python文件操作二
  5. 让Elasticsearch飞起来!百亿级实时查询优化实战
  6. 全国计算机等级考试题库二级C操作题100套(第30套)
  7. Fatal Error[Pe1696]:cannot open source file “sys.h”
  8. [转载] 【零基础学爬虫】python中的yield详解
  9. 小米用户画像_腾讯企鹅智库发布手机品牌用户画像:华为一二线城市用户少于小米...
  10. MS SQL Server带有时间的记录怎样查询
  11. 逼急了自己人都坑,腾讯内部上演吃鸡大战
  12. mysql之判断字段是否存在于表中
  13. R 修改安装包默认存放位置的方法
  14. 天涯明月刀最新服务器,天涯明月刀最新开服时间表 | 手游网游页游攻略大全
  15. php 生成小程序码
  16. 怎么锻炼java逻辑思维_托马斯逻辑训练板技高一筹,锻炼编程逻辑思维能力
  17. 抓包工具——IE浏览器HttpWatch插件
  18. 利用imu估计roll、pitch的理解
  19. 向日葵连接linux无桌面,向日葵控控远程时无画面显示可能的原因及解决办法
  20. 关于Co-segmentation

热门文章

  1. 《向上生长》读书摘记
  2. CodeBlocks安装汉化与使用说明
  3. 七牛云上传视频并转码
  4. 安徽农商行计算机类笔试考什么,2019安徽农商行社会招聘:笔试考什么?如何复习? 【附带笔试模拟题】...
  5. 在linux服务器上用headless firefox打开中文网页,字体乱码问题
  6. 达达,不能只做京东的达达
  7. 地理学(第一、第二)定律
  8. HBase Java 编程
  9. js实现拼图游戏(数字版本与图片版本)
  10. js实现时间每秒更新