JAVA—咖啡館

          ——?dú)g迎訪問rogerfan的博客,常來《JAVA——咖啡館》坐坐,喝杯濃香的咖啡,彼此探討一下JAVA技術(shù),交流工作經(jīng)驗(yàn),分享JAVA帶來的快樂!本網(wǎng)站部分轉(zhuǎn)載文章,如果有版權(quán)問題請(qǐng)與我聯(lián)系。

          BlogJava 首頁 新隨筆 聯(lián)系 聚合 管理
            447 Posts :: 145 Stories :: 368 Comments :: 0 Trackbacks

          公告

           

          Locations of visitors to this page
          點(diǎn)擊這里給我發(fā)消息 點(diǎn)擊這里給我發(fā)消息

          常用鏈接

          留言簿(17)

          隨筆分類(542)

          隨筆檔案(438)

          文章分類(182)

          文章檔案(142)

          新聞分類

          ※→ 【JAVA文檔】

          ※→ 【親人博客】

          ※→ 【休閑娛樂】

          ※→ 【友情鏈接】

          ※→ 【學(xué)習(xí)網(wǎng)站】

          ※→ 【服務(wù)網(wǎng)站】

          ※→ 【著名網(wǎng)站】

          ※→ 【阿里博客】

          最新隨筆

          搜索

          積分與排名

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          TwoDimensionCode類:二維碼操作核心類
          package qrcode;

          import java.awt.Color;
          import java.awt.Graphics2D;
          import java.awt.image.BufferedImage;
          import java.io.File;
          import java.io.IOException;
          import java.io.InputStream;
          import java.io.OutputStream;

          import javax.imageio.ImageIO;

          import jp.sourceforge.qrcode.QRCodeDecoder;
          import jp.sourceforge.qrcode.exception.DecodingFailedException;

          import com.swetake.util.Qrcode;

          public class TwoDimensionCode {
              
              /**
               * 生成二維碼(QRCode)圖片
               * 
          @param content 存儲(chǔ)內(nèi)容
               * 
          @param imgPath 圖片路徑
               
          */
              public void encoderQRCode(String content, String imgPath) {
                  this.encoderQRCode(content, imgPath, "png", 7);
              }
              
              /**
               * 生成二維碼(QRCode)圖片
               * 
          @param content 存儲(chǔ)內(nèi)容
               * 
          @param output 輸出流
               
          */
              public void encoderQRCode(String content, OutputStream output) {
                  this.encoderQRCode(content, output, "png", 7);
              }
              
              /**
               * 生成二維碼(QRCode)圖片
               * 
          @param content 存儲(chǔ)內(nèi)容
               * 
          @param imgPath 圖片路徑
               * 
          @param imgType 圖片類型
               
          */
              public void encoderQRCode(String content, String imgPath, String imgType) {
                  this.encoderQRCode(content, imgPath, imgType, 7);
              }
              
              /**
               * 生成二維碼(QRCode)圖片
               * 
          @param content 存儲(chǔ)內(nèi)容
               * 
          @param output 輸出流
               * 
          @param imgType 圖片類型
               
          */
              public void encoderQRCode(String content, OutputStream output, String imgType) {
                  this.encoderQRCode(content, output, imgType, 7);
              }

              /**
               * 生成二維碼(QRCode)圖片
               * 
          @param content 存儲(chǔ)內(nèi)容
               * 
          @param imgPath 圖片路徑
               * 
          @param imgType 圖片類型
               * 
          @param size 二維碼尺寸
               
          */
              public void encoderQRCode(String content, String imgPath, String imgType, int size) {
                  try {
                      BufferedImage bufImg = this.qRCodeCommon(content, imgType, size);
                      
                      File imgFile = new File(imgPath);
                      // 生成二維碼QRCode圖片
                      ImageIO.write(bufImg, imgType, imgFile);
                  } catch (Exception e) {
                      e.printStackTrace();
                  }
              }

              /**
               * 生成二維碼(QRCode)圖片
               * 
          @param content 存儲(chǔ)內(nèi)容
               * 
          @param output 輸出流
               * 
          @param imgType 圖片類型
               * 
          @param size 二維碼尺寸
               
          */
              public void encoderQRCode(String content, OutputStream output, String imgType, int size) {
                  try {
                      BufferedImage bufImg = this.qRCodeCommon(content, imgType, size);
                      // 生成二維碼QRCode圖片
                      ImageIO.write(bufImg, imgType, output);
                  } catch (Exception e) {
                      e.printStackTrace();
                  }
              }
              
              /**
               * 生成二維碼(QRCode)圖片的公共方法
               * 
          @param content 存儲(chǔ)內(nèi)容
               * 
          @param imgType 圖片類型
               * 
          @param size 二維碼尺寸
               * 
          @return
               
          */
              private BufferedImage qRCodeCommon(String content, String imgType, int size) {
                  BufferedImage bufImg = null;
                  try {
                      Qrcode qrcodeHandler = new Qrcode();
                      // 設(shè)置二維碼排錯(cuò)率,可選L(7%)、M(15%)、Q(25%)、H(30%),排錯(cuò)率越高可存儲(chǔ)的信息越少,但對(duì)二維碼清晰度的要求越小
                      qrcodeHandler.setQrcodeErrorCorrect('M');
                      qrcodeHandler.setQrcodeEncodeMode('B');
                      // 設(shè)置設(shè)置二維碼尺寸,取值范圍1-40,值越大尺寸越大,可存儲(chǔ)的信息越大
                      qrcodeHandler.setQrcodeVersion(size);
                      // 獲得內(nèi)容的字節(jié)數(shù)組,設(shè)置編碼格式
                      byte[] contentBytes = content.getBytes("utf-8");
                      // 圖片尺寸
                      int imgSize = 67 + 12 * (size - 1);
                      bufImg = new BufferedImage(imgSize, imgSize, BufferedImage.TYPE_INT_RGB);
                      Graphics2D gs = bufImg.createGraphics();
                      // 設(shè)置背景顏色
                      gs.setBackground(Color.WHITE);
                      gs.clearRect(0, 0, imgSize, imgSize);

                      // 設(shè)定圖像顏色> BLACK
                      gs.setColor(Color.BLACK);
                      // 設(shè)置偏移量,不設(shè)置可能導(dǎo)致解析出錯(cuò)
                      int pixoff = 2;
                      // 輸出內(nèi)容> 二維碼
                      if (contentBytes.length > 0 && contentBytes.length < 800) {
                          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 * 3 + pixoff, i * 3 + pixoff, 3, 3);
                                  }
                              }
                          }
                      } else {
                          throw new Exception("QRCode content bytes length = " + contentBytes.length + " not in [0, 800].");
                      }
                      gs.dispose();
                      bufImg.flush();
                  } catch (Exception e) {
                      e.printStackTrace();
                  }
                  return bufImg;
              }
              
              /**
               * 解析二維碼(QRCode)
               * 
          @param imgPath 圖片路徑
               * 
          @return
               
          */
              public String decoderQRCode(String imgPath) {
                  // QRCode 二維碼圖片的文件
                  File imageFile = new File(imgPath);
                  BufferedImage bufImg = null;
                  String content = null;
                  try {
                      bufImg = ImageIO.read(imageFile);
                      QRCodeDecoder decoder = new QRCodeDecoder();
                      content = new String(decoder.decode(new TwoDimensionCodeImage(bufImg)), "utf-8"); 
                  } catch (IOException e) {
                      System.out.println("Error: " + e.getMessage());
                      e.printStackTrace();
                  } catch (DecodingFailedException dfe) {
                      System.out.println("Error: " + dfe.getMessage());
                      dfe.printStackTrace();
                  }
                  return content;
              }
              
              /**
               * 解析二維碼(QRCode)
               * 
          @param input 輸入流
               * 
          @return
               
          */
              public String decoderQRCode(InputStream input) {
                  BufferedImage bufImg = null;
                  String content = null;
                  try {
                      bufImg = ImageIO.read(input);
                      QRCodeDecoder decoder = new QRCodeDecoder();
                      content = new String(decoder.decode(new TwoDimensionCodeImage(bufImg)), "utf-8"); 
                  } catch (IOException e) {
                      System.out.println("Error: " + e.getMessage());
                      e.printStackTrace();
                  } catch (DecodingFailedException dfe) {
                      System.out.println("Error: " + dfe.getMessage());
                      dfe.printStackTrace();
                  }
                  return content;
              }

              public static void main(String[] args) {
                  String imgPath = "G:/TDDOWNLOAD/Michael_QRCode.png";
                  String encoderContent = "Hello 大大、小小,welcome to QRCode!" + "\nMyblog [ http://sjsky.iteye.com ]" + "\nEMail [ sjsky007@gmail.com ]";
                  TwoDimensionCode handler = new TwoDimensionCode();
                  handler.encoderQRCode(encoderContent, imgPath, "png");
          //        try {
          //            OutputStream output = new FileOutputStream(imgPath);
          //            handler.encoderQRCode(content, output);
          //        } catch (Exception e) {
          //            e.printStackTrace();
          //        }
                  System.out.println("========encoder success");
                  
                  
                  String decoderContent = handler.decoderQRCode(imgPath);
                  System.out.println("解析結(jié)果如下:");
                  System.out.println(decoderContent);
                  System.out.println("========decoder success!!!");
              }
          }
          TwoDimensionCodeImage 類:二維碼圖片對(duì)象
          package qrcode;

          import java.awt.image.BufferedImage;

          import jp.sourceforge.qrcode.data.QRCodeImage;

          public class TwoDimensionCodeImage implements QRCodeImage {

              BufferedImage bufImg;
              
              public TwoDimensionCodeImage(BufferedImage bufImg) {
                  this.bufImg = bufImg;
              }
              
              @Override
              public int getHeight() {
                  return bufImg.getHeight();
              }

              @Override
              public int getPixel(int x, int y) {
                  return bufImg.getRGB(x, y);
              }

              @Override
              public int getWidth() {
                  return bufImg.getWidth();
              }

          }
          posted on 2015-12-30 10:09 rogerfan 閱讀(527) 評(píng)論(0)  編輯  收藏 所屬分類: 【Java知識(shí)】
          主站蜘蛛池模板: 博野县| 廊坊市| 宜兴市| 栾川县| 宜阳县| 安龙县| 甘南县| 南投市| 苍山县| 锦屏县| 昭通市| 新昌县| 南雄市| 会泽县| 襄汾县| 阜平县| 长治市| 芷江| 札达县| 巴中市| 新宁县| 德清县| 体育| 轮台县| 绿春县| 乐安县| 天水市| 剑川县| 新泰市| 临夏市| 泗水县| 阿克苏市| 枣阳市| 噶尔县| 武安市| 缙云县| 福州市| 汶上县| 武夷山市| 双桥区| 宜良县|