場景:移動支付須要對二維碼的生成與部署有所瞭解,掌握目前主流的二維碼生成技術。html
首先說下,QRCode是日本人開發的,ZXing是google開發,barcode4j也是老美開發的,barcode4j對一維條形碼處理的很好,並且支持的格式不少,固然也能夠對二維碼進行處理,效果我的感受沒有前兩種好;ZXing對j2me,j2se,還有Android等支持也比較好,若是你是搞Android的或之後準備走Android,建議仍是用zxing的比較好,畢竟都一個母親(goole)生的,QRCode就不用說了吧,雖然說技術無國界,可是國人仍是有點...。java
好,言歸正傳,java用ZXing開發二維碼,首先須要添加code.jar包,這個包須要本身生成下,官網下載ZXing-2.3.0(目前最新的)後,解壓 把code中的代碼拷貝到一個工程中生成jar包。ZXing-code-2.3.0.jar包和源碼包本人已編譯後,不想本身從新編譯下的能夠直接下載使用的,下面的代碼就是使用這個jar包的,ZXing-code-2.3.0.jar下載地址:http://download.csdn.net/detail/sanfye/8704583 jquery
Code源碼包:http://download.csdn.net/detail/sanfye/8704593微信
負責將二維碼矩陣輸出到圖片上面。測試
ZXing 生成二維碼google
二維碼的生成能夠參考Google代碼測試包中的MatrixToImageWriter類來輔助開發,能夠將該類直接拷貝到源碼中使用 (code/src/test/java ),固然你也能夠本身寫個,我的感受不必因此就直接考過來用了。另外,在生成帶圖片的二維碼時要注意下,參數中設置的容錯級別儘可能設置到最高,以避免出現沒法解析的狀況:hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);編碼
(對於「Google代碼測試包中的MatrixToImageWriter」我在做者說的jar包中並無看到,只是按照下面的步驟生成二維碼,留下個疑問???)spa
package t1; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.io.OutputStream; import javax.imageio.ImageIO; import com.google.zxing.common.BitMatrix; /** * 二維碼的生成須要藉助MatrixToImageWriter類,該類是由Google提供的,能夠將該類直接拷貝到源碼中使用,固然你也能夠本身寫個 * 生產條形碼的基類 */ public class MatrixToImageWriter { private static final int BLACK = 0xFF000000;//用於設置圖案的顏色 private static final int WHITE = 0xFFFFFFFF; //用於背景色 private MatrixToImageWriter() { } public static BufferedImage toBufferedImage(BitMatrix matrix) { int width = matrix.getWidth(); int height = matrix.getHeight(); BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { image.setRGB(x, y, (matrix.get(x, y) ? BLACK : WHITE)); // image.setRGB(x, y, (matrix.get(x, y) ? Color.YELLOW.getRGB() : Color.CYAN.getRGB())); } } return image; } public static void writeToFile(BitMatrix matrix, String format, File file) throws IOException { BufferedImage image = toBufferedImage(matrix); //設置logo圖標 LogoConfig logoConfig = new LogoConfig(); image = logoConfig.LogoMatrix(image); if (!ImageIO.write(image, format, file)) { throw new IOException("Could not write an image of format " + format + " to " + file); }else{ System.out.println("圖片生成成功!"); } } public static void writeToStream(BitMatrix matrix, String format, OutputStream stream) throws IOException { BufferedImage image = toBufferedImage(matrix); //設置logo圖標 LogoConfig logoConfig = new LogoConfig(); image = logoConfig.LogoMatrix(image); if (!ImageIO.write(image, format, stream)) { throw new IOException("Could not write an image of format " + format); } } }
主要處理logo圖標已達到和微信自動的二維碼相近的效果:.net
package t1; import java.awt.BasicStroke; import java.awt.Color; import java.awt.Graphics2D; import java.awt.geom.RoundRectangle2D; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; /** * 二維碼 添加 logo圖標 處理的方法, * 模仿微信自動生成二維碼效果,有圓角邊框,logo和二維碼間有空白區,logo帶有灰色邊框 * @author Administrator sangwenhao * */ public class LogoConfig { /** * 設置 logo * @param matrixImage 源二維碼圖片 * @return 返回帶有logo的二維碼圖片 * @throws IOException * @author Administrator sangwenhao */ public BufferedImage LogoMatrix(BufferedImage matrixImage) throws IOException{ /** * 讀取二維碼圖片,並構建繪圖對象 */ Graphics2D g2 = matrixImage.createGraphics(); int matrixWidth = matrixImage.getWidth(); int matrixHeigh = matrixImage.getHeight(); /** * 讀取Logo圖片 */ BufferedImage logo = ImageIO.read(new File("E:\\heack.jpg")); //開始繪製圖片 g2.drawImage(logo,matrixWidth/5*2,matrixHeigh/5*2, matrixWidth/5, matrixHeigh/5, null);//繪製 BasicStroke stroke = new BasicStroke(5,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND); g2.setStroke(stroke);// 設置筆畫對象 //指定弧度的圓角矩形 RoundRectangle2D.Float round = new RoundRectangle2D.Float(matrixWidth/5*2, matrixHeigh/5*2, matrixWidth/5, matrixHeigh/5,20,20); g2.setColor(Color.white); g2.draw(round);// 繪製圓弧矩形 //設置logo 有一道灰色邊框 BasicStroke stroke2 = new BasicStroke(1,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND); g2.setStroke(stroke2);// 設置筆畫對象 RoundRectangle2D.Float round2 = new RoundRectangle2D.Float(matrixWidth/5*2+2, matrixHeigh/5*2+2, matrixWidth/5-4, matrixHeigh/5-4,20,20); g2.setColor(new Color(128,128,128)); g2.draw(round2);// 繪製圓弧矩形 g2.dispose(); matrixImage.flush() ; return matrixImage ; } }
package t1; import java.io.File; import java.io.IOException; import java.util.Hashtable; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; import com.google.zxing.WriterException; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; public class EncodeTest { public static void Encode_QR_CODE() throws IOException, WriterException{ String contents = "ZXing 二維碼內容1234!"; // 二維碼內容 int width = 430; // 二維碼圖片寬度 300 int height = 430; // 二維碼圖片高度300 String format = "gif";// 二維碼的圖片格式 gif Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>(); // 指定糾錯等級,糾錯級別(L 7%、M 15%、Q 25%、H 30%) hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); // 內容所使用字符集編碼 hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); // hints.put(EncodeHintType.MAX_SIZE, 350);//設置圖片的最大值 // hints.put(EncodeHintType.MIN_SIZE, 100);//設置圖片的最小值 hints.put(EncodeHintType.MARGIN, 1);//設置二維碼邊的空度,非負數 BitMatrix bitMatrix = new MultiFormatWriter().encode(contents,//要編碼的內容 //編碼類型,目前zxing支持:Aztec 2D,CODABAR 1D format,Code 39 1D,Code 93 1D ,Code 128 1D, //Data Matrix 2D , EAN-8 1D,EAN-13 1D,ITF (Interleaved Two of Five) 1D, //MaxiCode 2D barcode,PDF417,QR Code 2D,RSS 14,RSS EXPANDED,UPC-A 1D,UPC-E 1D,UPC/EAN extension,UPC_EAN_EXTENSION BarcodeFormat.QR_CODE, width, //條形碼的寬度 height, //條形碼的高度 hints);//生成條形碼時的一些配置,此項可選 // 生成二維碼 File outputFile = new File("e:" + File.separator + "new-1.gif");//指定輸出路徑 MatrixToImageWriter.writeToFile(bitMatrix, format, outputFile); } public static void main(String[] args) throws Exception { try { Encode_QR_CODE(); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } }
效果圖:code
本博客與二維碼相關的文章: