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

          /*********************************************************
          *
          * Created on 2007年3月1日
          *
          * Program : QRCodeEncoderTest.java
          * @author : ryanpai
          * Java平臺(tái) : J2SDK 1.4.9
          * OS : XP
          * 1. 下載使用 (http://www.swetake.com/qr/ ) qrcode.jar
          *     => 建議改為qr_encode.jar
          *     (因?yàn)楦鶶ourceForge 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平臺(tái) : 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) 評(píng)論(0)  編輯  收藏 所屬分類: java
          主站蜘蛛池模板: 华宁县| 福州市| 上林县| 高碑店市| 丹寨县| 齐齐哈尔市| 清新县| 奉节县| 英德市| 万年县| 苏尼特左旗| 泰和县| 乌兰察布市| 积石山| 巍山| 如东县| 耿马| 郓城县| 西和县| 罗甸县| 三都| 台州市| 天津市| 界首市| 剑河县| 两当县| 柏乡县| 广饶县| 孟州市| 宝鸡市| 孟连| 竹北市| 南投市| 高清| 左权县| 苗栗市| 香格里拉县| 贵德县| 中方县| 东海县| 正镶白旗|