zoukankan      html  css  js  c++  java
  • 二维码---生成并下载二维码

    package com.epalmpay.util;

    import com.google.zxing.BarcodeFormat;
    import com.google.zxing.EncodeHintType;
    import com.google.zxing.MultiFormatWriter;
    import com.google.zxing.WriterException;
    import com.google.zxing.client.j2se.MatrixToImageWriter;
    import com.google.zxing.common.BitMatrix;
    import org.apache.commons.io.output.ByteArrayOutputStream;
    import org.springframework.http.HttpHeaders;
    import org.springframework.http.HttpStatus;
    import org.springframework.http.MediaType;
    import org.springframework.http.ResponseEntity;

    import javax.imageio.ImageIO;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.util.HashMap;
    import java.util.Map;

    /**
    * Created by wucongpeng on 2017/6/11.
    */
    public class DownLoadUtil {

    /**
    * 生成并下载二维码
    * @param url 二维码对于URL
    * @param width 二维码宽
    * @param height 二维码高
    * @param format 二维码格式
    * @return
    * @throws WriterException
    * @throws IOException
    */
    public static ResponseEntity<byte[]> getResponseEntity(String url, String fileName, int width, int height, String format) throws WriterException, IOException {

    // String fileName = url.substring(url.lastIndexOf('/') + 1);

    Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
    hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
    BitMatrix bitMatrix = new MultiFormatWriter().encode(url,
    BarcodeFormat.QR_CODE, width, height, hints);// 生成矩阵
    //将矩阵转为Image
    BufferedImage image = MatrixToImageWriter.toBufferedImage(bitMatrix);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ImageIO.write(image, format, out);//将BufferedImage转成out输出流
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
    headers.setContentDispositionFormData("attachment", new String(fileName.getBytes("utf-8"), "ISO8859-1"));
    return new ResponseEntity<byte[]>(out.toByteArray(),
    headers, HttpStatus.CREATED);
    }
    }
  • 相关阅读:
    【转】[fix] Wireshark error: There are no interfaces on which a capture can be done. on Mac OS X
    【转载】Linux 文件系统的目录结构
    postgreSQL使用
    [转载] Linux启动过程详解-《别怕Linux编程》之八
    冒泡排序
    java值类型和引用类型
    冒泡排序法与二分查找法
    关系型数据库
    SQList的建表并添加数据练习
    数据存储——SQLite数据库存储
  • 原文地址:https://www.cnblogs.com/jabez1992/p/9222854.html
Copyright © 2011-2022 走看看