国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

java代碼生成二維碼圖片

2019-11-14 21:46:17
字體:
來源:轉載
供稿:網友
java代碼生成二維碼圖片

需要使用到的jar包:QRCode.jar

這里提供一個下載:點此下載QRCode.jar

因為代碼比較簡單。就不多啰嗦,直接帖代碼了,拷過去就能直接用.

TwoDimensionCode.java:
  1 import java.awt.Color;  2 import java.awt.Graphics2D;  3 import java.awt.image.BufferedImage;  4 import java.io.File;  5 import java.io.IOException;  6 import java.io.InputStream;  7 import java.io.OutputStream;  8   9 import javax.imageio.ImageIO; 10  11 import jp.sourceforge.qrcode.QRCodeDecoder; 12 import jp.sourceforge.qrcode.exception.DecodingFailedException; 13  14 import com.swetake.util.Qrcode; 15  16  17 public class TwoDimensionCode { 18       19       /** 20       * 生成二維碼(QRCode)圖片 21       * @param content 存儲內容 22       * @param imgPath 圖片路徑 23       */ 24       public void encoderQRCode(String content, String imgPath)  { 25             this.encoderQRCode(content, imgPath, "png", 7); 26      } 27       28       /** 29       * 生成二維碼(QRCode)圖片 30       * @param content 存儲內容 31       * @param output 輸出流 32       */ 33       public void encoderQRCode(String content, OutputStream output) { 34             this.encoderQRCode(content, output, "png", 7); 35      } 36       37       /** 38       * 生成二維碼(QRCode)圖片 39       * @param content 存儲內容 40       * @param imgPath 圖片路徑 41       * @param imgType 圖片類型 42       */ 43       public void encoderQRCode(String content, String imgPath,  String imgType) { 44             this.encoderQRCode(content, imgPath, imgType, 7); 45      } 46       47       /** 48       * 生成二維碼(QRCode)圖片 49       * @param content 存儲內容 50       * @param output 輸出流 51       * @param imgType 圖片類型 52       */ 53       public void encoderQRCode(String content, OutputStream output, String imgType) { 54             this.encoderQRCode(content, output, imgType, 7); 55      } 56  57       /** 58       * 生成二維碼(QRCode)圖片 59       * @param content 存儲內容 60       * @param imgPath 圖片路徑 61       * @param imgType 圖片類型 62       * @param size 二維碼尺寸 63       */ 64       public void encoderQRCode(String content, String imgPath,  String imgType, int size) { 65             try { 66                 BufferedImage bufImg =this.qRCodeCommon(content, imgType, size); 67                  68                 File imgFile = new File(imgPath); 69                  // 生成二維碼QRCode圖片 70                 ImageIO. write(bufImg, imgType , imgFile); 71            } catch (Exception e) { 72                 e.PRintStackTrace(); 73            } 74      } 75  76       /** 77       * 生成二維碼(QRCode)圖片 78       * @param content 存儲內容 79       * @param output 輸出流 80       * @param imgType 圖片類型 81       * @param size 二維碼尺寸 82       */ 83       public void encoderQRCode(String content, OutputStream output, String imgType, int size) { 84             try { 85                 BufferedImage bufImg =this.qRCodeCommon(content, imgType, size); 86                  // 生成二維碼QRCode圖片 87                 ImageIO. write(bufImg, imgType, output); 88            } catch (Exception e) { 89                 e.printStackTrace(); 90            } 91      } 92       93       /** 94       * 生成二維碼(QRCode)圖片的公共方法 95       * @param content 存儲內容 96       * @param imgType 圖片類型 97       * @param size 二維碼尺寸 98       * @return 99       */100       private BufferedImage qRCodeCommon(String content, String  imgType, int size) {101            BufferedImage bufImg = null;102             try {103                 Qrcode qrcodeHandler = new Qrcode();104                  // 設置二維碼排錯率,可選L(7%)、M(15%)、Q(25%)、H(30%),排錯率越高可存儲的信息越少,但對二維碼清晰度的要求越小105                 qrcodeHandler.setQrcodeErrorCorrect( 'M');106                 qrcodeHandler.setQrcodeEncodeMode( 'B');107                  // 設置設置二維碼尺寸,取值范圍1-40,值越大尺寸越大,可存儲的信息越大108                 qrcodeHandler.setQrcodeVersion(size);109                  // 獲得內容的字節數組,設置編碼格式110                  byte[] contentBytes = content.getBytes( "utf-8");111                  // 圖片尺寸112                  int imgSize = 67 + 12 * (size - 1);113                 bufImg = new BufferedImage(imgSize, imgSize, BufferedImage.TYPE_INT_RGB );114                 Graphics2D gs = bufImg.createGraphics();115                  // 設置背景顏色116                 gs.setBackground(Color. WHITE);117                 gs.clearRect(0, 0, imgSize, imgSize);118 119                  // 設定圖像顏色> BLACK120                 gs.setColor(Color. BLACK);121                  // 設置偏移量,不設置可能導致解析出錯122                  int pixoff = 2;123                  // 輸出內容> 二維碼124                  if (contentBytes. length > 0 && contentBytes.length < 800) {125                       boolean[][] codeOut = qrcodeHandler.calQrcode(contentBytes);126                       for ( int i = 0; i < codeOut. length; i++) {127                             for ( int j = 0; j < codeOut. length; j++) {128                                  if (codeOut[j][i]) {129                                      gs.fillRect(j * 3 + pixoff, i * 3 + pixoff, 3, 3);130                                 }131                            }132                      }133                 } else {134                       throw new Exception( "QRCode content bytes  length = " + contentBytes. length + " not in [0, 800].");135                 }136                 gs.dispose();137                 bufImg.flush();138            } catch (Exception e) {139                 e.printStackTrace();140            }141             return bufImg;142      }143      144       /**145       * 解析二維碼(QRCode)146       * @param imgPath 圖片路徑147       * @return148       */149       public String decoderQRCode(String imgPath) {150             // QRCode 二維碼圖片的文件151            File imageFile = new File(imgPath);152            BufferedImage bufImg = null;153            String content = null;154             try {155                 bufImg = ImageIO. read(imageFile);156                 QRCodeDecoder decoder = new QRCodeDecoder();157                 content = new String(decoder.decode( newTwoDimensionCodeImage(bufImg)), "utf-8" );158            } catch (IOException e) {159                 System. out.println( "Error: " + e.getMessage());160                 e.printStackTrace();161            } catch (DecodingFailedException dfe) {162                 System. out.println( "Error: " + dfe.getMessage());163                 dfe.printStackTrace();164            }165             return content;166      }167      168       /**169       * 解析二維碼(QRCode)170       * @param input 輸入流171       * @return172       */173       public String decoderQRCode(InputStream input) {174            BufferedImage bufImg = null;175            String content = null;176             try {177                 bufImg = ImageIO. read(input);178                 QRCodeDecoder decoder = new QRCodeDecoder();179                 content = new String(decoder.decode( newTwoDimensionCodeImage(bufImg)), "utf-8" );180            } catch (IOException e) {181                 System. out.println( "Error: " + e.getMessage());182                 e.printStackTrace();183            } catch (DecodingFailedException dfe) {184                 System. out.println( "Error: " + dfe.getMessage());185                 dfe.printStackTrace();186            }187             return content;188      }189 190       public static void main(String[] args) {191            String imgPath = "G:/ZTFCard.png";192            String encoderContent = "我的名片" + "/n我的微博:[http://t.QQ.com/fengqingyan]" + "/n電子郵件:[zhitianfeng@hotmail.com]" +"/n手機:[15601973133]" ;193            TwoDimensionCode handler = new TwoDimensionCode();194            handler.encoderQRCode(encoderContent, imgPath, "png" );195 //         try {196 //              OutputStream output = new FileOutputStream(imgPath);197 //              handler.encoderQRCode(content, output);198 //         } catch (Exception e) {199 //              e.printStackTrace();200 //         }201            System. out.println( "========encoder success" );202            203            204            String decoderContent = handler.decoderQRCode(imgPath);205            System. out.println( "解析結果如下:" );206            System. out.println(decoderContent);207            System. out.println( "========decoder success!!!" );208      }209 }

第二個java文件:TwoDimensionCodeImage.java

 1 import java.awt.image.BufferedImage; 2  3 import jp.sourceforge.qrcode.data.QRCodeImage; 4  5 public class TwoDimensionCodeImage implements QRCodeImage { 6  7     BufferedImage bufImg; 8      9     public TwoDimensionCodeImage(BufferedImage bufImg) {10         this.bufImg = bufImg;11     }12     13     public int getHeight() {14         return bufImg.getHeight();15     }16 17     public int getPixel(int x, int y) {18         return bufImg.getRGB(x, y);19     }20 21     public int getWidth() {22         return bufImg.getWidth();23     }24 25 }

下面是一個測試的類:QRtest.java

public class QRtest {    public static void main(String[] args){        TwoDimensionCode td = new TwoDimensionCode();        td.encoderQRCode("this is a test","d://123.jpg","jpg",12);    }}

順便附上代碼:點擊下載代碼

END!

附上一張生成的樣圖:

還是挺贊的說。

突然想到以后可以用此方法去表白什么的,生成一張 I love you!的二維碼給喜歡的女孩子去掃吧,哈哈


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 抚松县| 巴楚县| 天气| 淮安市| 阿坝| 南江县| 德清县| 西昌市| 德清县| 平山县| 会同县| 井陉县| 县级市| 济阳县| 门源| 栾川县| 长寿区| 平陆县| 竹山县| 乐都县| 晋宁县| 临潭县| 乌海市| 扶余县| 海口市| 黄山市| 海林市| 涿鹿县| 社旗县| 澜沧| 安多县| 陈巴尔虎旗| 淮阳县| 钟祥市| 昌乐县| 高雄县| 汽车| 尉氏县| 榆林市| 枝江市| 香格里拉县|