我的程序遇到了一些问题。我有一个GUI,用于显示网络摄像头的实时图像(一方使用[ jvacv ] [1]),另一方显示捕获的图像。为了捕捉图像,我有一个按钮。一个问题是,只有当我再次关闭并打开程序时,捕获的图像才会刷新。另一个是我想从网络摄像头捕获1080p图像,但实时图像为640x480。
以下是代码:
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.lang.ProcessBuilder.Redirect;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.SwingUtilities;
import com.googlecode.javacv.FrameGrabber;
import com.googlecode.javacv.OpenCVFrameGrabber;
import com.googlecode.javacv.FrameGrabber.Exception;
import com.googlecode.javacv.OpenCVFrameGrabber;
import com.googlecode.javacv.cpp.opencv_core.IplImage;
import static com.googlecode.javacv.cpp.opencv_highgui.*;
public class Final
{
private VideoPanel videoPanel = new VideoPanel();
private ImagePanel imagePanel = new ImagePanel();
private JButton jbtCapture = new JButton("Captura");
private JRadioButton jbtAutoCap = new JRadioButton("Captura Automatica");
private FrameGrabber vision;
private BufferedImage image;
private IplImage gimage;
public class VideoPanel extends JPanel
{
public VideoPanel()
{
vision = new OpenCVFrameGrabber(0);
try
{
vision.start();
} catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
try {
image = vision.grab().getBufferedImage();
if (image != null)
{
g.drawImage(image, 0, 0, 640, 480, null);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
repaint();
}
}
class ImagePanel extends JPanel
{
private BufferedImage image;
public ImagePanel()
{
try {
image = ImageIO.read(new File("image001.bmp"));
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
if(image != null)
{
g.drawImage(image,5,0,640,480, null);
}
}
}
private void displayGUI()
{
JFrame janela = new JFrame();
JPanel jpButton = new JPanel();
jpButton.setLayout(null);
jbtCapture.setBounds(145,0,110,30);
jpButton.add(jbtCapture);
jbtAutoCap.setBounds(0, 5, 140, 23);
jpButton.add(jbtAutoCap);
janela.setLayout(null);
videoPanel.setBounds(5, 5, 640, 480);
janela.add(videoPanel);
imagePanel.setBounds(705,5,640,480);
janela.add(imagePanel);
jpButton.setBounds(5, 500, 670, 40);
janela.add(jpButton);
janela.setSize(1366,730);
janela.setVisible(true);
jbtCapture.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try {
gimage = vision.grab();
cvSaveImage("image001.bmp", gimage);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
);
}
public static void main(String[] args)
{
// TODO Auto-generated method stub
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
// TODO Auto-generated method stub
new Final().displayGUI();
}
});
}
}