gpt4 book ai didi

java - 即使图片中存在条形码,也会出现 NotFoundException

转载 作者:太空宇宙 更新时间:2023-11-04 06:10:22 25 4
gpt4 key购买 nike

我正在尝试制作一个简单的条形码扫描仪项目来娱乐。我遇到了一个小问题。为此,我使用 zXing 和 Webcam Capture。

即使图片中存在条形码,Java 也会通过 NotFoundException 不断告诉我没有找到条形码。我每次运行 webcamImageObtained 时都会查找一个帧(我假设是每一帧?),然后在捕获的帧中查找条形码。

我用那个网络摄像头拍了这张照片(讽刺的是使用代码哈哈):

barcode

当我将鼠标悬停在这个条形码上时,它报告每秒大约 30 个图像,否则当它从我的屏幕上看我时大约每秒 7-8 个图像(如果这意味着什么的话)。

每当我找到代码时,我都想将其添加到 JList (尚未考虑重复项和类似内容)。

每次 webcamImageObtained(WebcamEvent we) 触发时,我都会调用此代码:

@Override
public void webcamImageObtained(WebcamEvent we) {
BufferedImage myImage;
try {
myImage = webcam.getImage();
LuminanceSource source = new BufferedImageLuminanceSource(myImage);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Result result = reader.decode(bitmap);
DefaultListModel dlm = (DefaultListModel) list.getModel();
dlm.addElement(result.toString());
list.setModel(dlm);
} catch (NotFoundException ex) {
Logger.getLogger(AdvancedWebcamPanelExample.class.getName()).log(Level.SEVERE, null, ex);
} catch (ChecksumException ex) {
Logger.getLogger(AdvancedWebcamPanelExample.class.getName()).log(Level.SEVERE, null, ex);
} catch (FormatException ex) {
Logger.getLogger(AdvancedWebcamPanelExample.class.getName()).log(Level.SEVERE, null, ex);
}
}

这是整个类(class):

package sandbox_webcam;

import com.github.sarxos.webcam.Webcam;
import com.github.sarxos.webcam.WebcamDiscoveryEvent;
import com.github.sarxos.webcam.WebcamDiscoveryListener;
import com.github.sarxos.webcam.WebcamEvent;
import com.github.sarxos.webcam.WebcamListener;
import com.github.sarxos.webcam.WebcamPanel;
import com.github.sarxos.webcam.WebcamPicker;
import com.github.sarxos.webcam.WebcamResolution;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.ChecksumException;
import com.google.zxing.FormatException;
import com.google.zxing.LuminanceSource;
import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.lang.Thread.UncaughtExceptionHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;

public class AdvancedWebcamPanelExample extends JFrame implements Runnable, WebcamListener, WindowListener, UncaughtExceptionHandler, ItemListener, WebcamDiscoveryListener {

private Webcam webcam = null;
private WebcamPanel panel = null;
private WebcamPicker picker = null;
private JButton button = null;
private JList list = null;
private ActionListener buttonListener = null;
private com.google.zxing.Reader reader = new com.google.zxing.MultiFormatReader();

@Override
public void run() {
Webcam.addDiscoveryListener(this);

setTitle("Java Webcam Capture POC");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());

addWindowListener(this);

picker = new WebcamPicker();
picker.addItemListener(this);

webcam = picker.getSelectedWebcam();

if (webcam == null) {
System.out.println("No webcams found...");
System.exit(1);
}

webcam.setViewSize(WebcamResolution.VGA.getSize());
webcam.addWebcamListener(AdvancedWebcamPanelExample.this);

panel = new WebcamPanel(webcam, false);
panel.setFPSDisplayed(true);

buttonListener = new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
if (webcam != null) {
BufferedImage image = webcam.getImage();
JFileChooser filechooser = new JFileChooser();

int saveValue = filechooser.showDialog(button, "Save");
if (saveValue == JFileChooser.APPROVE_OPTION) {
try {
File f = filechooser.getSelectedFile();
ImageIO.write(image, "png", new File(f.getAbsolutePath() + ".png"));
System.out.println("Picture saved at: " + f.getAbsolutePath());
} catch (IOException ex) {
System.err.println("Failed to save the picture!");
ex.printStackTrace();
}
}
} else {
System.err.println("no webcam found to take a picture");
}
}
};

button = new JButton("Snap a Picture!");
button.addActionListener(buttonListener);
list = new JList();
list.setMinimumSize(new Dimension(200,this.getHeight()));

