• Qrcode生成二维码工具类


    Qrcode生成二维码工具类

    package com.qrcode.util;
    
    import java.awt.Color;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.image.BufferedImage;
    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.ObjectOutputStream;
    
    import javax.imageio.ImageIO;
    
    import com.swetake.util.Qrcode;
    
    /**
     * 生成二维码工具类
     * @author pengfei.xiong
     */
    public class QrcodeUtil {
        /**
         * 二维码生成核心方法
         * @param content 二维码存储的内容  
         * @param imgPath 二维码图片路径
         * @param logoPath 中间logo的图片路径
         * @throws Exception 异常
         */
         public static void  getQrocodeImg(String content,String imgPath,String logoPath) 
    
    throws Exception{
             //实例化qecode对象
             Qrcode qrcode = new Qrcode();
             //设置排错率15%空间 错误信息
             qrcode.setQrcodeErrorCorrect('M');
             //二维码存储内容的编码集
             qrcode.setQrcodeEncodeMode('B');
             //设置二维码的版本 1-40 值越大尺寸越大,可存储的信息越大
             qrcode.setQrcodeVersion(15);
    
             //准备绘制二维码 画画条件:画板 画笔
             int width = 235;     //宽
             int height = 235;   //高
             //创建画板
             BufferedImage image = new BufferedImage(width, height, 
    
    BufferedImage.TYPE_INT_RGB);
             //绘制工具
             Graphics2D gs =image.createGraphics();
    
             //设置背景颜色
             gs.setBackground(Color.white);
             //开始绘制矩形
             gs.clearRect(0, 0, width, height);
             //设置前景色
             gs.setColor(Color.black);
    
             //接受内容
            // byte[] codeOut = objectToByte(content);
             byte[] codeOut;
             codeOut = content.getBytes("utf-8");
    
    
             //通过byte数组获取boolean 类型二维码
             boolean[][] code = qrcode.calQrcode(codeOut);
    
             //遍历这个二维数组
             for (int i = 0; i < code.length; i++) {
                for (int j = 0; j < code.length; j++) {
                    //如果是真的就涂黑
                    if (code[i][j]) {
                        gs.fillRect(j*3+2, i*3+2, 3, 3);
                    }
                }
            }
    
    
             //加入中间logo
             Image img= ImageIO.read(new File(logoPath));
             //80,80是距离gs两个边的距离,55,55是中间logo的大小
             gs.drawImage(img,80,80,75,75,null);
    
            //释放资源
             image.flush();
             gs.dispose();
             image.flush();
    
             //写入指定路径
             ImageIO.write(image, "png", new File(imgPath));
             System.out.println("二维码生成成功!");
         }
    
         public static void main(String[] args) {
            try {
                /*Student stu = new Student("test", 11);
                stu.setId(10);*/
                getQrocodeImg("http://baidu.com", 
    
    "D:/Qrcode/qrcode.png","src/com/qrcode/util/images/p1.jpg");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
         /**
          * Object转换为byte[]
          * @param object
          * @return byte[]数组
          */
         public static byte[] objectToByte(java.lang.Object object){
             byte[] bytes = null;
             ByteArrayOutputStream bo = new ByteArrayOutputStream();
             try {
                 ObjectOutputStream oo = new ObjectOutputStream(bo);
                oo.writeObject(object);
                bytes = bo.toByteArray();
                bo.close();
                oo.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return bytes;
    
         }
    }
    

    这个工具类生成的二维码目前只能存储字符串和网页路径,要存更多内容和对象的话,自己改下,也希望能大神能贴出更好的!需要导入的包有Qrcode_swetake.jar,自行百度找一个
    找不到的话这是jar链接 : http://download.csdn.net/download/xpf_user/10029120

    勿忘初心 得过且过
  • 相关阅读:
    yum安装mysql5.7和8.0版本
    centos7永久修改主机名
    centos7 安装 oracle jdk(非open-jdk)
    yum安装Nginx
    该文件没有与之关联的程序来执行该操作。请安装应用,若已经安装应用,请在"默认应用设置
    java 通过url地址 获取视频时长
    获取视频属性工具类
    redis看门狗
    mysql中group_concat函数的使用
    mybatis-config.xml配置
  • 原文地址:https://www.cnblogs.com/xpf1009/p/9227312.html
Copyright © 2020-2023  润新知