Tôi có tệp jpeg có mã vạch 2D. Độ phân giải hình ảnh là 1593X1212. Tôi đang sử dụng thư viện xing để giải mã mã vạch này từ hình ảnh. Tôi đã nhận được mã sau trên mạng.ngoại lệ com.google.zxing.NotFoundException xảy ra khi chương trình java lõi được thực thi?
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;
}
}
Khi tôi thực hiện chương trình cốt lõi java đơn giản này tôi cho ngoại lệ
com.google.zxing.NotFoundException
của nó thậm chí không cung cấp cho bất kỳ stackstrace.
Tôi muốn hỏi các chuyên gia rằng tại sao loại ngoại lệ như vậy lại xuất hiện. Cảm ơn bạn!
Bạn có giải pháp về vấn đề này không? Tôi đang đối mặt với cùng một vấn đề ... vui lòng cho tôi biết –