java 二維碼

來源:互聯網
上載者:User

標籤:alt   時間   mod   barcode   nload   壓縮包   工具   tools   getheight   

在http://www.ostools.net/qr看到了一個產生二維碼的工具,於是就產生了一個想法:

為什麼自己不做一個二維碼的產生和解析工具呢?花了一個多鐘的時間,嘿嘿,就做出來啦...

先來看看吧:

CODE_QR:                                            

 CODE_128:        

PDF_417:

二維碼的意思是:

下面是操作步驟:

一:下載zxing的壓縮包:

可以到這裡下載:http://code.google.com/p/zxing/downloads/list

ZXing-2.1.zip:http://code.google.com/p/zxing/downloads/detail?name=ZXing-2.1.zip&can=2&q=

得到:

zxing-2.1\core\core.jar

zxing-2.1\javase\javase.jar

二:把他添加進入你的項目的裡面:

/QRCodes/src/com/b510/qrcode/QRCode.java

 

  1 /**  2  *   3  */  4 package com.b510.qrcode;  5   6 import java.awt.image.BufferedImage;  7 import java.io.File;  8 import java.io.IOException;  9 import java.util.Hashtable; 10 import java.util.Map; 11  12 import javax.imageio.ImageIO; 13  14 import com.google.zxing.BarcodeFormat; 15 import com.google.zxing.BinaryBitmap; 16 import com.google.zxing.DecodeHintType; 17 import com.google.zxing.EncodeHintType; 18 import com.google.zxing.LuminanceSource; 19 import com.google.zxing.MultiFormatReader; 20 import com.google.zxing.MultiFormatWriter; 21 import com.google.zxing.ReaderException; 22 import com.google.zxing.Result; 23 import com.google.zxing.Writer; 24 import com.google.zxing.WriterException; 25 import com.google.zxing.client.j2se.BufferedImageLuminanceSource; 26 import com.google.zxing.common.BitMatrix; 27 import com.google.zxing.common.HybridBinarizer; 28 import com.google.zxing.oned.CodaBarWriter; 29 import com.google.zxing.oned.Code128Writer; 30 import com.google.zxing.oned.Code39Writer; 31 import com.google.zxing.oned.EAN13Writer; 32 import com.google.zxing.oned.EAN8Writer; 33 import com.google.zxing.oned.ITFWriter; 34 import com.google.zxing.oned.UPCAWriter; 35 import com.google.zxing.pdf417.encoder.PDF417Writer; 36 import com.google.zxing.qrcode.QRCodeWriter; 37  38 /** 39  * 利用zxing開源工具產生二維碼QRCode 40  *  41  * @date 2012-10-26 42  * @author xhw 43  *  44  */ 45 public class QRCode { 46     private static final int BLACK = 0xff000000; 47     private static final int WHITE = 0xFFFFFFFF; 48  49     /** 50      * @param args 51      */ 52     public static void main(String[] args) { 53         QRCode test = new QRCode(); 54         File file = new File("C://test.png"); 55         /** 56          * 在com.google.zxing.MultiFormatWriter類中,定義了一些我們不知道的碼,二維碼只是其中的一種<br> 57          * public BitMatrix encode(String contents, 58                           BarcodeFormat format, 59                           int width, int height, 60                           Map<EncodeHintType,?> hints) throws WriterException { 61             Writer writer; 62             switch (format) { 63               case EAN_8: 64                 writer = new EAN8Writer(); 65                 break; 66               case EAN_13: 67                 writer = new EAN13Writer(); 68                 break; 69               case UPC_A: 70                 writer = new UPCAWriter(); 71                 break; 72               case QR_CODE:  //這裡是二維碼 73                 writer = new QRCodeWriter(); 74                 break; 75               case CODE_39: 76                 writer = new Code39Writer(); 77                 break; 78               case CODE_128:  //這個可以產生 79                 writer = new Code128Writer(); 80                 break; 81               case ITF: 82                 writer = new ITFWriter(); 83                 break; 84               case PDF_417:  //這個可以產生 85                 writer = new PDF417Writer(); 86                 break; 87               case CODABAR: 88                 writer = new CodaBarWriter(); 89                 break; 90               default: 91                 throw new IllegalArgumentException("No encoder available for format " + format); 92             } 93             return writer.encode(contents, format, width, height, hints); 94           } 95  96          */ 97         test.encode("helloworld,I‘m Hongten.welcome to my zone:http://www.cnblogs.com/hongten", file, BarcodeFormat.QR_CODE, 200, 200, null); 98         test.decode(file); 99     }100 101     /**102      * 產生QRCode二維碼<br> 103      * 在編碼時需要將com.google.zxing.qrcode.encoder.Encoder.java中的<br>104      *  static final String DEFAULT_BYTE_MODE_ENCODING = "ISO8859-1";<br>105      *  修改為UTF-8,否則中文編譯後解析不了<br>106      */107     public void encode(String contents, File file, BarcodeFormat format, int width, int height, Map<EncodeHintType, ?> hints) {108         try {
//消除亂碼
          contents = new String(contents.getBytes("UTF-8"), "ISO-8859-1"); 109 BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, format, width, height);110 writeToFile(bitMatrix, "png", file);111 } catch (Exception e) {112 e.printStackTrace();113 }114 }115 116 /**117 * 產生二維碼圖片<br>118 * 119 * @param matrix120 * @param format121 * 圖片格式122 * @param file123 * 產生二維碼圖片位置124 * @throws IOException125 */126 public static void writeToFile(BitMatrix matrix, String format, File file) throws IOException {127 BufferedImage image = toBufferedImage(matrix);128 ImageIO.write(image, format, file);129 }130 131 /**132 * 產生二維碼內容<br>133 * 134 * @param matrix135 * @return136 */137 public static BufferedImage toBufferedImage(BitMatrix matrix) {138 int width = matrix.getWidth();139 int height = matrix.getHeight();140 BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);141 for (int x = 0; x < width; x++) {142 for (int y = 0; y < height; y++) {143 image.setRGB(x, y, matrix.get(x, y) == true ? BLACK : WHITE);144 }145 }146 return image;147 }148 149 /**150 * 解析QRCode二維碼151 */152 @SuppressWarnings("unchecked")153 public void decode(File file) {154 try {155 BufferedImage image;156 try {157 image = ImageIO.read(file);158 if (image == null) {159 System.out.println("Could not decode image");160 }161 LuminanceSource source = new BufferedImageLuminanceSource(image);162 BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));163 Result result;164 @SuppressWarnings("rawtypes")165 Hashtable hints = new Hashtable();166 //解碼設定編碼方式為:utf-8167 hints.put(DecodeHintType.CHARACTER_SET, "utf-8");168 result = new MultiFormatReader().decode(bitmap, hints);169 String resultStr = result.getText();170 System.out.println("解析後內容:" + resultStr);171 } catch (IOException ioe) {172 System.out.println(ioe.toString());173 } catch (ReaderException re) {174 System.out.println(re.toString());175 }176 } catch (Exception ex) {177 System.out.println(ex.toString());178 }179 }180 }

是不是很簡單.....

源碼下載:http://files.cnblogs.com/hongten/QRCodes.rar

java 二維碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.