QRCode生成二维码并批量ZIP下载

一、二维码生成方式

主流二维码生成方式分为两种:

1、基于QRCode实现

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.imageio.ImageIO;

import jp.sourceforge.qrcode.QRCodeDecoder;
import jp.sourceforge.qrcode.data.QRCodeImage;
import jp.sourceforge.qrcode.exception.DecodingFailedException;

import com.swetake.util.Qrcode;

public class QRCodeUtils {  
    /** 
     * 编码字符串内容到目标File对象中 
     *  
     * @param encodeddata 编码的内容 
     * @param destFile  生成file文件  1381090722   5029067275903 
     * @throws IOException 
     */  
    public static void qrCodeEncode(String encodeddata, File destFile) throws IOException {  
        Qrcode qrcode = new Qrcode();  
        qrcode.setQrcodeErrorCorrect('M');  // 纠错级别(L 7%、M 15%、Q 25%、H 30%)和版本有关  
        qrcode.setQrcodeEncodeMode('B');      
        qrcode.setQrcodeVersion(20);     // 设置Qrcode包的版本  
        //二维码的大小需要根据版本的大小来确定
        int width = 67+12*(20-1);//295
          
        byte[] d = encodeddata.getBytes("utf-8"); // 字符集  
        // 创建BufferedImage对象
        BufferedImage bi = new BufferedImage(width, width, BufferedImage.TYPE_INT_RGB);  
        // 获取Graphics2D     // 创建图层  
        Graphics2D g = bi.createGraphics();  
          
        g.setBackground(Color.WHITE);   // 设置背景颜色(白色)  
        // 画图  
        //g.setBackground(new Color(255,255,255));
        //g2d.setPaint(new Color(0,0,0));  
        g.clearRect(0, 0, width, width);    // 矩形 X、Y、width、height  
        g.setColor(Color.BLACK);    // 设置图像颜色(黑色)  
        /**
         * 容易踩坑的地方
         * 1.注意for循环里面的i,j的顺序,
         *   s[j][i]二维数组的j,i的顺序要与这个方法中的 gs.fillRect(j*3+2,i*3+2, 3, 3);
         *   顺序匹配,否则会出现解析图片是一串数字
         * 2.注意此判断if (d.length > 0 && d.length < 123)
         *   是否会引起字符串长度大于120导致生成代码不执行,二维码空白
         *   根据自己的字符串大小来设置此配置
         */
        if (d.length > 0 && d.length < 123) {  
            boolean[][] b = qrcode.calQrcode(d);  
            for (int i = 0; i < b.length; i++) {  
                for (int j = 0; j < b.length; j++) {  
                    if (b[j][i]) {  
                        g.fillRect(j * 3 + 2, i * 3 + 2, 3, 3);  
                    }  
                }  
            }  
        }  
          
//      Image img = ImageIO.read(new File("D:/tt.png"));  logo  
//      g.drawImage(img, 25, 55,60,50, null);  
         
        g.dispose(); // 释放此图形的上下文以及它使用的所有系统资源。调用 dispose 之后,就不能再使用 Graphics 对象  
        /*需要画文字时需要使用图片合成方法,将两张图片合成为一张图片,再将这张图片保存*/
        // 创建BufferedImage对象

        bi.flush(); // 刷新此 Image 对象正在使用的所有可重构的资源  
         
        ImageIO.write(bi, "png", destFile);  
        System.out.println("Input Encoded data is:" + encodeddata);  
    }  
  
    /** 
     * 解析二维码,返回解析内容 
     *  
     * @param imageFile 
     * @return 
     */  
    public static String qrCodeDecode(File imageFile) {  
        String decodedData = null;  
        QRCodeDecoder decoder = new QRCodeDecoder();  
        BufferedImage image = null;  
        try {  
            image = ImageIO.read(imageFile);  
        } catch (IOException e) {  
            System.out.println("Error: " + e.getMessage());  
        }  
  
        try {  
            decodedData = new String(decoder.decode(new J2SEImage(image)), "utf-8");  
            System.out.println("Output Decoded Data is:" + decodedData);  
        } catch (DecodingFailedException dfe) {  
            System.out.println("Error: " + dfe.getMessage());  
        } catch (UnsupportedEncodingException e) {  
            e.printStackTrace();  
        }  
        return decodedData;  
    }  
  
    public static void main(String[] args) {  
        String FilePath = "d:/qrcode.png";  
        File qrFile = new File(FilePath);  
  
        // 二维码内容  
        String encodeddata = "http://pub.o-banks.com/propertywx/property/scanandpay.do?headid=1";  
        try {  
            QRCodeUtils.qrCodeEncode(encodeddata, qrFile);  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
  
        // 解码  
        String reText = QRCodeUtils.qrCodeDecode(qrFile);  
        System.out.println(reText);  
    }  
}  
  
class J2SEImage implements QRCodeImage {  
    BufferedImage image;  
  
    public J2SEImage(BufferedImage image) {  
        this.image = image;  
    }  
  
    public int getWidth() {  
        return image.getWidth();  
    }  
  
    public int getHeight() {  
        return image.getHeight();  
    }  
  
    public int getPixel(int x, int y) {  
        return image.getRGB(x, y);  
    }  
} 

2、使用google的zxing生成

import java.awt.image.BufferedImage;  
import java.io.File;  
import java.io.IOException;  
import java.nio.file.FileSystems;  
import java.nio.file.Path;  
import java.util.HashMap;  
import java.util.Map;  
  
import javax.imageio.ImageIO;  
  
import com.google.zxing.BarcodeFormat;  
import com.google.zxing.Binarizer;  
import com.google.zxing.BinaryBitmap;  
import com.google.zxing.DecodeHintType;  
import com.google.zxing.EncodeHintType;  
import com.google.zxing.LuminanceSource;  
import com.google.zxing.MultiFormatReader;  
import com.google.zxing.MultiFormatWriter;  
import com.google.zxing.NotFoundException;  
import com.google.zxing.Result;  
import com.google.zxing.WriterException;  
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;  
import com.google.zxing.client.j2se.MatrixToImageWriter;  
import com.google.zxing.common.BitMatrix;  
import com.google.zxing.common.HybridBinarizer;  
  
public class TestZXing {  
  
    public static void main(String[] args) throws Exception {  
        // testEncode();  
        testDecode();  
    }  
  
    /** 
     * 生成二维码 
     *  
     * @throws WriterException 
     * @throws IOException 
     */  
    public static void testEncode() throws WriterException, IOException {  
        String filePath = "D://";  
        String fileName = "zxing.png";  
        String content = "测试zxing生成二维码";  
        int width = 300; // 图像宽度  
        int height = 300; // 图像高度  
        String format = "png";// 图像类型  
        Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();  
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");  
        BitMatrix bitMatrix = new MultiFormatWriter().encode(content,  
                BarcodeFormat.QR_CODE, width, height, hints);// 生成矩阵  
        Path path = FileSystems.getDefault().getPath(filePath, fileName);  
        MatrixToImageWriter.writeToPath(bitMatrix, format, path);// 输出图像  
        System.out.println("输出成功.");  
    }  
  
    /** 
     * 解析二维码 
     */  
    public static void testDecode() {  
        String filePath = "D://zxing.png";  
        BufferedImage image;  
        try {  
            image = ImageIO.read(new File(filePath));  
            LuminanceSource source = new BufferedImageLuminanceSource(image);  
            Binarizer binarizer = new HybridBinarizer(source);  
            BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);  
            Map<DecodeHintType, Object> hints = new HashMap<DecodeHintType, Object>();  
            hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");  
            Result result = new MultiFormatReader().decode(binaryBitmap, hints);// 对图像进行解码  
            System.out.println("图片中内容:  ");  
            System.out.println("author: " + result.getText());  
            System.out.println("图片中格式:  ");  
            System.out.println("encode: " + result.getBarcodeFormat());  
        } catch (IOException e) {  
            e.printStackTrace();  
        } catch (NotFoundException e) {  
            e.printStackTrace();  
        }  
    }  
  
}  