add(picker, BorderLayout.NORTH);
add(panel, BorderLayout.CENTER);
add(button, BorderLayout.SOUTH);
add(list, BorderLayout.EAST);

pack();
setVisible(true);

Thread t = new Thread() {
@Override
public void run() {
panel.start();
}
};

t.setName("example-starter");
t.setDaemon(true);
t.setUncaughtExceptionHandler(this);
t.start();
}

@Override
public void webcamOpen(WebcamEvent we) {
System.out.println("webcam open");
}

@Override
public void webcamClosed(WebcamEvent we) {
System.out.println("webcam closed");
}

@Override
public void webcamDisposed(WebcamEvent we) {
System.out.println("webcam disposed");
}

@Override
public void webcamImageObtained(WebcamEvent we) {
BufferedImage myImage;
try {
myImage = webcam.getImage();
LuminanceSource source = new BufferedImageLuminanceSource(myImage);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Result result = reader.decode(bitmap);
DefaultListModel dlm = (DefaultListModel) list.getModel();
dlm.addElement(result.toString());
list.setModel(dlm);
} catch (NotFoundException ex) {
Logger.getLogger(AdvancedWebcamPanelExample.class.getName()).log(Level.SEVERE, null, ex);
} catch (ChecksumException ex) {
Logger.getLogger(AdvancedWebcamPanelExample.class.getName()).log(Level.SEVERE, null, ex);
} catch (FormatException ex) {
Logger.getLogger(AdvancedWebcamPanelExample.class.getName()).log(Level.SEVERE, null, ex);
}
}

@Override
public void windowOpened(WindowEvent e) {
// do nothing
}

@Override
public void windowClosing(WindowEvent e) {
// do nothing
}

@Override
public void windowClosed(WindowEvent e) {
webcam.close();
}

@Override
public void windowIconified(WindowEvent e) {
System.out.println("webcam viewer paused");
panel.pause();
}

@Override
public void windowDeiconified(WindowEvent e) {
System.out.println("webcam viewer resumed");
panel.resume();
}

@Override
public void windowActivated(WindowEvent e) {
// do nothing
}

@Override
public void windowDeactivated(WindowEvent e) {
// do nothing
}

@Override
public void uncaughtException(Thread t, Throwable e) {
System.err.println(String.format("Exception in thread #s", t.getName()));
e.printStackTrace();
}

@Override
public void itemStateChanged(ItemEvent e) {
if (e.getItem() != webcam) {
if (webcam != null) {
panel.stop();

remove(panel);
webcam.removeWebcamListener(this);
webcam.close();

webcam = (Webcam) e.getItem();
webcam.setViewSize(WebcamResolution.VGA.getSize());
webcam.addWebcamListener(this);

System.out.println("selected " + webcam.getName());

panel = new WebcamPanel(webcam, false);
panel.setFPSDisplayed(true);

add(panel, BorderLayout.CENTER);
pack();

Thread t = new Thread() {
@Override
public void run() {
panel.start();
}
};

t.setName("example-stopper");
t.setDaemon(true);
t.setUncaughtExceptionHandler(this);
t.start();
}
}
}

@Override
public void webcamFound(WebcamDiscoveryEvent event) {
if (picker != null) {
picker.addItem(event.getWebcam());
}
}

@Override
public void webcamGone(WebcamDiscoveryEvent event) {
if (picker != null) {
picker.removeItem(event.getWebcam());
}
}

}

我是否遗漏了有关该库如何扫描条形码的信息?

编辑

不确定这有多大帮助..

Mar 02, 2015 10:04:34 PM sandbox_webcam.AdvancedWebcamPanelExample webcamImageObtained
SEVERE: null
com.google.zxing.NotFoundException

这里抛出异常:

Result result = reader.decode(bitmap);

最佳答案

有一个不同的问题,有一些可用的答案:Android zxing NotFoundException

正如詹姆斯所说,如果条码不起作用,并且在不同的环境和照明条件下,尝试在不同的介质(纸张/屏幕)上使用条码是一个好主意。特别要确保光线充足,并且摄像头对准条码时的 FPS 足够高。

为了进行调试,还可以将BinaryImage转换回可查看的格式,并检查转换为黑白后条形码是否实际上可见。

关于java - 即使图片中存在条形码,也会出现 NotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28819542/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com