二维码的创建详解

二维码的创建及使用

 今天小编在家里闲着无聊搞了一个简单的二维码创建,分享给大家:
 需要导入两个jar包:core-3.1.0.jar  Qrcode_swetake.jar
 接下来就是代码喽!!!

代码详解:

    package cd.xdl.qr;

    import java.awt.Color;
    import java.awt.Graphics2D;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.UnsupportedEncodingException;

    import javax.imageio.ImageIO;

    import com.swetake.util.Qrcode;

    public class CreateQR {

        public void encoderQRCode(String content, String imgPath){
            try {
                Qrcode qrcodeHandler = new Qrcode();
                // 设置二维码排错率,可选L(7%)、M(15%)、Q(25%)、H(30%),排错率越高可存储的信息越少,但对二维码清晰度的要求越小
                qrcodeHandler.setQrcodeErrorCorrect('M');
                //编码模式:Numeric数字 Binary二进制 Kanji汉字 Alphanumeric英文字母
                qrcodeHandler.setQrcodeEncodeMode('B');
                qrcodeHandler.setQrcodeVersion(5);
                byte[] contentBytes = content.getBytes("gb2312");
                //创建画布
                BufferedImage bufImg = new BufferedImage(600, 600, BufferedImage.TYPE_INT_RGB);
                //创建画笔
                Graphics2D gs = bufImg.createGraphics();
                //设置背景颜色
                gs.setBackground(Color.WHITE);
                gs.clearRect(0, 0, 600, 600);
                //设置图像颜色
                gs.setColor(Color.BLACK);
                //设置偏移量,不设置可能导致解析出错
                int pixoff = 2;
                //输出内容  
                if(contentBytes.length > 0 && contentBytes.length < 800){
                    boolean[][] codeOut = qrcodeHandler.calQrcode(contentBytes);
                    for (int i = 0; i < codeOut.length; i++) {
                        for (int j = 0; j < codeOut.length; j++) {
                            if(codeOut[j][i]){
                                gs.fillRect(j*16+pixoff, i*16+pixoff, 16, 16);
                            }
                        }
                    }
                }else{

                }
                gs.dispose();;
                bufImg.flush();
                File imgFile = new File(imgPath);
                //生成二维码图片
                ImageIO.write(bufImg, "png", imgFile);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }


        public static void main(String[] args) {
            String imgPath = "D:\\"+"MyQRCode"+".png";
            String content = "https://www.baidu.com";
            CreateQR handle = new CreateQR();
            handle.encoderQRCode(content, imgPath);
            System.out.println("imgPath:"+imgPath);
        }
    }

注意:

上述代码中的content即二维码创建的内容可以是文字,也可以是网址哦~网址的话会直接跳转过去的!!!
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值