二、SpringMVC实现文件下载

springMVC实现文件下载两种方式:

1、ResponseEntity下载文件

后端代码:

 @RequestMapping(value="/export",method={RequestMethod.POST,RequestMethod.GET})    
    public ResponseEntity<byte[]> export() throws IOException {    
        String path="D:\\workspace\\文件.xlsx";  
        File file=new File(path);  
        HttpHeaders headers = new HttpHeaders();    
        String fileName=new String("你好.xlsx".getBytes("UTF-8"),"iso-8859-1");//为了解决中文名称乱码问题  
        headers.setContentDispositionFormData("attachment", fileName);   
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);   
        return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),    
                                          headers, HttpStatus.CREATED);    
    }    

前端代码:

<a href="*/export.do" >下载</a>  

2.Java通用下载文件(文件流的形式下载)

后端方法工具类(controller引用即可):

 /**
  * 下载文件
  * 
  * @param file
  * @param response
  */
 public static void downloadFile(File file, HttpServletResponse response,
   boolean isDelete) {
  try {
   // 以流的形式下载文件。
   BufferedInputStream fis = new BufferedInputStream(
     new FileInputStream(file.getPath()));
   byte[] buffer = new byte[fis.available()];
   fis.read(buffer);
   fis.close();
   // 清空response
   response.reset();
   OutputStream toClient = new BufferedOutputStream(
     response.getOutputStream());
   response.setContentType("application/octet-stream");
   response.setHeader("Content-Disposition",
     "attachment;filename="
       + new String(file.getName().getBytes("UTF-8"),
         "ISO-8859-1"));
   toClient.write(buffer);
   toClient.flush();
   toClient.close();
   if (isDelete) {
    file.delete(); // 是否将生成的服务器端文件删除
   }
  } catch (IOException ex) {
   ex.printStackTrace();
  }
 }

三、批量二维码下载

二维码单个下载可以采用上面的方式,但是如果一个机构或一个公司需要实现批量下载呢,有人会说循环就行了啊!是的,循环就行了,但是循环下载如何实现呢,循环下载是否够快呢,是否会中断,中断怎么办呢,这些都需要考虑,我这里采用的是先将文件打包成ZIP文件,然后下载ZIP文件,下载完之后删除ZIP临时文件(考虑到磁盘的空间)。

service实现类:

	public void exportHeads(id,HttpServletResponse response) {
		  try {
			   //1.获取项目中文件的文件夹 String path=Thread.currentThread().getContextClassLoader().getResource("").toString();
			   //windows下"/"是表示参数,"\"是表示本地路径。
		       //linux下 "/"表示路径,"\"表示转义,"-"和"--"表示参数。
		       //path=D:\glzf\apache-tomcat-7.0.54\wtpwebapps\education\WEB-INF\\document\111.xls
			   String basePath = filePath.replace("/", FILE_SEPARATOR).replace("\\", FILE_SEPARATOR);
			   //2.获取oss上的文件路径List集合:https://www.xxx.xx/xxx.jpg
			   List<Map<String, Object>> sclList = SchoolMapper.selectByschoolId(id);
			   List<String> urlListAll = new ArrayList<String>();
			   for(Map<String, Object> map:proList){
				   urlListAll.add(basePath+map.get("gradenm")+"#"+map.get("classnm")+".PNG");
			   }
			   //3.定义文件list
			   List<File> files = new ArrayList<File>();
			   //4.定义zip文件名
//			   String fileName = UUID.randomUUID().toString() + ".zip";
			   String fileName = "业主二维码"+UUID.randomUUID().toString()+".zip";
			   //5.将文件从https上下载进服务器的目录,用files装好
			   for (String url : urlListAll) {
				File f = new File(url);  
			    if(!f.exists()){
			     try {
			      f.createNewFile();
			     } catch (IOException e) {
			      e.printStackTrace();
			     }
			    }
			    files.add(f);
			   }
			   //6.创建zip文件
			   ZipUtil.createFile(basePath, fileName);
			   File file = new File(basePath + fileName);
			   FileOutputStream outStream = new FileOutputStream(file);
			   ZipOutputStream toClient = new ZipOutputStream(outStream);
			   //7.将files打包成zip文件
			   ZipUtil.zipFile(files, toClient);
			   toClient.close();
			   outStream.close();
			   //8.下载zip文件,并删除服务器源文件 false:不删除
			   ZipUtil.downloadFile(file, response, false);
			   //9.删除服务器临时的单个文件
			   ZipUtil.delete(basePath + fileName);
//			   for(String t : tempUrlList){
//				   ZipUtil.delete(t);
//			   }
			  } catch (Exception e) {
			   e.printStackTrace();
			  }
	}

