最近在完成一個大作業(yè),反正一個小部分就是掃描二維碼,跳轉(zhuǎn)到一個界面去,搜網(wǎng)上也沒有什么太有用的信息,覺得難死了。。 后來想想,以前寫過一個程序,就是把字符串生成相應(yīng)的二維碼,然后我就抱著試試看的心態(tài),把url 放進(jìn)去,掃一下看看,結(jié)果,成功了。。。瞎貓碰著死老鼠,真幸運(yùn)~~
可以參考我的這篇文章: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 ,掃描之后就跳轉(zhuǎn)到這個界面
String path = 'D:/'; //圖片生成的位置
int width = 900;
int height = 900;
// 二維碼圖片格式
String format = 'gif';
// 設(shè)置編碼,防止中文亂碼
Hashtable<EncodeHintType, Object> ht = new Hashtable<EncodeHintType, Object> ();
ht.put (EncodeHintType.CHARACTER_SET, 'UTF-8');
// 設(shè)置二維碼參數(shù)(編碼內(nèi)容,編碼類型,圖片寬度,圖片高度,格式)
BitMatrix bitMatrix = new MultiFormatWriter ().encode (text, BarcodeFormat.QR_CODE, width, height, ht);
// 生成二維碼(定義二維碼輸出服務(wù)器路徑)
File outputFile = new File (path);
if (!outputFile.exists ())
{
//創(chuàng)建文件夾
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
}
}
|