zoukankan      html  css  js  c++  java
  • java zxing生成二维码

    package zxing.test;
    
    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.ByteArrayOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import org.apache.commons.codec.binary.Base64;
    
    /**
     * @作者 yan
     * @创建日期 
     * @版本 V1.0
     * @描述 
     */
    public class QrCodeUtil {
        
        /**
         * 生成二维码,返回二维码Base64编码
         * @param content
         * @param size
         * @param imgFormt
         * @return 
         */
        public static String createQrCodeBase64(String content, int size, String imgFormt){
            ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
            
            try {
                Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
                hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
                hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
                BitMatrix bitMatrix = new MultiFormatWriter().encode(content,BarcodeFormat.QR_CODE, size, size, hints);
                MatrixToImageWriter.writeToStream(bitMatrix, imgFormt, baos);
                
            } catch (WriterException ex) {
                Logger.getLogger(QrCodeUtil.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(QrCodeUtil.class.getName()).log(Level.SEVERE, null, ex);
            }
            
            byte result [] = baos.toByteArray();
            
            return Base64.encodeBase64String(result);
        }
    
        /**
         * 生成二维码,保存到output
         * @param output
         * @param content
         * @param size
         * @param imgFormt
         * @return 
         */
        public static boolean createQrCode(OutputStream output, String content, int size, String imgFormt){
            
            try {
                Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
                hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
                hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
                BitMatrix bitMatrix = new MultiFormatWriter().encode(content,BarcodeFormat.QR_CODE, size, size, hints);
                MatrixToImageWriter.writeToStream(bitMatrix, imgFormt, output);
                
                return true;
            } catch (WriterException ex) {
                Logger.getLogger(QrCodeUtil.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(QrCodeUtil.class.getName()).log(Level.SEVERE, null, ex);
            }
            
            return false;
        }
    }

    jar:

    zxing-core-3.2.0.jar

    zxing-javase-3.2.0.jar

  • 相关阅读:
    Windows下Subversion配置管理员指南
    asp.net HttpHand和HttpModule的详细解释,包括Asp.Net对Http请求的处理流程。
    SQL Server 2000不能远程连接的问题
    ASP通用分页类[强]
    不错的站长工具网址(不断收集中....)
    函数的四种调用模式
    HDOJ 3943 Kth Nya Number(数位DP)
    HDOJ 2196 Computer
    HDOJ 3695 Computer Virus on Planet Pandora (AC自动机)
    HDOJ 4277 USACO ORZ (搜索+剪枝)
  • 原文地址:https://www.cnblogs.com/yshyee/p/8611896.html
Copyright © 2011-2022 走看看