posts - 22,comments - 35,trackbacks - 0
QR Code 編碼

/*********************************************************
*
* Created on 2007年3月1日
*
* Program : QRCodeEncoderTest.java
* @author : ryanpai
* Java平台 : J2SDK 1.4.9
* OS : XP
* 1. 下載使用 (http://www.swetake.com/qr/ ) qrcode.jar
*     => 建議改為qr_encode.jar
*     (因為跟SourceForge QR Code Decode Library 之 qrcode.jar 同名)
* 2. 將上述下載的 jar 增加至classpath
*
********************************************************/


import com.swetake.util.Qrcode;
import java.io.*;

import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;

/******************************************
*
* @author  Ryan Pai
*
******************************************/


public class QRCodeEncoderTest {
   
    /** Creates a new instance of QRCodeEncoderTest */
    public QRCodeEncoderTest() {
    }
   
    /**
     * @param args the command line arguments
     */

   
    public static void main(String[] args) {
       
        try{
           
            // TODO code application logic here
           
            // Constructor Qrcode Object
            com.swetake.util.Qrcode testQrcode
                  = new com.swetake.util.Qrcode();
            testQrcode.setQrcodeErrorCorrect('M');
            testQrcode.setQrcodeEncodeMode('B');
            testQrcode.setQrcodeVersion(7);
           
            // 設定QR Code 編碼內容
            String testString = "";
            testString = testString+"JavaWorld .\n";
            testString = testString+"中文Java討論網站\n";
            testString = testString+"http://www.javaworld.com\n";
           
            // getBytes
            byte[] d = testString.getBytes("Big5");
           
            // 設定圖檔寬度 140*140
            BufferedImage bi
            = new BufferedImage(140, 140, BufferedImage.TYPE_INT_RGB);
           
            // createGraphics
            Graphics2D g = bi.createGraphics();
           
            // set background
            g.setBackground(Color.WHITE);
            g.clearRect(0, 0, 140, 140);
           
            // 設定字型顏色 => BLACK
            g.setColor(Color.BLACK);
           
            // 轉出 Bytes
            if (d.length>0 && d.length <120){
                boolean[][] s = testQrcode.calQrcode(d);
                for (int i=0;i<s.length;i++){
                    for (int j=0;j<s.length;j++){
                        if (s[j][i]) {
                            g.fillRect(j*3+2,i*3+2,3,3);
                        }
                    }
                }
            }
           
            g.dispose();
            bi.flush();
           
            // 設定 產生檔案路徑
            String FilePath="D:\\TestQRCode.jpg";
            File f = new File(FilePath);
           
            // 產生TestQRCode JPG File
            ImageIO.write(bi, "jpg", f);
           
        } // end try
        catch (Exception e) {
            e.printStackTrace();
        } // end catch
       
QR Code 解碼

/*********************************************************
*
* Created on 2007年3月1日
*
* Program : QRCodeDecoderTest.java
* @author : ryanpai
* Java平台 : J2SDK 1.4.9
* OS : XP
* 1. 下載使用 (http://sourceforge.jp/projects/qrcode/ ) qrcode.jar
* => (SourceForge Open Source QR Code Decode Library)
* 2. 修改官方 Sample => QRCodeDecoderCUIExample.java
* 3. 將上述下載的 jar 增加至classpath
*
********************************************************/


import jp.sourceforge.qrcode.codec.QRCodeDecoder;
import jp.sourceforge.qrcode.codec.data.QRCodeImage;
import jp.sourceforge.qrcode.codec.exception.DecodingFailedException;
import jp.sourceforge.qrcode.codec.exception.InvalidVersionInfoException;

import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;

/******************************************
*
* @author RYANPAI
*
******************************************/


public class QRCodeDecoderTest {

/** Creates a new instance of QRCodeDecoderTest */
public QRCodeDecoderTest() {
}

/**
* @param args the command line arguments
*/

public static void main(String[] args) {

// TODO code application logic here
QRCodeDecoder decoder = new QRCodeDecoder();

// 設定讀取QR Code圖檔路徑
File imageFile = new File("D:\\TestQRCode.jpg");

// 設定 BufferedImage
BufferedImage image = null;

try {
image = ImageIO.read(imageFile);
} catch (IOException e) {
System.out.println("Error: "+ e.getMessage());
}
try {
String decodedData
= new String(decoder.decode(new J2SEImage(image)));
System.out.println(decodedData);
} catch (DecodingFailedException dfe) {
System.out.println("Error: " + dfe.getMessage());
}


}
}

class J2SEImage implements QRCodeImage {
BufferedImage image;

public J2SEImage(BufferedImage image) {
this.image = image;
}

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

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

public int getPixel(int x, int y) {
return image.getRGB(x, y);
}

}

} // end main


}
posted on 2009-06-29 15:59 kelven 阅读(521) 评论(0)  编辑  收藏 所属分类: java

只有注册用户登录后才能发表评论。


网站导航: