具體代碼如下:
imgStr是一個base64字符串
public static String decoderQRCodeForBase64(String imgStr) throws Exception {
if (imgStr == null) {
return "";
}
BASE64Decoder decoder = new BASE64Decoder();
byte[] b = decoder.decodeBuffer(imgStr);
for (int i = 0; i < b.length; ++i) {
if (b[i] < 0) {// 調整異常數據
b[i] += 256;
}
}
InputStream input = new ByteArrayInputStream(b);
String content = decoderQRCode(input);
return content;
}
public static String decoderQRCode(InputStream input) throws Exception {
BufferedImage bufImg = null;
String content = null;
bufImg = ImageIO.read(input);
QRCodeDecoder decoder = new QRCodeDecoder();
content = new String(decoder.decode(new TwoDimensionCodeImage(bufImg)),
"utf-8");
return content;
}
在bufImg = ImageIO.read(input);這裡讀不出來,返回的是null,為什麼呢,求大神解答,小白在線等。
捕獲下異常,看看運行時有沒有異常信息。