




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
java二維碼生成與解析代碼實現(xiàn)java二維碼生成與解析代碼實現(xiàn)java二維碼生成與解析代碼實現(xiàn)java二維碼生成與解析代碼實現(xiàn)所需jar包:QRCode。jarTwoDimensionCode類:二維碼操作核心類[java]
png",
7);
}
/**
*
生成二維碼(QRCode)圖片
*
@param
content
存儲內(nèi)容
*
@param
output
輸出流
*/
public
void
encoderQRCode(String
content,
OutputStream
output)
{
this。encoderQRCode(content,
output,
"png",
7);
}
/**
*
生成二維碼(QRCode)圖片
*
@param
content
存儲內(nèi)容
*
@param
imgPath
圖片路徑
*
@param
imgType
圖片類型
*/
public
void
encoderQRCode(String
content,
String
imgPath,
String
imgType)
{
this.encoderQRCode(content,
imgPath,
imgType,
7);
}
/**
*
生成二維碼(QRCode)圖片
*
@param
content
存儲內(nèi)容
*
@param
output
輸出流
*
@param
imgType
圖片類型
*/
public
void
encoderQRCode(String
content,
OutputStream
output,
String
imgType)
{
this。encoderQRCode(content,
output,
imgType,
7);
}
/**
*
生成二維碼(QRCode)圖片
*
@param
content
存儲內(nèi)容
*
@param
imgPath
圖片路徑
*
@param
imgType
圖片類型
*
@param
size
二維碼尺寸
*/
public
void
encoderQRCode(String
content,
String
imgPath,
String
imgType,
int
size)
{
try
{
BufferedImage
bufImg
=
this。qRCodeCommon(content,
imgType,
size);
File
imgFile
=
new
File(imgPath);
//
生成二維碼QRCode圖片
ImageIO.write(bufImg,
imgType,
imgFile);
}
catch
(Exception
e)
{
e.printStackTrace();
}
}
/**
*
生成二維碼(QRCode)圖片
*
@param
content
存儲內(nèi)容
*
@param
output
輸出流
*
@param
imgType
圖片類型
*
@param
size
二維碼尺寸
*/
public
void
encoderQRCode(String
content,
OutputStream
output,
String
imgType,
int
size)
{
try
{
BufferedImage
bufImg
=
this.qRCodeCommon(content,
imgType,
size);
//
生成二維碼QRCode圖片
ImageIO。write(bufImg,
imgType,
output);
}
catch
(Exception
e)
{
e.printStackTrace();
}
}
/**
*
生成二維碼(QRCode)圖片的公共方法
*
@param
content
存儲內(nèi)容
*
@param
imgType
圖片類型
*
@param
size
二維碼尺寸
*
@return
*/
private
BufferedImage
qRCodeCommon(String
content,
String
imgType,
int
size)
{
BufferedImage
bufImg
=
null;
try
{
Qrcode
qrcodeHandler
=
new
Qrcode();
//
設(shè)置二維碼排錯率,可選L(7%)、M(15%)、Q(25%)、H(30%),排錯率越高可存儲的信息越少,但對二維碼清晰度的要求越小
qrcodeHandler.setQrcodeErrorCorrect('M’);
qrcodeHandler。setQrcodeEncodeMode('B’);
//
設(shè)置設(shè)置二維碼尺寸,取值范圍1-40,值越大尺寸越大,可存儲的信息越大
qrcodeHandler.setQrcodeVersion(size);
//
獲得內(nèi)容的字節(jié)數(shù)組,設(shè)置編碼格式
byte[]
contentBytes
=
content。getBytes("utf—8");
//
圖片尺寸
int
imgSize
=
67
+
12
*
(size
—
1);
bufImg
=
new
BufferedImage(imgSize,
imgSize,
BufferedImage.TYPE_INT_RGB);
Graphics2D
gs
=
bufImg.createGraphics();
//
設(shè)置背景顏色
gs。setBackground(Color。WHITE);
gs。clearRect(0,
0,
imgSize,
imgSize);
//
設(shè)定圖像顏色〉
BLACK
gs.setColor(Color.BLACK);
//
設(shè)置偏移量,不設(shè)置可能導(dǎo)致解析出錯
int
pixoff
=
2;
//
輸出內(nèi)容>
二維碼
if
(contentBytes。length
>
0
&&
contentBytes.length
<
800)
{
boolean[][]
codeOut
=
qrcodeHandler.calQrcode(contentBytes);
for
(int
i
=
0;
i
〈
codeOut.length;
i++)
{
for
(int
j
=
0;
j
〈
codeOut。length;
j++)
{
if
(codeOut[j][i])
{
gs。fillRect(j
*
3
+
pixoff,
i
*
3
+
pixoff,
3,
3);
}
}
}
}
else
{
throw
new
Exception(”QRCode
content
bytes
length
=
"
+
contentBytes.length
+
”
not
in
[0,
800]。");
}
gs.dispose();
bufImg。flush();
}
catch
(Exception
e)
{
e。printStackTrace();
}
return
bufImg;
}
/**
*
解析二維碼(QRCode)
*
@param
imgPath
圖片路徑
*
@return
*/
public
String
decoderQRCode(String
imgPath)
{
//
QRCode
二維碼圖片的文件
File
imageFile
=
new
File(imgPath);
BufferedImage
bufImg
=
null;
String
content
=
null;
try
{
bufImg
=
ImageIO.read(imageFile);
QRCodeDecoder
decoder
=
new
QRCodeDecoder();
content
=
new
String(decoder。decode(new
TwoDimensionCodeImage(bufImg)),
"utf-8”);
}
catch
(IOException
e)
{
System.out.println(”Error:
”
+
e.getMessage());
e。printStackTrace();
}
catch
(DecodingFailedException
dfe)
{
System。out.println(”Error:
”
+
dfe.getMessage());
dfe.printStackTrace();
}
return
content;
}
/**
*
解析二維碼(QRCode)
*
@param
input
輸入流
*
@return
*/
public
String
decoderQRCode(InputStream
input)
{
BufferedImage
bufImg
=
null;
String
content
=
null;
try
{
bufImg
=
ImageIO.read(input);
QRCodeDecoder
decoder
=
new
QRCodeDecoder();
content
=
new
String(decoder。decode(new
TwoDimensionCodeImage(bufImg)),
”utf-8”);
}
catch
(IOException
e)
{
System.out。println(”Error:
”
+
e。getMessage());
e.printStackTrace();
}
catch
(DecodingFailedException
dfe)
{
System.out.println("Error:
”
+
dfe.getMessage());
dfe.printStackTrace();
}
return
content;
}
public
static
void
main(String[]
args)
{
String
imgPath
=
"G:/TDDOWNLOAD/Michael_QRCode。png”;
String
encoderContent
=
”Hello
大大、小小,welcome
to
QRCode!”
+
”\nMyblog
[
aaa
]"
+
"\nEMail
[
bbb
]";
TwoDimensionCode
handler
=
new
TwoDimensionCode();
handler.encoderQRCode(encoderContent,
imgPath,
”png");
//
try
{
//
OutputStream
output
=
new
FileOutputStream(imgPath);
//
handler.encoderQRCode(content,
output);
//
}
catch
(Exception
e)
{
//
e.printStackTrace();
//
}
System.out.println("========encoder
success”);
String
decoderContent
=
handler.decoderQRCode(imgPath);
System.out.println("解析結(jié)果如下:");
System.out。println(decoderContent);
System.out。println(”========decoder
success!!!”);
}
}
T
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2024年甘肅蘭阿煤業(yè)有限責(zé)任公司招聘53人筆試參考題庫附帶答案詳解
- 24《司馬光》教學(xué)設(shè)計-2024-2025學(xué)年語文三年級上冊統(tǒng)編版五四制
- 5.1《水經(jīng)注》序教案-【中職專用】高二語文同步教學(xué)(高教版2023·拓展模塊下冊)
- 2025年河南輕工職業(yè)學(xué)院單招職業(yè)傾向性測試題庫1套
- 2025年桂林師范高等專科學(xué)校單招職業(yè)技能測試題庫及答案一套
- 2024年12月2025年甘肅慶陽環(huán)縣事業(yè)單位引進高層次和急需緊缺人才109人筆試歷年典型考題(歷年真題考點)解題思路附帶答案詳解
- 《第三單元 創(chuàng)建交互動畫 第14課 在網(wǎng)站上發(fā)布動畫 在網(wǎng)站上發(fā)布GIF動畫 》教學(xué)設(shè)計 -2023-2024學(xué)年人教版初中信息技術(shù)八年級上冊
- 2024中國旅游集團總部崗位公開招聘筆試參考題庫附帶答案詳解
- 2025年鐵路運輸服務(wù)項目發(fā)展計劃
- Unit 5 Topic 1 Section D 教學(xué)設(shè)計-2024-2025學(xué)年仁愛科普版八年級英語下冊
- 生物產(chǎn)品檢驗檢疫基礎(chǔ)知識單選題100道及答案
- 江蘇省中職《英語》學(xué)業(yè)水平考試備考試題集(含歷年真題)
- Unit 3 Environmental Protection Reading and Thinking 選擇性必修第三冊
- 2025年合伙型公司新合伙人加入?yún)f(xié)議
- 小學(xué)道德與法治課堂教學(xué)中提升學(xué)生核心素養(yǎng)策略
- 中水回用項目可行性研究報告
- 2025城市商鋪買賣合同書
- 基于主題式教學(xué)法的初級漢語綜合課《我的低碳生活》教學(xué)設(shè)計
- 微信公眾號運營及推廣合同
- 2025年春新北師大版物理八年級下冊課件 第六章 質(zhì)量和密度 第一節(jié) 物體的質(zhì)量及其測量
- 2024全國各省高考詩歌鑒賞真題及解析
評論
0/150
提交評論