最近在某論壇上看到Zxing 這個插件 , 決定用用 !首先它是用于生成二維碼的 , 類似于登錄時的驗證碼 , 他也是從后臺生成一張圖片 , 只不過二維碼圖案涉及一定算法 , 并且有一定的規(guī)律 ,這些都是前輩們定好了 ,并且已經(jīng)把這里面復(fù)雜的代碼封裝了 , 接下來的工作就簡單了!相關(guān)文章:http://developer.51cto.com/art/201310/414082_all.htm
首先在網(wǎng)上搜索Zxing ,或者去 maven repository下載zxing jar!
1、手動添加如下代碼(核心實現(xiàn))

1 package com.wisesoft.ztree.util; 2 import com.google.zxing.common.BitMatrix; 3 import javax.imageio.ImageIO; 4 import java.io.File; 5 import java.io.OutputStream; 6 import java.io.IOException; 7 import java.awt.image.BufferedImage; 8 public final class MatrixToImageWriter { 9 PRivate static final int BLACK = 0xFF000000;10 private static final int WHITE = 0xFFFFFFFF;11 private MatrixToImageWriter() {12 }13 public static BufferedImage toBufferedImage(BitMatrix matrix) {14 int width = matrix.getWidth();15 int height = matrix.getHeight();16 BufferedImage image = new BufferedImage(width, height,17 BufferedImage.TYPE_INT_RGB);18 for (int x = 0; x < width; x++) {19 for (int y = 0; y < height; y++) {20 image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);21 }22 }23 return image;24 }25 public static void writeToFile(BitMatrix matrix, String format, File file)26 throws IOException {27 BufferedImage image = toBufferedImage(matrix);28 if (!ImageIO.write(image, format, file)) {29 throw new IOException("Could not write an image of format "30 + format + " to " + file);31 }32 }33 public static void writeToStream(BitMatrix matrix, String format,34 OutputStream stream) throws IOException {35 BufferedImage image = toBufferedImage(matrix);36 if (!ImageIO.write(image, format, stream)) {37 throw new IOException("Could not write an image of format "38 + format);39 }40 }41 }42 43MatrixToImageWriter
“matrix.get(x, y) ? BLACK : WHITE” MatrixToImageWriter 類是繪制圖形的核心類 , 通過循環(huán)每一個像素點 , 并且詢問BitMatrix.get(x,y)返回時true或者false 決定繪制黑白條紋
2、二維碼規(guī)格定義部分

1 package com.wisesoft.ztree.controller; 2 import java.io.IOException; 3 import javax.servlet.http.HttpServletRequest; 4 import javax.servlet.http.HttpServletResponse; 5 import org.springframework.stereotype.Controller; 6 import org.springframework.web.bind.annotation.RequestMapping; 7 import com.google.zxing.BarcodeFormat; 8 import com.google.zxing.MultiFormatWriter; 9 import com.google.zxing.WriterException;10 import com.google.zxing.common.BitMatrix;11 import com.wisesoft.ztree.util.MatrixToImageWriter;12 13 @Controller14 @RequestMapping("/qr")15 public class QRController {16 @RequestMapping("put")17 public void qr(HttpServletRequest req,HttpServletResponse resp) throws WriterException, IOException{18 String contents = "http://sqdd.myapp.com/myapp/QQteam/AndroidQQ/mobileqq_android.apk"; //二維碼鏈接地址19 int width = 300; //生成二維碼寬度20 int height = 300; //高度21 String format = "png"; //二維碼文件格式 22 BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height); //xzing插件核心,將參數(shù)傳入23 //MatrixToImageWriter.writeToFile(bitMatrix,format, new File("D://qq_download.png")); //將生成的二維碼寫入文件中24 MatrixToImageWriter.writeToStream(bitMatrix,format, resp.getOutputStream()); //將生成的二維碼放入流中25 //MatrixToImageWriter.toBufferedImage(matrix); 26 }27 }QRController writeToStream 的三個參數(shù) , 第一個是BitMatrix對象 ,第二個 是format為圖片格式 ,第三個是一個輸出流對象
如果format格式不對:
MatrixToImageWriter.writeToStream(bitMatrix,"aaaaa", resp.getOutputStream());
異常:
"Could not write an image of format aaaaa"
新聞熱點
疑難解答