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);
    }
    }
  • 相关阅读:
    ASP.Net如何用Cookies保存对象
    MS SQL语句优化
    服务消费者
    [模板]线性筛素数(欧拉筛法)
    luogu4159 迷路 (矩阵加速)
    poj1845 sumdiv (因数的和)
    luogu3674 小清新人渣的本愿 (bitset+莫队)
    luogu3621 城池攻占 (倍增)
    luogu3233 世界树 (虚树)
    bzoj4540 序列 (单调栈+莫队+rmq)
  • 原文地址:https://www.cnblogs.com/jabez1992/p/9222854.html
Copyright © 2011-2022 走看看