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

java 二维码

时间:2017-10-09 13:14:23      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:mon   character   生成二维码   bytes   equal   red   显示   stat   jar包   

生成二维码用的谷歌的jar包

<!-- 谷歌二维码 -->
    <dependency>  
       <groupId>com.google.zxing</groupId>  
       <artifactId>core</artifactId>  
       <version>3.0.0</version>  
       </dependency>  
       <dependency>  
       <groupId>com.google.zxing</groupId>  
       <artifactId>javase</artifactId>  
       <version>3.0.0</version>  
    </dependency>
/**
     * 生成二维码 base64格式图片二维码
     * @param code  信息链接
     * @param logPath log地址
     * @return
     * @throws WriterException 
     * @throws IOException 
     */
    public static String qrCode(String code, String logPath) throws WriterException, IOException{
        
        MultiFormatWriter writer = new MultiFormatWriter();
        Map<EncodeHintType, Object> hintMap = new HashMap<EncodeHintType, Object>();
        //设置字符编码
        hintMap.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        
        BitMatrix matrix = writer.encode(code, BarcodeFormat.QR_CODE, 300, 300, hintMap);
        BufferedImage bufferedImage = toBufferedImage(matrix);
        if(logPath != null && !"".equals(logPath)){
            /**
             * 在二维码中添加log
             */
            URL url = new URL(Const.IMAGE_PATH + logPath);  
            DataInputStream dataInputStream = new DataInputStream(url.openStream());  
            BufferedImage logo = ImageIO.read(dataInputStream);  
            Graphics2D g = bufferedImage.createGraphics();  
            //考虑到LOGO照片贴到二维码中,建议大小不要超过二维码的1/5;  
            int width = bufferedImage.getWidth() / Const.CODE_SIZE;  
            int height = bufferedImage.getHeight() / Const.CODE_SIZE;  
            //LOGO起始位置,此目的是为LOGO居中显示  
            int x = (bufferedImage.getWidth() - width) / 2;  
            int y = (bufferedImage.getHeight() - height) / 2;  
            //绘制图  
            g.drawImage(logo, x, y, width, height, null);  
            //给LOGO画边框  
            //构造一个具有指定线条宽度以及 cap 和 join 风格的默认值的实心 BasicStroke  
            g.setStroke(new BasicStroke());  
            g.setColor(Color.WHITE);  
            g.drawRect(x, y, width, height);
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ImageIO.write(bufferedImage, Const.FILE_TYPE.substring(1, Const.FILE_TYPE.length()) , out);
        //转二进制
        byte[] bytes = out.toByteArray();
        return Const.BASE64 + Base64.encode(bytes);
    }
/**
     * BitMatrix 转成  BufferedImage
     * @param matrix BitMatrix
     * @return
     */
    private static BufferedImage toBufferedImage(BitMatrix matrix) {
        Integer width = matrix.getWidth();
        Integer height = matrix.getHeight();
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                 image.setRGB(x, y, matrix.get(x, y) ? Const.BLACK : Const.WHITE);
            }
        }
        return image;
    }
package com.yuanxinxinxi.yuanxinbuluo.common;

/**
 * 
 * 文件处理长量
 */
public class Const {
   /**
     * 压缩图片的后缀
     */
    public final static String FILE_TYPE = ".PNG";
    /**
     * 压缩图片的最低像素
     */
    public final static Integer BASE = 1280;
    /**
     * 白色
     */
    public static final Integer BLACK = 0xFF000000;
    /**
     * 黑色
     */
    public static final Integer WHITE = 0xFFFFFFFF;
    /**
     * base64前缀
     */
    public static final String BASE64 = "data:image/png;base64,";/**
     * 二维码的大小
     */
    public static final Integer CODE_SIZE = 5;
}

 

java 二维码

标签:mon   character   生成二维码   bytes   equal   red   显示   stat   jar包   

原文地址:http://www.cnblogs.com/xxiaomuma/p/7640424.html

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