我正在尝试读取从jsp文件上传的图像文件中的qrcode。为了阅读QRcode,我使用了zxing jar。
代码来自
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但是没有适当的回应。这段代码可以工作吗?或者我是不是应该另寻他法。我已经完成了生成qrcode的代码。从文件中读取代码是zxing的问题。
发布于 2015-06-24 19:55:55
我也有类似的问题,我找到了这个https://github.com/zxing/zxing/issues/216
您应该放入PURE_BARCODE提示。因此,您的代码应该是
// ...
Map<DecodeHintType, Object> hints = new EnumMap<>(DecodeHintType.class);
hints.put(DecodeHintType.PURE_BARCODE, true);
Result qrCodeResult = new MultiFormatReader().decode(binaryBitmap, hints);
return qrCodeResult.getText();
// ...https://stackoverflow.com/questions/27902661
复制相似问题