Google Analytics

2014年6月25日 星期三

Java產生QR Code - Part 2

承上篇(Java產生QR Code - Part 1)

Java產生QRCode可以使用swetake所開放的原始碼http://www.swetake.com/qrcode/java/qr_java.html來做開發。

一個基礎的QR Code產生程式碼如下:
public static void main(String[] args) throws Exception {
        String content = "http://google.com.tw";
        String charsetName =  "UTF-8";
        String qrCodeImagePath = "c:/qrCode.jpg";
   
        int version = 2; 
        char errorCorrect = 'M';
        char encodeMode ='B';
   
        Color backgroundColor = Color.WHITE;
        Color paintColor = Color.BLACK;
        int matrixWidth = 3;
        int borderWidth = 2;
        String imageFormat = "png";
   
        int matrix = 17 + version * 4;
        int imageWidth = (matrix)*matrixWidth+borderWidth*2;
   
        Qrcode qrcodeHandler = new Qrcode();
        qrcodeHandler.setQrcodeVersion(version);
        qrcodeHandler.setQrcodeErrorCorrect(errorCorrect);
        qrcodeHandler.setQrcodeEncodeMode(encodeMode); 

        byte[] contentBytes = content.getBytes(charsetName); 
       
        BufferedImage bufImg = new BufferedImage(imageWidth, imageWidth, BufferedImage.TYPE_INT_RGB); 
        Graphics2D gs = bufImg.createGraphics();   
        gs.setBackground(backgroundColor); 
        gs.clearRect(0, 0, imageWidth, imageWidth); 
        gs.setColor(paintColor);

        if (contentBytes.length > 0) { 
            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 * matrixWidth + borderWidth, i * matrixWidth + borderWidth, matrixWidth, matrixWidth); 
                    } 
                } 
            } 
        } 
       
        bufImg.flush(); 
   
        File imgFile = new File(qrCodeImagePath); 
        ImageIO.write(bufImg, imageFormat, imgFile); 

}


說明

以下幾行程式碼,其目的在定義QR Code的屬性:
        int version = 2; 
        char errorCorrect = 'M';
        char encodeMode ='B';
        ......(略)......
        Qrcode qrcodeHandler = new Qrcode();
        qrcodeHandler.setQrcodeVersion(version);
        qrcodeHandler.setQrcodeErrorCorrect(errorCorrect);
        qrcodeHandler.setQrcodeEncodeMode(encodeMode); 
根據你要放入QR Code中的資訊內容及編碼方式,查表決定QR Code的version與容錯等級。
內容編碼為'N', 'A'與其他字元,'N'代表數字(Numeric)、'A'代表英文字母加數字(Alphanumeric),我們這邊要用UTF-8,可以放入除'N'與'A'之外的任意字元,如'B';version範圍為1~40;容錯等級為'L', 'M', 'Q', 'H',分別代表7%, 15%, 25%, 30%的容錯等級(相關背景知識見Java產生QR Code - Part 1)。

以下幾行程式碼,其目的在定義產生的QR Code的圖片格式與大小:
        Color backgroundColor = Color.WHITE;
        Color paintColor = Color.BLACK;
        int matrixWidth = 3;
        int borderWidth = 2;
        String imageFormat = "png";
        ......(略)......
        BufferedImage bufImg = new BufferedImage(imageWidth, imageWidth, BufferedImage.TYPE_INT_RGB);  
        Graphics2D gs = bufImg.createGraphics();   
        gs.setBackground(backgroundColor); 
        gs.clearRect(0, 0, imageWidth, imageWidth); 

        gs.setColor(paintColor);
再此設定了QR Code的輸出圖片格式為png、前景顏色為黑色、背景顏色為白色、每一個martix的寬度(等於高度)為3px、QR Code的邊界為2px。

執行結果(連到 http://google.com.tw)



1 則留言:

  1. Slingo Titanium Blade - ITADIUMART.COM
    Slingo Titanium Blade - ITADIUMART.COM titanium i phone case - The only one-stop shop for 2019 ford ecosport titanium the best shaving equipment - all aftershokz trekz titanium at the titanium water bottle best price titanium or ceramic flat iron on the market. The most trusted international shopping

    回覆刪除