qrcode 二维码生成

<!-- qrcode -->
        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>core</artifactId>
            <version>3.2.1</version>
        </dependency>
        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>javase</artifactId>
            <version>3.2.1</version>
        </dependency>
        <dependency>
            <groupId>me.ele.openapi</groupId>
            <artifactId>eleme-openapi-sdk</artifactId>
            <version>1.3.13</version>
        </dependency>

package com.fyc.common.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 com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * 生成二维码/条形码工具类
 */
public class QrcodeUtil {
   public enum QRcodeType {
      QR_CODE,//二维码
      CODE_128,//条形码
      other;
   }
   
   private static QrcodeUtil qrcode=null;
   public static final String default_stuffix="png";
   public static final String default_charset="UTF-8";
   
   private QrcodeUtil(){}
   
   /**
    * 单例
    * @return
    */
   public static QrcodeUtil qrcode(){
      if(qrcode==null){
         qrcode=new QrcodeUtil();
      }
      return qrcode;
   }
   
   /**
    * 输出二维码到输出流
    * @param codeType 二维码
    * @param codeData 二维码内容
    * @param height 高度
    * @param width 宽度
    * @param correctionLevel 错误修正级别1,2,3,4
    * @param stream 输出流
    * @return
    * @throws WriterException
    * @throws IOException
    */
   public OutputStream createQRCodeStream(QRcodeType codeType,String codeData,int height, int width,int correctionLevel,OutputStream stream)throws WriterException, IOException {
      Map<EncodeHintType, Object> hintMap = createHintMap(correctionLevel);
      BarcodeFormat type=getBarcodeFormat(codeType);
      BitMatrix matrix=new MultiFormatWriter().encode(new String(codeData.getBytes(default_charset), default_charset),type, width, height, hintMap);
      matrix=deleteWhite(matrix);
      MatrixToImageWriter.writeToStream(matrix, default_stuffix, stream);
      return stream;
   }
   
   /**
    * 生成条形码
    * @param codeType 条形码
    * @param codeData 条形码内容
    * @param height 高度
    * @param width 宽度
    * @param correctionLevel 错误修正基本1,2,3,4
    * @param stream 输出流
    * @return
    * @throws WriterException
    * @throws IOException
    */
   public  OutputStream createBarCodeStream(QRcodeType codeType,String codeData,int height, int width,int correctionLevel,OutputStream stream)throws WriterException, IOException {
       
      int codeWidth = 3 + // start guard
                   (7 * 6) + // left bars
                   5 + // middle guard
                   (7 * 6) + // right bars
                   3; // end guard
       codeWidth = Math.max(codeWidth, width);
      Map<EncodeHintType, Object> hintMap = createHintMap(correctionLevel);
      BarcodeFormat type=getBarcodeFormat(codeType);
      BitMatrix matrix=new MultiFormatWriter().encode(new String(codeData.getBytes(default_charset), default_charset),type, codeWidth, height, hintMap);
      matrix=deleteWhite(matrix);
      MatrixToImageWriter.writeToStream(matrix, default_stuffix, stream);
      return stream;
   }
   /**
    * 参数处理,错误修正级别
    * @param correctionLevel
    * @return
    */
   private  Map<EncodeHintType, Object> createHintMap(int correctionLevel){
      Map<EncodeHintType, Object> hintMap = new HashMap<EncodeHintType, Object>();
      hintMap.put(EncodeHintType.MARGIN, 1);//空白填充
      if(correctionLevel==2){
         hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
      }else if(correctionLevel==3){
         hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.Q);
      }else if(correctionLevel==4){
         hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
      }else{
         hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);  
      }
      return hintMap;
   }  
   
   public BarcodeFormat getBarcodeFormat(QRcodeType codeType){
      if(QRcodeType.QR_CODE==codeType){
         return BarcodeFormat.QR_CODE;
      }else if(QRcodeType.CODE_128==codeType){
         return BarcodeFormat.CODE_128;
      }else{
         return BarcodeFormat.QR_CODE;
      }
   }
   
   /**
    * 删除白边
    * @param matrix
    * @return
    */
    private static BitMatrix deleteWhite(BitMatrix matrix) {
           int[] rec = matrix.getEnclosingRectangle();
           int resWidth = rec[2] + 1;
           int resHeight = rec[3] + 1;

           BitMatrix resMatrix = new BitMatrix(resWidth, resHeight);
           resMatrix.clear();
           for (int i = 0; i < resWidth; i++) {
               for (int j = 0; j < resHeight; j++) {
                   if (matrix.get(i + rec[0], j + rec[1]))
                       resMatrix.set(i, j);
               }
           }
           return resMatrix;
       }
   
} 
例子
ServletOutputStream outputStream = response.getOutputStream();
QrcodeUtil qrcodeUtil = QrcodeUtil.qrcode();
qr.setMobile(qRLink.getMobile());
qr.setCouponVos(couponVos);
qr.setCreateTime(qRLink.getCreateTime());
qr.setValidTime(qRLink.getValidTime());
qr.setKey(qRLink.getKey());
String qrString = URLEncoder.encode(EncryptUtil.aesEncrypt(JSONObject.toJSONString(qr)));
qrcodeUtil.createQRCodeStream(QrcodeUtil.QRcodeType.QR_CODE,qrString, 155, 155, 1, response.getOutputStream());
outputStream.flush();
outputStream.close();

        

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值