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);
                     
                      // 設(shè)定QR Code 編碼內(nèi)容
                      String testString = "";
                      testString = testString+"JavaWorld .\n";
                      testString = testString+"中文Java討論網(wǎng)站\n";
                      testString = testString+"http://www.javaworld.com\n";
                     
                      // getBytes
                      byte[] d = testString.getBytes("Big5");
                     
                      // 設(shè)定圖檔寬度 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);
                     
                      // 設(shè)定字型顏色 => BLACK
                      g.setColor(Color.BLACK);
                     
                      // 轉(zhuǎn)出 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();
                     
                      // 設(shè)定 產(chǎn)生檔案路徑
                      String FilePath="D:\\TestQRCode.jpg";
                      File f = new File(FilePath);
                     
                      // 產(chǎn)生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();

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

          // 設(shè)定 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 閱讀(545) 評論(0)  編輯  收藏 所屬分類: java
          主站蜘蛛池模板: 本溪| 化隆| 南涧| 定日县| 宝应县| 南阳市| 广南县| 潼关县| 宁远县| 石城县| 大同市| 吉木乃县| 清原| 吉安县| 太仓市| 拉孜县| 左权县| 彭山县| 香格里拉县| 日土县| 双鸭山市| 景东| 弋阳县| 沾益县| 岗巴县| 尚志市| 漯河市| 辉县市| 临桂县| 阳朔县| 菏泽市| 南召县| 上杭县| 惠东县| 云和县| 普兰店市| 昌黎县| 习水县| 上杭县| 敦煌市| 沅陵县|