用QRCode生成二维码

来源:互联网 发布:ipad如何禁止软件上网 编辑:程序博客网 时间:2024/05/04 13:05
用QRCode生成与解析二维码,需要用到的jar包Qrcode_A.jar,qrcode_B.jar  jar包下载地址   链接:http://pan.baidu.com/s/1pK9WLw3 密码:8qf5
//生成二维码
public static void QrcodeGet(){        Qrcode code=new Qrcode();        code.setQrcodeErrorCorrect('M');//纠错等级        code.setQrcodeEncodeMode('B');//N代表数字A 代表 a-z  B代表其他字符        code.setQrcodeVersion(10);//调整生成二维码的高度和宽度        String str="www.baidu.com";        int width=300;        int height=300;        BufferedImage image=new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);        Graphics2D gs=image.createGraphics();//        gs.setBackground(Color.white);//画板颜色        gs.setColor(Color.BLACK);//画笔颜色        gs.clearRect(0, 0, width, height);        int pixoff=2;//偏移量        byte[]d=str.getBytes();        if(d.length>0&&d.length<120){            boolean[][]s=code.calQrcode(d);            for(int i=0;i<s.length;i++){                for(int j=0;j<s.length;j++){                    if(s[j][i]){                        //前两个参数 是据左 ,据上的距离,后面两个参数,是每一次画的宽度                        gs.fillRect(j*3+pixoff+75, i*3+pixoff+75, 3, 3);                    }                }            }            gs.dispose();            image.flush();            try {                ImageIO.write(image, "png", new File("F:/二维码.png"));            } catch (IOException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }                    }                    }    //解析二维码    public static void QrcodeJieX(){        //指定文件路径        File file=new File("F:/二维码.png");        String result="";        try {            //读取验证码图片            BufferedImage bufferedImage=ImageIO.read(file);             //调用方法            QRCodeDecoder codeDecoder=new QRCodeDecoder();            result = new String(codeDecoder.decode(new MyQRCodeImage(bufferedImage)),"gb2312");        } catch (Exception e) {            e.printStackTrace();        }                 System.out.println("二维码的内容为:"+result);           }

0 0
原创粉丝点击