util工具类

/**
 * zip压缩文件
 */
public class ZipUtil {
 
 /**
  * 压缩文件列表中的文件
  * 
  * @param files
  * @param outputStream
  * @throws IOException
  */
 public static void zipFile(List<File> files, ZipOutputStream outputStream)
   throws IOException, ServletException {
  try {
   int size = files.size();
   // 压缩列表中的文件
   for (int i = 0; i < size; i++) {
    File file = (File) files.get(i);
    zipFile(file, outputStream);
   }
  } catch (IOException e) {
   throw e;
  }
 }
 /**
  * 将文件写入到zip文件中
  * 
  * @param inputFile
  * @param outputstream
  * @throws Exception
  */
 public static void zipFile(File inputFile, ZipOutputStream outputstream)
   throws IOException, ServletException {
  try {
   if (inputFile.exists()) {
    if (inputFile.isFile()) {
     FileInputStream inStream = new FileInputStream(inputFile);
     BufferedInputStream bInStream = new BufferedInputStream(
       inStream);
     ZipEntry entry = new ZipEntry(inputFile.getName());
     outputstream.putNextEntry(entry);
     final int MAX_BYTE = 10 * 1024 * 1024; // 最大的流为10M
     long streamTotal = 0; // 接受流的容量
     int streamNum = 0; // 流需要分开的数量
     int leaveByte = 0; // 文件剩下的字符数
     byte[] inOutbyte; // byte数组接受文件的数据
     streamTotal = bInStream.available(); // 通过available方法取得流的最大字符数
     streamNum = (int) Math.floor(streamTotal / MAX_BYTE); // 取得流文件需要分开的数量
     leaveByte = (int) streamTotal % MAX_BYTE; // 分开文件之后,剩余的数量
     if (streamNum > 0) {
      for (int j = 0; j < streamNum; ++j) {
       inOutbyte = new byte[MAX_BYTE];
       // 读入流,保存在byte数组
       bInStream.read(inOutbyte, 0, MAX_BYTE);
       outputstream.write(inOutbyte, 0, MAX_BYTE); // 写出流
      }
     }
     // 写出剩下的流数据
     inOutbyte = new byte[leaveByte];
     bInStream.read(inOutbyte, 0, leaveByte);
     outputstream.write(inOutbyte);
     outputstream.closeEntry(); // Closes the current ZIP entry
     // and positions the stream for
     // writing the next entry
     bInStream.close(); // 关闭
     inStream.close();
    }
   } else {
    throw new ServletException("文件不存在!");
   }
  } catch (IOException e) {
   throw e;
  }
 }
 /**
  * 下载文件
  * 
  * @param file
  * @param response
  */
 public static void downloadFile(File file, HttpServletResponse response,
   boolean isDelete) {
  try {
   // 以流的形式下载文件。
   BufferedInputStream fis = new BufferedInputStream(
     new FileInputStream(file.getPath()));
   byte[] buffer = new byte[fis.available()];
   fis.read(buffer);
   fis.close();
   // 清空response
   response.reset();
   OutputStream toClient = new BufferedOutputStream(
     response.getOutputStream());
   response.setContentType("application/octet-stream");
   response.setHeader("Content-Disposition",
     "attachment;filename="
       + new String(file.getName().getBytes("UTF-8"),
         "ISO-8859-1"));
   toClient.write(buffer);
   toClient.flush();
   toClient.close();
   if (isDelete) {
    file.delete(); // 是否将生成的服务器端文件删除
   }
  } catch (IOException ex) {
   ex.printStackTrace();
  }
 }
 
