二维码Java和Jquery生成方式

二维码生成方式有两种,一种是java,一种是js.其中js生成比较流行.下面先说java的生成方式:

(一)1. zxing生成方式:

地址:https://github.com/zxing/

int width = 300;  //设置宽度
int height = 300;  //设置高度
String format = "png";  //图片的格式
String content = "http://www.baidu.com";  //图片内容

// 定义二维码参数
HashMap hm = new HashMap<>();
hm.put(EncodeHintType.CHARACTER_SET, "utf-8");
hm.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
hm.put(EncodeHintType.MARGIN, 2);

try {
BitMatrix bm = new MultiFormatWriter().encode(content,BarcodeFormat.QR_CODE, width, height,hm);
Path file = new File("D:/img.png").toPath();
MatrixToImageWriter.writeToPath(bm, format, file);

} catch (Exception e) {

e.printStackTrace();
}

2.zxing解析方式:

MultiFormatReader formatReader=new MultiFormatReader();
            File file=new File("D:/img.png");
            BufferedImage image=ImageIO.read(file);
            BinaryBitmap binaryBitmap=new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));
            // 定义二维码参数
            HashMap hm = new HashMap<>();
            hm.put(EncodeHintType.CHARACTER_SET, "utf-8");
            Result result=formatReader.decode(binaryBitmap,hm);
            System.out.println("解析结果为:"+result);

(二)

1.QRCode生成方式:

生成:http://www.swetake.com/qrcode/index-e.html

读取:https://osdn.jp/projects/qrode/

 

Qrcode qrCode = new Qrcode();
        qrCode.setQrcodeErrorCorrect('M');// 纠错等级
        qrCode.setQrcodeEncodeMode('B');// N代表数字,A代表a-Z,B代表其他的字符
        qrCode.setQrcodeVersion(7);// 版本
        String str = "http://www.baidu.com";
        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[] bytes = str.getBytes("gb2312");
        if (bytes.length > 0 && bytes.length < 120) {
            boolean[][] s = qrCode.calQrcode(bytes);
            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:/imag1.png"));

2.QRCode解析方式:

public class Utils implements QRCodeImage{
    
    BufferedImage bufferedImage;
    
    public Utils(BufferedImage bufferedImage){
        this.bufferedImage=bufferedImage;
    }
    
    @Override
    public int getHeight() {
        
        return bufferedImage.getHeight();
    }

    @Override
    public int getPixel(int arg0, int arg1) {
        
        return bufferedImage.getRGB(arg0, arg1);
    }

    @Override
    public int getWidth() {
        
        return bufferedImage.getWidth();
    }

}
File file =new File("D://imag1.png");
        BufferedImage bufferedImage=ImageIO.read(file);
        QRCodeDecoder codeDecoder=new QRCodeDecoder();
        String result=new String(codeDecoder.decode(new Utils(bufferedImage)),"gb2312");
        
        System.out.println(result);

(三)js生成:

地址:https://github.com/jeromeetienne/jquery-qrcode

How to Use It

Let me walk you thru it. First include it in your webpage with the usual script tag

<script type="text/javascript" src="jquery.qrcode.min.js"></script>
Then create a DOM element which gonna contains the generated qrcode image. Lets say a div

<div id="qrcode"></div>
Then you add the qrcode in this container by

jquery('#qrcode').qrcode("this plugin is great");
This is it. see it live.

You can set the height and width of the generated qrcode:

jquery('#qrcode').qrcode({width: 64,height: 64,text: "size doesn't matter"});

 ==========================================================

 视频地址:https://www.imooc.com/video/10314

 

转载于:https://www.cnblogs.com/yangh4105/p/8394711.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值