最近在完成一個大作業,反正一個小部分就是掃描二維碼,跳轉到一個界面去,搜網上也沒有什麼太有用的信息,覺得難死了。。 後來想想,以前寫過一個程序,就是把字符串生成相應的二維碼,然後我就抱着試試看的心態,把url 放進去,掃一下看看,結果,成功了。。。瞎貓碰着死老鼠,真幸運~~
可以參考我的這篇文章:http://blog.csdn.net/prayallforyou/article/details/51417807
public class test {
private static final int BLACK = 0xFF000000;
private static final int WHITE = 0xFFFFFFFF;
public static void main ( String[] args ) throws Exception
{
String text = "https://www.baidu.com/"; //這裏是URL ,掃描之後就跳轉到這個界面
String path = "D:/"; //圖片生成的位置
int width = 900;
int height = 900;
// 二維碼圖片格式
String format = "gif";
// 設置編碼,防止中文亂碼
Hashtable<EncodeHintType, Object> ht = new Hashtable<EncodeHintType, Object> ();
ht.put (EncodeHintType.CHARACTER_SET, "UTF-8");
// 設置二維碼參數(編碼內容,編碼類型,圖片寬度,圖片高度,格式)
BitMatrix bitMatrix = new MultiFormatWriter ().encode (text, BarcodeFormat.QR_CODE, width, height, ht);
// 生成二維碼(定義二維碼輸出服務器路徑)
File outputFile = new File (path);
if (!outputFile.exists ())
{
//創建文件夾
outputFile.mkdir ();
}
int b_width = bitMatrix.getWidth ();
int b_height = bitMatrix.getHeight ();
// 建立圖像緩衝器
BufferedImage image = new BufferedImage (b_width, b_height, BufferedImage.TYPE_3BYTE_BGR);
for ( int x = 0; x < b_width; x++ )
{
for ( int y = 0; y < b_height; y++ )
{
image.setRGB (x, y, bitMatrix.get (x, y) ? BLACK : WHITE);
}
}
// 生成二維碼
ImageIO.write (image, format, new File (path + "/erweima." + format)); //二維碼的名稱 是 erweima.sgif
}
}