二维码生成方式有两种,一种是java,一种是js.其中js生成比较流行.下面先说java的生成方式:
(一)1. 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生成方式:
生成:
读取:
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生成:
地址:
How to Use ItLet me walk you thru it. First include it in your webpage with the usual script tagThen create a DOM element which gonna contains the generated qrcode image. Lets say a div Then you add the qrcode in this container byjquery('#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"});
==========================================================
视频地址: