我正在尝试在Java中创建一个WebCam
示例,但它不起作用。 JPanel
的重绘方法不会调用paintComponent
。当我在任何地方调用重绘时,它不会更新图像,但程序仍然在运行:
这是我的例子:
public class JFrameExample extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private CameraPanel cameraPanel;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
JFrameExample frame = new JFrameExample();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public JFrameExample() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(300, 200, 900, 600);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
cameraPanel = new CameraPanel();
cameraPanel.setBackground(Color.RED);
cameraPanel.setBounds(10, 52, 640, 480);
contentPane.add(cameraPanel);
JButton btnActivar = new JButton("Activar");
btnActivar.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
startCamera();
}
});
btnActivar.setBounds(10, 11, 89, 23);
contentPane.add(btnActivar);
}
private void startCamera(){
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
VideoCapture eyeCamera = new VideoCapture(0);
if(eyeCamera.isOpened()){
while(true){
Mat frame = new Mat();
eyeCamera.read(frame);
cameraPanel.setimage(matToBufferedImage(frame));
cameraPanel.setSize(new Dimension(frame.width(),frame.height()));
contentPane.repaint();
cameraPanel.repaint();
this.repaint();
}
}
}
public static BufferedImage matToBufferedImage(Mat matrix) {
int cols = matrix.cols();
int rows = matrix.rows();
int elemSize = (int) matrix.elemSize();
byte[] data = new byte[cols * rows * elemSize];
int type;
matrix.get(0, 0, data);
switch (matrix.channels()) {
case 1:
type = BufferedImage.TYPE_BYTE_GRAY;
break;
case 3:
type = BufferedImage.TYPE_3BYTE_BGR;
// bgr to rgb
byte b;
for (int i = 0; i < data.length; i = i + 3) {
b = data[i];
data[i] = data[i + 2];
data[i + 2] = b;
}
break;
default:
return null;
}
BufferedImage image2 = new BufferedImage(cols, rows, type);
image2.getRaster().setDataElements(0, 0, cols, rows, data);
return image2;
}
}
public class CameraPanel extends JPanel{
private static final long serialVersionUID = 1L;
private BufferedImage image;
public CameraPanel() {
super();
}
public BufferedImage getimage() {
return image;
}
public void setimage(BufferedImage newimage) {
image = newimage;
System.out.println("setImage method");
}
@Override
protected void paintComponent(Graphics grafics) {
System.out.println("paintComponent method");
super.paintComponent(grafics);
if(image != null)
grafics.drawImage(image, 10, 10, 50, 50, this);
}
}
答案 0 :(得分:1)
我无法尝试,因为您的应用无法运行。但是,while(true) {}
中的Thread
部分可以解决您的问题。
Thread paintThread = new Thread(new Runnable(){
public void run() {
while(true){
Mat frame = new Mat();
eyeCamera.read(frame);
cameraPanel.setimage(matToBufferedImage(frame));
cameraPanel.setSize(new Dimension(frame.width(),frame.height()));
contentPane.repaint();
cameraPanel.repaint();
this.repaint();
}
}
}
paintThread.start();