zxing解析二維碼拋出com.google.zxing.NotFoundException的解決過程
1.二維碼所有bit都是0
分析了一下,發(fā)現(xiàn)我在生成二維碼的時候白色像素填充使用的是透明色,這樣在顯示的時候因為背景是白色,所以看上去和用手機(jī)掃都沒有問題,但是自己代碼識別的時候就會把透明色識別為黑色,這樣就導(dǎo)致整個二維碼圖片全是黑色像素,所以zxing拋出com.google.zxing.NotFoundException異常。
2.亂碼
// 解碼設(shè)置編碼方式為:utf-8, hints.put(DecodeHintType.CHARACTER_SET, CHARSET);
3.優(yōu)化精度
//優(yōu)化精度 hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
4.開啟PURE_BARCODE模式。
(這是解決我的方案,帶圖片LOGO的解碼方案)
//復(fù)雜模式,開啟PURE_BARCODE模式 hints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
/**
* 流圖片解碼
* @param input
* @return QRResult
*/
public static QRResult decode(InputStream input) {
BufferedImage image;
try {
if (null == input) {
return new QRResult("得到的文件不存在!",300);
}
image = ImageIO.read(input);
LuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Map<DecodeHintType,Object> hints = new LinkedHashMap<DecodeHintType,Object>();
// 解碼設(shè)置編碼方式為:utf-8,
hints.put(DecodeHintType.CHARACTER_SET, CHARSET);
//優(yōu)化精度
hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
//復(fù)雜模式,開啟PURE_BARCODE模式
hints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
Result result = new MultiFormatReader().decode(bitmap, hints);
String txt = result.getText();
return new QRResult("成功解碼!",200,txt);
} catch (Exception e) {
LoggerUtils.error(MatrixUtil.class,"解碼失敗。", e);
return new QRResult("解碼失敗,請確認(rèn)的你二維碼是否正確,或者圖片有多個二維碼!",500);
}
}
/**
* 返回值處理
* @author zhou-baicheng
*
*/
public static class QRResult{
public QRResult(String message,int status) {
this.message = message;
this.status = status;
this.txt = "";
}
public QRResult(String message,int status,String txt) {
this.message = message;
this.status = status;
this.txt = txt;
}
//解碼內(nèi)容
private String txt;
//返回的消息內(nèi)容
private String message;
//返回的狀態(tài)碼,200:成功,500:錯誤
private int status ;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public String getTxt() {
return txt;
}
public void setTxt(String txt) {
this.txt = txt;
}
}
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java超詳細(xì)講解如何生成隨機(jī)整數(shù)
在?Java?中,生成隨機(jī)數(shù)的場景有很多,所以本文我們就來盤點(diǎn)一下?幾種生成隨機(jī)數(shù)的方式,以及它們之間的區(qū)別和每種生成方式所對應(yīng)的場景2022-05-05
SpringBoot2.x中management.security.enabled=false無效的解決
這篇文章主要介紹了SpringBoot2.x中management.security.enabled=false無效的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07
Eclipse手動導(dǎo)入DTD文件實(shí)現(xiàn)方法解析
這篇文章主要介紹了Eclipse手動導(dǎo)入DTD文件實(shí)現(xiàn)方法解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-10-10
springboot整合mybatis-plus 實(shí)現(xiàn)分頁查詢功能
這篇文章主要介紹了springboot整合mybatis-plus 實(shí)現(xiàn)分頁查詢功能,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09

