执行核心java程序时是否会出现异常?
我有一个带有二维条形码的jpeg文件。图像分辨率为1593X1212。我正在使用xing库从图像中解码此条形码。我在网上得到了以下代码执行核心java程序时是否会出现异常?,java,exception,barcode,zxing,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 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;
}
它甚至没有给任何斯塔克斯特拉斯
我想问专家们,为什么会出现这样的例外情况。
谢谢你 在图像中未找到条形码时引发该异常:
这是正常的;这只是意味着没有找到条形码。你还没有提供图像,所以我不能说你的图像是否可读,更不用说有支持的条形码格式了。我也有同样的问题。我使用了一张我知道有有效二维码的图像,我还得到了com.google.zxing.NotFoundException 问题是,您用作源的图像太大,库无法解码。在我缩小图像尺寸后,二维码解码器开始工作 在我的应用程序中,图像上的二维码总是或多或少地位于同一区域,因此我使用BuffereImage类的getSubimage函数来隔离二维码
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";
}
我也遇到了同样的问题,我调用了readQRCode(filePath、charset、hintMap);并且得到了同样的信息。我打电话给我用zxing图书馆写的图书馆。要解决这个问题,只需将(zxing)jar添加到顶级代码中,即使没有在那里访问库 我也有同样的问题。当我在JavaSE库上运行几乎完全相同的代码时,它工作正常。当我使用相同的图片运行Android代码时,它不起作用。花了很多时间试图找出
Bitmap resize=Bitmap.createScaledBitmap(srcBitmap,dstWidth,dstWidth,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);
问题:假设图片上的条形码已经有正确的方向,中兴的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);
}
如果您使用
public static String readQRCode(String filePath, String charset, Map hintMap)
throws FileNotFoundException, IOException, NotFoundException {
BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(
new BufferedImageLuminanceSource(
ImageIO.read(new FileInputStream(filePath)))));
Result qrCodeResult = new MultiFormatReader().decode(binaryBitmap, tmpHintsMap);
return qrCodeResult.getText();
}
public static String readQRCode(String filePath, String charset, Map hintMap)
throws FileNotFoundException, IOException, NotFoundException {
BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(
new BufferedImageLuminanceSource(
ImageIO.read(new FileInputStream(filePath)))));
Result qrCodeResult = new MultiFormatReader().decode(binaryBitmap, tmpHintsMap);
return qrCodeResult.getText();
}
更改此代码。它正常工作
public static String readQRCode(String filePath, String charset, Map hintMap)
throws FileNotFoundException, IOException, NotFoundException {
Map < DecodeHintType, Object > tmpHintsMap = new EnumMap < DecodeHintType, Object > (
DecodeHintType.class);
//tmpHintsMap.put(DecodeHintType.TRY_HARDER, Boolean.FALSE);
//tmpHintsMap.put(DecodeHintType.POSSIBLE_FORMATS, EnumSet.allOf(BarcodeFormat.class));
tmpHintsMap.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(
new BufferedImageLuminanceSource(
ImageIO.read(new FileInputStream(filePath)))));
Result qrCodeResult = new MultiFormatReader().decode(binaryBitmap, tmpHintsMap);
return qrCodeResult.getText();
}
公共静态字符串readQRCode(字符串文件路径、字符串字符集、映射hintMap)
抛出FileNotFoundException、IOException、NotFoundException{
MaptmpHintsMap=newenummap(
DecodeHintType.class);
//tmpHintsMap.put(DecodeHintType.TRY_,Boolean.FALSE);
//tmpHintsMap.put(DecodeHintType.mables_格式,EnumSet.allOf(BarcodeFormat.class));
tmpHintsMap.put(DecodeHintType.PURE_条形码,布尔值.TRUE);
BinaryBitmap BinaryBitmap=新的BinaryBitmap(新的混合二进制程序(
新的BufferedImageLuminanceSource(
ImageIO.read(新的FileInputStream(filePath()())));
结果qrCodeResult=new multiformatrader().decode(二进制位图,tmpHintsMap);
返回qrCodeResult.getText();
}
这个解决方案适合我。我希望这对你有帮助。我用reader.decodeWithState(…)
替换reader.decodeWithState(…)
我已经在ImageAnalysis中调整了目标分辨率,它开始工作了
从
对这个
ImageAnalysis imageAnalysis =
new ImageAnalysis.Builder()
.setTargetResolution(new Size(700, 500))
.setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST)
.build();
谢谢你的回复,先生!但上面提供的图像包含2D条形码,条形码的分辨率约为84Pix X 82pix。那么,为什么上面的代码找不到图像上的条形码。您是否尝试过使用相同的代码但使用不同的条形码图像?是否有条形码样本图像可用于测试您的代码?是的,我已更改了图像并重新执行了程序,但它仍然给出了相同的例外情况。您是否找到了此问题的解决方案?我面临着相同的问题。请让我知道,建议重新检查其类路径上的JAR不太可能有助于搜索此内容具体错误。这与本主题无关。您如何“调整”精确度/搜索设置?有相关文件吗?当我有一个大的PNG(来自PDF),左上角有一个二维码,其他的都是空白,它找不到它…我不知道为什么会是否定的,但这个答案对我很有帮助。需要一个简单的try-catch来确保使用的位图中包含qr码。如果没有,并且使用了假图像,那么我们应该捕获错误并向用户显示一些消息。如果不使用,为什么要初始化解码提示?这让我浪费时间去写一些东西,结果我没有在另一幅图像中使用DecodeState成功检测到二维码。非常感谢。
public static String readQRCode(String filePath, String charset, Map hintMap)
throws FileNotFoundException, IOException, NotFoundException {
BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(
new BufferedImageLuminanceSource(
ImageIO.read(new FileInputStream(filePath)))));
Result qrCodeResult = new MultiFormatReader().decode(binaryBitmap, tmpHintsMap);
return qrCodeResult.getText();
}
public static String readQRCode(String filePath, String charset, Map hintMap)
throws FileNotFoundException, IOException, NotFoundException {
BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(
new BufferedImageLuminanceSource(
ImageIO.read(new FileInputStream(filePath)))));
Result qrCodeResult = new MultiFormatReader().decode(binaryBitmap, tmpHintsMap);
return qrCodeResult.getText();
}
public static String readQRCode(String filePath, String charset, Map hintMap)
throws FileNotFoundException, IOException, NotFoundException {
Map < DecodeHintType, Object > tmpHintsMap = new EnumMap < DecodeHintType, Object > (
DecodeHintType.class);
//tmpHintsMap.put(DecodeHintType.TRY_HARDER, Boolean.FALSE);
//tmpHintsMap.put(DecodeHintType.POSSIBLE_FORMATS, EnumSet.allOf(BarcodeFormat.class));
tmpHintsMap.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(
new BufferedImageLuminanceSource(
ImageIO.read(new FileInputStream(filePath)))));
Result qrCodeResult = new MultiFormatReader().decode(binaryBitmap, tmpHintsMap);
return qrCodeResult.getText();
}
MultiFormatReader reader = new MultiFormatReader();// use this otherwise
Result result = reader.decodeWithState(bitmap);
ImageAnalysis imageAnalysis =
new ImageAnalysis.Builder()
.setTargetResolution(new Size(mySurfaceView.getWidth(), mySurfaceView.getHeight()))
.setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST)
.build();
ImageAnalysis imageAnalysis =
new ImageAnalysis.Builder()
.setTargetResolution(new Size(700, 500))
.setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST)
.build();