java 二维码

生成二维码用的谷歌的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;
}

 

转载于:https://www.cnblogs.com/xxiaomuma/p/7640424.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值