Par*_*nak 9 java exception barcode zxing
我有一个带有二维条形码的jpeg文件.图像分辨率为1593X1212.我正在使用xing库从图像中解码这个条形码.我在网上得到了以下代码.
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.ChecksumException;
import com.google.zxing.FormatException;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.NotFoundException;
import com.google.zxing.Reader;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;
public class NewLibTest {
public static void main(String args[]){
System.out.println(decode(new File("E:\\xyz.jpg")));
}
/**
* Decode method used to read image or barcode itself, and recognize the barcode,
* get the encoded contents and returns it.
* @param <DecodeHintType>
* @param file image that need to be read.
* @param config configuration used when reading the barcode.
* @return decoded results from barcode.
*/
public static String decode(File file){//, Map<DecodeHintType, Object> hints) throws Exception {
// check the required parameters
if (file == null || file.getName().trim().isEmpty())
throw new IllegalArgumentException("File not found, or invalid file name.");
BufferedImage image = null;
try {
image = ImageIO.read(file);
} catch (IOException ioe) {
try {
throw new Exception(ioe.getMessage());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (image == null)
throw new IllegalArgumentException("Could not decode image.");
LuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
MultiFormatReader barcodeReader = new MultiFormatReader();
Result result;
String finalResult = null;
try {
//if (hints != null && ! hints.isEmpty())
// result = barcodeReader.decode(bitmap, hints);
//else
result = barcodeReader.decode(bitmap);
// setting results.
finalResult = String.valueOf(result.getText());
} catch (Exception e) {
e.printStackTrace();
// throw new BarcodeEngine().new BarcodeEngineException(e.getMessage());
}
return finalResult;
}
Run Code Online (Sandbox Code Playgroud)
}
当我执行这个简单的核心java程序时,我给出了异常
com.google.zxing.NotFoundException
Run Code Online (Sandbox Code Playgroud)
它甚至没有给任何stackstrace.
我想问专家,为什么会出现这种异常.谢谢!
小智 10
我有同样的问题.我使用了一个我知道有一个有效QR码的图像,我也得到了com.google.zxing.NotFoundException.
问题是您用作源的图像对于要解码的库来说很大.在缩小图像尺寸后,QR码解码器工作.
出于我的应用的目的,图像上的QR码总是或多或少在同一区域,因此我使用BufferedImage类的getSubimage函数来隔离QR码.
BufferedImage image;
image = ImageIO.read(imageFile);
BufferedImage cropedImage = image.getSubimage(0, 0, 914, 400);
// using the cropedImage instead of image
LuminanceSource source = new BufferedImageLuminanceSource(cropedImage);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
// barcode decoding
QRCodeReader reader = new QRCodeReader();
Result result = null;
try
{
result = reader.decode(bitmap);
}
catch (ReaderException e)
{
return "reader error";
}
Run Code Online (Sandbox Code Playgroud)
小智 6
我有同样的问题.当我在Java SE库上运行几乎完全相同的代码时,它可以工作.当我使用相同的图片运行Android代码时,它无法正常工作.花了很多时间试图找出......
您可以使用缩放位图
Bitmap resize = Bitmap.createScaledBitmap(srcBitmap, dstWidth,dstHeight,false);
问题:你必须打开一些标志.几乎所有标志都在玩这个解决方案对我有用:
Map<DecodeHintType, Object> tmpHintsMap = new EnumMap<DecodeHintType, Object>(
DecodeHintType.class);
tmpHintsMap.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
tmpHintsMap.put(DecodeHintType.POSSIBLE_FORMATS,
EnumSet.allOf(BarcodeFormat.class));
tmpHintsMap.put(DecodeHintType.PURE_BARCODE, Boolean.FALSE);
Run Code Online (Sandbox Code Playgroud)
...
MultiFormatReader mfr = null;
mfr = new MultiFormatReader();
result = mfr.decode(binaryBitmap, tmpHintsMap);
Run Code Online (Sandbox Code Playgroud)问题:ZXing的Android库运行一次条形码扫描,假设图片上的条形码已经有正确的方向.如果不是这种情况,你必须运行四次,每次旋转图片大约90度!
对于旋转,您可以使用此方法.角度是以度为单位的角度.
public Bitmap rotateBitmap(Bitmap source, float angle)
{
Matrix matrix = new Matrix();
matrix.postRotate(angle);
return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
}
Run Code Online (Sandbox Code Playgroud)