1. 程式人生 > >Java實現二維碼製作

Java實現二維碼製作

二維碼概述

二維碼又稱QR Code,QR全稱Quick Response,是用某種特定的幾何圖形按一定規律在平面(二維方向上)分佈的黑白相間的圖形記錄資料符號資訊的。

zxing

1、引入pom檔案

<!-- zxing -->
<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>core</artifactId
>
<version>3.2.1</version> </dependency> <dependency> <groupId>com.google.zxing</groupId> <artifactId>javase</artifactId> <version>3.2.1</version> </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

2、生成和讀取二維碼

/**
 * 生成二維碼
 */
public classCreateQrCode
{
public static void main(String[] args) { int width = 300; int height = 300; String format = "png"; String content = "www.shuai.com"; //定義二維碼的引數 HashMap map = new HashMap(); //設定編碼 map.put(EncodeHintType.CHARACTER_SET, "utf-8"); //設定糾錯等級
map.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M); map.put(EncodeHintType.MARGIN, 2); try { //生成二維碼 BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height); Path file = new File("E:/develop/qrcode.png").toPath(); MatrixToImageWriter.writeToPath(bitMatrix, format, file); } catch (WriterException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } /** * 讀取二維碼 */ public classReadQrCode { public static void main(String[] args) { try { MultiFormatReader multiFormatReader = new MultiFormatReader(); File file = new File("E:/develop/qrcode.png"); BufferedImage image = ImageIO.read(file); //定義二維碼引數 Map hints = new HashMap(); hints.put(EncodeHintType.CHARACTER_SET,"utf-8"); //獲取讀取二維碼結果 BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image))); Result result = multiFormatReader.decode(binaryBitmap, hints); System.out.println("讀取二維碼: " + result.toString()); System.out.println("二維碼格式: " + result.getBarcodeFormat()); System.out.println("二維碼內容: " + result.getText()); } catch (NotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
  • 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
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60

qrcodejar

1、引入qrcode的jar 2、生成二維碼,解析二維碼

/**
 * 二維碼圖片
 */
public classMyQrCodeImageimplementsQRCodeImage{
    BufferedImage bufferedImage;

    public MyQrCodeImage(BufferedImage bufferedImage) {
        this.bufferedImage = bufferedImage;
    }

    public int getHeight() {
        return bufferedImage.getHeight();
    }

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

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

}

/**
 * 生成二維碼
 */
public classCreateQrCode {

    public static void main(String[] args) throws IOException {
        Qrcode x = new Qrcode();
        x.setQrcodeErrorCorrect('M');//設定糾錯等級
        x.setQrcodeEncodeMode('B');//N代表數字,A代表a-Z,B代表其它字元
        x.setQrcodeVersion(7);//設定版本
        String qrData = "www.shuai.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[] d = qrData.getBytes("gbk");
        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("E:/develop/code.png"));
    }
}

/**
 * 讀取二維碼
 */
public classReadQrCode {

    public static void main(String[] args) throws IOException {
        File file = new File("E:/develop/code.png");
        BufferedImage bufferedImage = ImageIO.read(file);
        Qrcode qrcode = new Qrcode();
        QRCodeDecoder codeDecoder = new QRCodeDecoder();
        String result = new String(codeDecoder.decode(new MyQrCodeImage(bufferedImage)),"GBK");
        System.out.println(result);
    }
}
  • 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
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78

jquery.qrcode.js

1、引入jquery.min.js、jquery.qrcode.min.js 2、頁面

<%@ page language=”java” contentType=“text/html; charset=UTF-8 

    pageEncoding=“UTF-8%> 

<!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd“> 

<html> 

<head> 

<metahttp-equiv=“Content-Type”content=“text/html; charset=UTF-8”> 

<title>二維碼</title> 

<%  

    String path = request.getContextPath(); 

    String bathPath = request.getScheme() + “://” + request.getServerName() + “:” + request.getServerPort();  

    pageContext.setAttribute(“path”, path); 

    pageContext.setAttribute(“bathPath”, bathPath); 

%> 

<scripttype=“text/javascriptsrc=“${path }/js/jquery.min.js></script> 

<scripttype=“text/javascriptsrc=“${path }/js/jquery.qrcode.min.js></script> 

</head> 

<body> 

    生成的二維碼: <br/> 

    <divid=“qrcode”></div> 

    <scripttype=“text/javascript> 

        $(’#qrcode’).qrcode(“www.shuai.cn”); 

    </script> 

</body>

</html>

  • 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