码迷,mamicode.com
首页 > 编程语言 > 详细

Java 二维码生成工具类

时间:2014-10-24 01:38:17      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   color   io   os   ar   java   for   

public class QRcodeUtils {
    
    /**
     * 容错率等级 L
     */
    public static final char CORRECT_L = ‘L‘;

    /**
     * 容错率等级 M
     */
    public static final char CORRECT_M = ‘M‘;

    
    /**
     * 容错率等级 Q
     */
    public static final char CORRECT_Q = ‘Q‘;

    
    /**
     * 容错率等级 H
     */
    public static final char CORRECT_H = ‘H‘;
    
    /**
     * 旁白占用比率
     */
    public static final double PADDING_RATIO = 0.05;


    /**
     * 生成二维码
     * @param content 要生成的内容
     * @param size    要生成的尺寸
     * @param versoin 二维码尺寸,取值范围1-40,值越大尺寸越大,可存储的信息越大 
     * @param ecc 二维码排错率,可选L(7%)、M(15%)、Q(25%)、H(30%)
     * @return
     * @throws Exception 
     */
    public static BufferedImage encode(String content, int size, int version, char ecc, BufferedImage logo) throws Exception{
        BufferedImage image = null;
        Qrcode qrcode = new Qrcode();  
        // 设置二维码排错率,可选L(7%)、M(15%)、Q(25%)、H(30%),排错率越高可存储的信息越少,但对二维码清晰度的要求越小  
        qrcode.setQrcodeErrorCorrect(ecc);  
        qrcode.setQrcodeEncodeMode(‘B‘);  
        // 设置设置二维码尺寸,取值范围1-40,值越大尺寸越大,可存储的信息越大  
        qrcode.setQrcodeVersion(version);  
        // 获得内容的字节数组,设置编码格式  
        byte[] contentBytes = content.getBytes("utf-8");  

        double ratio = 3;//默认放大三倍
        int qrcodeSize = 21 + 4 * (version - 1);
        if(size > 0){
            ratio = (double)size / qrcodeSize;
        }
        int intRatio = (int)Math.ceil(ratio);
        
        //旁白
        int padding = (int)Math.ceil(qrcodeSize * intRatio * PADDING_RATIO);
        if(padding < 10){
            padding = 10;
        }
 
        // 图片尺寸  
        int imageSize = qrcodeSize * intRatio + padding * 2;
        image = new BufferedImage(imageSize, imageSize, BufferedImage.TYPE_INT_RGB);  
        Graphics2D gs = image.createGraphics();  
        // 设置背景颜色  
        gs.setBackground(Color.WHITE);  
        gs.clearRect(0, 0, imageSize, imageSize);  
  
        // 设定图像颜色  BLACK  
        gs.setColor(Color.BLACK);
 
        // 输出内容  二维码  
        if (contentBytes.length > 0 && contentBytes.length < 800) {  
            boolean[][] codeOut = qrcode.calQrcode(contentBytes);  
            for (int i = 0; i < codeOut.length; i++) {  
                for (int j = 0; j < codeOut.length; j++) {  
                    if (codeOut[j][i]) {  
                        gs.fillRect(j * intRatio + padding, i * intRatio + padding, intRatio, intRatio); 
                    }  
                }  
            }  
        } else {  
            throw new Exception("QRCode content bytes length = " + contentBytes.length + " not in [0, 800].");  
        }
        
        if(logo != null){
            int logoSize = imageSize / 4;
            int p = ( imageSize - logoSize ) / 2;
            logo = ImageUtils.setRadius(logo);
            gs.drawImage(logo, p,  p, logoSize, logoSize, null);
        }
        
        gs.dispose();
        image.flush();
        
        if(intRatio > ratio){
            image = resize(image, (int)Math.ceil(ratio * imageSize / intRatio ));
        }
        return image;
    }
    
    /**
     * 生成二维码
     * @param content
     * @param size
     * @param version
     * @param ecc
     * @return
     * @throws Exception
     */
    public static BufferedImage encode(String content, int size, int version, char ecc) throws Exception{
        return encode(content, size, version, ecc, null);
    }
    
    /**
     * 生成二维码
     * @param content
     * @param size
     * @return
     * @throws Exception 
     */
    public static BufferedImage encode(String content, int size) throws Exception{
        return encode(content, size, 9, CORRECT_Q);
    }
    
    /**
     * 生成二维码
     * @param content
     * @param size
     * @param version
     * @return
     */
    public static BufferedImage encode(String content, int size, int version) throws Exception{
        return encode(content, size, version, CORRECT_Q);
    }
    
    /**
     * 生成二维码
     * @param content
     * @param size
     * @param logo
     * @return
     */
    public static BufferedImage encode(String content, int size, BufferedImage logo) throws Exception{
        return encode(content, size, 9, CORRECT_Q, logo);
    }
    
    /**
     * 生成二维码
     * @param content
     * @param size
     * @param ecc
     * @return
     */
    public static BufferedImage encode(String content, int size, char ecc) throws Exception{
        return encode(content, size, 9, ecc);
    }
    
    /**
     * 生成二维码
     * @param content
     * @return
     */
    public static BufferedImage encode(String content) throws Exception{
        return encode(content, 0, 9, CORRECT_Q);
    }
    
    /**
     * 解析二维码
     * @throws UnsupportedEncodingException 
     * @throws DecodingFailedException 
     */
    public static String decode(BufferedImage bufferedImage) throws DecodingFailedException, UnsupportedEncodingException{
         QRCodeDecoder decoder = new QRCodeDecoder();  
         return new String(decoder.decode(new MyQRCodeImage(bufferedImage)), "utf-8");   
    }
    
    /**
     * 图片缩放
     */
    private static BufferedImage resize(BufferedImage image, int size){
        BufferedImage bufferedImage = new BufferedImage(size, size, BufferedImage.TYPE_INT_RGB);  
        Graphics2D gs = bufferedImage.createGraphics();  
        gs.setBackground(Color.WHITE);  
        gs.clearRect(0, 0, size, size);  
        gs.setColor(Color.BLACK);
        gs.fillRect(0, 0, size, size);
        gs.drawImage(image, 0, 0, size, size, null);  
        gs.dispose();  
        image.flush();
        return bufferedImage;
    }
    
}

 

Java 二维码生成工具类

标签:des   style   blog   color   io   os   ar   java   for   

原文地址:http://www.cnblogs.com/rubekid/p/4047262.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!