1. 程式人生 > >三種方式實現二維碼(java)

三種方式實現二維碼(java)

一. 通過使用zxing方式實現: 
jar準備: https://github.com/zxing/zxing 下載原始碼,將core/src/main/Java/下的所有檔案和javase/src/main/java/下的所有檔案一起打成jar檔案zxing.jar

建立二維碼:

@SuppressWarnings({"rawtypes", "unchecked"})
    private static void createZxing() throws WriterException, IOException {
        int width=300;
        int hight=300;
        String format="png"
; String content="www.baidu.com"; HashMap hints=new HashMap(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);//糾錯等級L,M,Q,H hints.put(EncodeHintType.MARGIN, 2); //邊距 BitMatrix bitMatrix=new
MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, hight, hints); Path file=new File("D:/download/imag.png").toPath(); MatrixToImageWriter.writeToPath(bitMatrix, format, file); }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

讀取二維碼:

private static void readZxing() throws IOException, NotFoundException {
        MultiFormatReader read = new
MultiFormatReader(); File file=new File("D:/download/imag.png"); BufferedImage image=ImageIO.read(file); Binarizer binarizer=new HybridBinarizer(new BufferedImageLuminanceSource(image)); BinaryBitmap binaryBitmap=new BinaryBitmap(binarizer); Result res=read.decode(binaryBitmap); System.out.println(res.toString()); System.out.println(res.getBarcodeFormat()); System.out.println(res.getText()); }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

二. 使用QRCode方式實現二維碼

jar包準備:Qrcode_A.jar,qrcode_B.jar 
建立二維碼:

    private static void createORcode() throws UnsupportedEncodingException, IOException {
        Qrcode qrcode=new Qrcode();
        qrcode.setQrcodeErrorCorrect('M'); //糾錯等級L M Q H
        qrcode.setQrcodeEncodeMode('B'); //N:數字,A:a-Z ,B:其他字元
        int version = 7;
        qrcode.setQrcodeVersion(version);
        String qrData="www.baidu.com";
        byte data[]=qrData.getBytes("gb2312");
        int width = 67+12*(version-1);
        int hight=67+12*(version-1);
        BufferedImage bufferedImage=new BufferedImage(width, hight, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics2d=bufferedImage.createGraphics();
        graphics2d.setBackground(Color.white);
        graphics2d.setColor(Color.black);
        graphics2d.clearRect(0, 0, width, hight);
        int pixoff=2; //偏移量
        if (data.length>0 && data.length<120) {
            boolean[][] s=qrcode.calQrcode(data);
            for (int i = 0; i < s.length; i++) {
                for (int j = 0; j < s.length; j++) {
                    if (s[j][i]) {
                        graphics2d.fillRect(j*3+pixoff, i*3+pixoff, 3, 3);
                    }
                }
            }
        }
        graphics2d.dispose();
        bufferedImage.flush();
        ImageIO.write(bufferedImage, "png", new File("D:/download/imag.png"));
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

讀取二維碼:兩種方式:1.上訴readZxing()的方式,2.如下

   private static void readQRcode() throws IOException, NotFoundException {
        File file=new File("D:/download/imag.png");
        BufferedImage bufferedImage=ImageIO.read(file);
        QRCodeDecoder codeDecoder=new QRCodeDecoder();
        String res=new String(codeDecoder.decode(new MyQRCodeImage(bufferedImage)),"gb2312");
        System.out.println(res);
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

三. 使用js外掛實現二維碼

js檔案準備:jQuery.min.js,jquery.qrcode.min.js 
匯入js檔案,然後編寫如下程式碼,訪問該頁面。

    <div id="qrcode"></div>
    <script type="text/javascript">
        $("#qrcode").qrcode("www.baidu.com");
    </script>