java 生成二维码

一、导入jar包 

        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>core</artifactId>
            <version>3.3.3</version>
        </dependency>

        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>javase</artifactId>
            <version>3.3.3</version>
        </dependency>

 

二、创建工具类

/**
     * 生成二维码
     * @param imgPath 保存地址
     * @param imgType 图片格式
     * @param content 隐藏内容
     * @param size    高宽
     * @throws Exception io异常等
     */
    public static void generateImg(String imgPath,String imgType,String content,int size,String logoPath)throws Exception {
        //配置二维码
        Hashtable<EncodeHintType,Object> hint = new Hashtable<>();
        //设置排错率
        hint.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        //设置编码格式
        hint.put(EncodeHintType.CHARACTER_SET,"utf-8");
        //设置边距
        hint.put(EncodeHintType.MARGIN,1);
        //获取内容
        BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE,size,size,hint);
        //内存画板
        BufferedImage img = new BufferedImage(size,size,BufferedImage.TYPE_INT_RGB);
        //如果没有logo地址那么二维码则不会有logo
        for (int x=0;x<size;x++){
            for (int j=0;j<size;j++){
                //循环构建
                img.setRGB(x,j,(bitMatrix.get(x,j)? 0xFF000000  :0xFFFFFFFF));
            }
        }

        if(!"".equals(logoPath)&&null!=logoPath){
            img = generateLogo(img,logoPath);
        }

        //创建图片文件流
        File file = new File(imgPath);
        //生成文件
        ImageIO.write(img,imgType,file);
    }


     /**
     * 生成logo
     * @param img 二维码图片流
     * @param logoPath logo图片地址
     * @return
     * @throws Exception
     */
    private static BufferedImage generateLogo(BufferedImage img,String logoPath)throws Exception{
        //将二维码加载到画板上(类似于背景壁纸)
        Graphics2D graphics2D = img.createGraphics();
        //将logo文件加载到内存
        BufferedImage logoImg = ImageIO.read(new File(logoPath));
        int height=img.getHeight();
        int width=img.getWidth();
        //绘制logo
        graphics2D.drawImage(logoImg,width*2/5,height*2/5,width*1/5,height*1/5,null);
        //创建一号画笔 用于绘制外边距内容
        BasicStroke marginStrock = new BasicStroke(5,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);
        //将画笔与画板建立联系
        graphics2D.setStroke(marginStrock);
        //创建一个logo外边框(正方形)
        RoundRectangle2D.Float marginRound = new RoundRectangle2D.Float(width*2/5,height*2/5,width*1/5,height*1/5,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);
        //设置logo外边框颜色
        graphics2D.setColor(Color.white);
        //绘制logo外边框
        graphics2D.draw(marginRound);

        //创建二号画笔 用户绘制边框
        BasicStroke borderStrock = new BasicStroke(1,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);
        //将画笔与画板建立联系
        graphics2D.setStroke(borderStrock);
        //创建一个logo边框(正方形)
        RoundRectangle2D.Float borderRound = new RoundRectangle2D.Float(width*2/5+2,height*2/5+2,width*1/5-4,height*1/5-4,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);
        //设置logo边框颜色
        graphics2D.setColor(Color.gray);
        //绘制logo外边框
        graphics2D.draw(borderRound);
        //释放内存
        graphics2D.dispose();
        img.flush();
        return img;
    }

三、获取二维码内容

/**
     * 解析二维码
     * @param imgPath 二维码路径
     * @return 解析的文件
     */
    public static String readContent(String imgPath){
        try {
            File file=new File(imgPath);
            BufferedImage img=ImageIO.read(file);
            MultiFormatReader multiFormatReader=new MultiFormatReader();
            LuminanceSource luminanceSource=new BufferedImageLuminanceSource(img);
            Binarizer binarizer=new HybridBinarizer(luminanceSource);
            BinaryBitmap binaryBitmap=new BinaryBitmap(binarizer);
            Map map=new HashMap();
            map.put(EncodeHintType.CHARACTER_SET,"utf-8");
            Result result=multiFormatReader.decode(binaryBitmap,map);
            return result.getText();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NotFoundException e) {
            e.printStackTrace();
        }
        return "";
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值