【发布时间】:2015-06-24 19:55:54
【问题描述】:
我正在尝试从从 jsp 文件上传的图像文件中读取 qrcode。为了读取二维码,我使用了 zxing jars。
代码来自
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Map;
import javax.imageio.ImageIO;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
public class GenerateQRCode {
public String readQRCode(String filePath, String charset)
throws FileNotFoundException, IOException, NotFoundException {
Hashtable hintMap = new Hashtable();
hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(
new BufferedImageLuminanceSource( ImageIO.read(new FileInputStream(filePath)))));
**Result qrCodeResult = new MultiFormatReader().decode(binaryBitmap, hintMap);**
return qrCodeResult.getText();
}
}
这是我试图在字符串“result”中获取 qrcode 值的方法。
String result = rr.readQRCode(tmpFile.getCanonicalPath(), "UTF-8");
上述调用的方法在粗线处抛出以下错误。 com.google.zxing.NotFoundException
我已经在 stackoverflow 中找到了相同问题的重复。
http://stackoverflow.com/questions/27770665/error-when-decoding-qr-code
但没有适当的回应。此代码是否有效。还是我应该寻找替代方案。我已经完成了生成二维码的代码。从文件中读取代码是 zxing 的问题。
【问题讨论】:
-
你能发布实际的异常文本(堆栈跟踪)吗?