生成二维码



QRCode二维码符号QR Code码是由日本Denso公司于1994年9月研制的一种矩阵二维码符号,它具有一维条码及其它二维条码所具有的信息容量大、可靠性高、可表示汉字及图象多种文字信息、保密防伪性强等优点。

前期准备

下载QRCode的jar包提取码:wv4a

代码演示

public class QRCode {
    public static void main(String[] args) throws IOException {
        Qrcode x=new Qrcode();
        x.setQrcodeErrorCorrect('M');
        x.setQrcodeEncodeMode('B');
        x.setQrcodeVersion(7);
        String qrData="https://itmanmzt.github.io/";

        int width=67+12*(7-1);
        int height=67+12*(7-1);
        BufferedImage bufferedImage=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
        Graphics2D gs=bufferedImage.createGraphics();
        gs.setBackground(Color.WHITE);
        gs.setColor(Color.BLACK);
        gs.clearRect(0,0,width,height);

        int pixoff=2;
        byte[] d=qrData.getBytes("gb2312");
        if(d.length>=0 && d.length<120){
            boolean[][] s=x.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,i*3+pixoff,3,3);
                    }
                }
            }
        }
        gs.dispose();
        bufferedImage.flush();

        ImageIO.write(bufferedImage,"png",new File("D:/qrcode.png"));
    }
}

结果展示

  

有Marin的地方就有你的收获