微信掃描二維碼跳轉頁面
最近在完成一個大作業,反正一個小部分就是掃描二維碼,跳轉到一個介面去,搜網上也沒有什麼太有用的資訊,覺得難死了。。 後來想想,以前寫過一個程式,就是把字串生成相應的二維碼,然後我就抱著試試看的心態,把url 放進去,掃一下看看,結果,成功了。。。瞎貓碰著死老鼠,真幸運~~
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
}
}