 /**
  *  创建文件
  * @param path
  * @param fileName
  */
 public static void createFile(String path, String fileName) {
  File f = new File(path);
  File file = new File(f, fileName);
  if (!file.exists()) {
   try {
    file.createNewFile();
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
 }
 /** 
  * 删除文件,可以是文件或文件夹 
  * @param fileName 要删除的文件名 
  * @return 删除成功返回true,否则返回false 
  */  
 public static boolean delete(String fileName) {  
   File file = new File(fileName);  
   if (!file.exists()) {  
    System.out.println("删除文件失败:" + fileName + "不存在!");  
    return false;  
   } else {  
    if (file.isFile())  
     return deleteFile(fileName);  
    else  
     return deleteDirectory(fileName);  
   }
 }

 /**
  * 删除单个文件
  *
  * @param fileName
  *            要删除的文件的文件名
  * @return 单个文件删除成功返回true,否则返回false
  */
 public static boolean deleteFile(String fileName) {
     File file = new File(fileName);
     // 如果文件路径所对应的文件存在,并且是一个文件,则直接删除
     if (file.exists() && file.isFile()) {
         if (file.delete()) {
             System.out.println("删除单个文件" + fileName + "成功!");
             return true;
         } else {
             System.out.println("删除单个文件" + fileName + "失败!");
             return false;
         }
     } else {
         System.out.println("删除单个文件失败:" + fileName + "不存在!");
         return false;
     }
 }

}

前端js实现:

        $("#export").click(function(){
        	faildig("是否要批量导出二维码?");
        	$(".layui-layer-btn0").click(function(){
        		var form1 = document.getElementById("schoolinfofrom");
    			form1.action = "export.do";
    			form1.submit();
     		});

 

转载于:https://my.oschina.net/githubhty/blog/1576283

大家好,大家都知道二维码,它是用特定的几何图形按一定规律在平面(二维方向)上分布的黑白相间的图形,是所有信息数据的一把钥匙。在现代商业活动中,可实现的应用十分广泛,如:产品防伪/溯源、广告推送、网站链接、数据下载、商品交易、定位/导航、电子凭证、车辆管理、信息传递、名片交流、wifi共享等。如今智能手机扫一扫(简称313)功能的应用使得二维码更加普遍。今天小编向大家介绍的就是制作二维码的那么一款软件---QR Code Generator。 小编带你看软件: 1.首先我们来看下QR Code Generator这款软件的界面     从图上我们可以大致看出对于第一次操作的人来说相对简单,更值得说明的是这款软件无需安装,所占空间较小 2.下面我们在数据里输入我们所需要的类容:     3.我们点下生成二维码:    4.下面只要保存一下就可以了,我们可以来看下效果图     QR Code Generator 1.1特色: 是一个简单,非常易于使用的图形QR码图像生成器实用程序。这是一个完美的和小尺寸桌面工具实现生成自己的QR码。 首先输入或粘贴任何文本数据字段,如简单的文本数据,短信,电子邮件地址,个人联系资料,电话号码等,如果有必要可以使用一个点击在线URL缩短服务!设置二维图像尺寸后,选择适当的纠错级别,最后点击“生成QR码”按钮。的产生过程是光速度快,后几毫秒的QR码被显示。现在,可以对图像保存到磁盘或可复制到系统剪贴板后使用与其他图像处理软件。 QR Code Generator系统要求操作系统: Windows XP,Vista,Windows7,8(32和64位) 处理器:1 GHz的CPU与支持 SSE2,英特尔奔腾4,奔腾酷睿或Atom,AMD速龙64或以上 系统内存:512 MB 硬盘:5 MB 其他:主动连接互联网   生成时,运行比价缓慢,建议不要打开太多程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值