Java 生成二維碼

jopen 12年前發布 | 282K 次閱讀 Java 條形碼/二維碼開發包

二維碼現在已經到處都是了,下面是二維碼的介紹
二維碼 ,又稱 二維條碼 , 二維條形碼最早發明于日本,它是用某種特定的幾何圖形按一定規律在平面(二維方向上)分布的黑白相間的圖形記錄數據符號信息的,在代碼編制上巧妙地利用構 成計算機內部邏輯基礎的“0”、“1”比特流的概念,使用若干個與二進制相對應的幾何形體來表示文字數值信息,通過圖象輸入設備或光電掃描設備自動識讀以 實現信息自動處理。它具有條碼技術的一些共性:每種碼制有其特定的字符集;每個字符占有一定的寬度;具有一定的校驗功能等。同時還具有對不同行的信息自動 識別功能、及處理圖形旋轉變化等特點。

1.java這邊的話生成二維碼有很多開發的jar包如zxing,qrcode.q前者是谷歌開發的后者則是小日本開發的,這里的話我使用zxing的開發包來弄


2.先下載zxing開發包,這里用到的只是core那個jar包


3.使用zxing開發還需要一個類,代碼如下

    package org.lxh;
import com.google.zxing.common.BitMatrix;

 import javax.imageio.ImageIO;  
 import java.io.File;  
 import java.io.OutputStream;  
 import java.io.IOException;  
 import java.awt.image.BufferedImage;  


 public final 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);  
       }  
     }  
     return image;  
   }  


   public static void writeToFile(BitMatrix matrix, String format, File file)  
       throws IOException {  
     BufferedImage image = toBufferedImage(matrix);  
     if (!ImageIO.write(image, format, file)) {  
       throw new IOException("Could not write an image of format " + format + " to " + file);  
     }  
   }  


   public static void writeToStream(BitMatrix matrix, String format, OutputStream stream)  
       throws IOException {  
     BufferedImage image = toBufferedImage(matrix);  
     if (!ImageIO.write(image, format, stream)) {  
       throw new IOException("Could not write an image of format " + format);  
     }  
   }  

 }  </pre>4.借助上面的類生成二維碼 <pre class="brush:java; toolbar: true; auto-links: false;">    package org.lxh;  

import java.io.File;  
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;  

public class Test {  

    /** 
     * @param args 
     * @throws Exception  
     */  
    public static void main(String[] args) throws Exception {  
        String text = "http://www.baidu.com";  
        int width = 300;  
        int height = 300;  
        //二維碼的圖片格式  
        String format = "gif";  
        Hashtable hints = new Hashtable();  
        //內容所使用編碼  
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");  
        BitMatrix bitMatrix = new MultiFormatWriter().encode(text,  
                BarcodeFormat.QR_CODE, width, height, hints);  
        //生成二維碼  
        File outputFile = new File("d:"+File.separator+"new.gif");  
        MatrixToImageWriter.writeToFile(bitMatrix, format, outputFile);  

    }  

}  </pre>text就是二維碼的內容里這里可以使普通的文字也可以是鏈接,很簡單吧最后把生成的二維碼圖片給大家 <br />


Java 生成二維碼

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!