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);
    }
    }
  • 相关阅读:
    HDU
    稀疏表(ST / Sparse Table)
    HDU
    HDU
    树状数组
    用 BitArray 来编写埃拉托斯特尼筛法
    BitArray 内置是逆序存储, 因此要自行实现正序输出
    位运算,位移,窗体
    按位运算,窗体程序,And,Or,Xor
    基数排序
  • 原文地址:https://www.cnblogs.com/jabez1992/p/9222854.html
Copyright © 2011-2022 走看看