使用 Netbeans,我正在尝试连接我的笔记本电脑摄像头或另一个 USB 摄像头并截屏。我的项目中有两个课程可以做到这一点:
functiiwebcam.java(它包含连接设备和捕获图像的所有功能)
这是代码:
import java.util.logging.Level; import java.util.logging.Logger; import javax.media.*; import javax.media.cdm.CaptureDeviceManager; import java.io.*; import java.awt.*; import javax.media.control.FrameGrabbingControl; import javax.media.format.VideoFormat; import javax.media.util.BufferToImage; import javax.swing.JLabel; import javax.swing.JOptionPane; public class functiiwebcam { //pentru fereastra, implicit dispozitiv String camera = "vfw:Microsoft WDM Image Capture (Win32):0"; Player broadcast = null; public Component Componente() { Component componente_video; try { // Selectare dispozitiv CaptureDeviceInfo device = CaptureDeviceManager.getDevice(camera); //Sursa imaginilor capturate MediaLocator localizador = device.getLocator(); //Locatia este de tipul "vfw://0" video para windows //Fereastra pentru afisarea imaginilor in aplicatie broadcast = Manager.createRealizedPlayer(localizador); //broadcast.start(); } catch (IOException ex) { // Logger.getLogger(functiiwebcam.class.getName()).log(Level.SEVERE, null, ex); ex.printStackTrace(); } catch (NoPlayerException ex) { Logger.getLogger(functiiwebcam.class.getName()).log(Level.SEVERE, null, ex); } catch (CannotRealizeException ex) { Logger.getLogger(functiiwebcam.class.getName()).log(Level.SEVERE, null, ex); } // Obtinerea imaginilor in player if ((componente_video = broadcast.getVisualComponent()) != null) { //dimensiunea ferestrei de broadcast componente_video.setSize(320, 240); return componente_video; } else { return null; } } // Metoda pentru captura imaginii Image img = null; public void capturareImagine() { FrameGrabbingControl ControlFG = (FrameGrabbingControl) broadcast.getControl("javax.media.control.FrameGrabbingControl"); Buffer buffer = ControlFG.grabFrame(); // creare image awt BufferToImage image = new BufferToImage((VideoFormat)buffer.getFormat()); img = image.createImage(buffer); } public Image getImage() { return img; } public void setImage(JLabel lb) { capturareImagine(); lb.setIcon( new javax.swing.ImageIcon( img ) ); System.out.println("ancho= " + img.getWidth(null)); System.out.println("alto= " + img.getHeight(null)); } }
Webcam.java(其中包含设计 SWING GUI -JFRAME FORM:按钮和面板等)
在这里,我认为您不需要整个代码。它是我设计界面时由 Netbeans 生成的。我只会放这个类:
import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JOptionPane; public class Webcam extends javax.swing.JFrame { functiiwebcam b = new functiiwebcam(); /** Creates new form Webcam */ public Webcam() { initComponents(); this.setTitle("Webcam"); // Centreaza ferestra this.setLocationRelativeTo(null); // Aplicare Layout de tip BOX panoului cu numele broadcast (cel in care se afiseaza imaginile de pe webcam) broadcast.setLayout(new javax.swing.BoxLayout(broadcast, javax.swing.BoxLayout.LINE_AXIS)); // Adaugarea componentei video broadcast.add(b.Componente()); } // Button for webcam capture private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { b.capturareImagine(); Icon icon = new javax.swing.ImageIcon(b.getImage()); captura.setIcon(icon);
当我运行应用程序时,它会抛出这个异常。但奇怪的是有时它会起作用。我可以从出现的窗口中选择设备,我可以看到来自网络摄像头的图像,甚至可以捕捉它。怎么了?
java.io.IOException: Could not connect to capture device
java.io.IOException: Could not connect to capture device
Iun 05, 2012 1:48:54 PM functiiwebcam Componente
SEVERE: null
javax.media.NoPlayerException: Error instantiating class: com.sun.media.protocol.vfw.DataSource : java.io.IOException: Could not connect to capture device
at javax.media.Manager.createPlayerForContent(Manager.java:1362)
at javax.media.Manager.createPlayer(Manager.java:417)
at javax.media.Manager.createRealizedPlayer(Manager.java:553)
at functiiwebcam.Componente(functiiwebcam.java:37)
at Webcam.<init>(Webcam.java:30)
at MediaPlayer.jMenuItem1ActionPerformed(MediaPlayer.java:476)
at MediaPlayer.access$900(MediaPlayer.java:17)
at MediaPlayer$11.actionPerformed(MediaPlayer.java:325)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.AbstractButton.doClick(AbstractButton.java:376)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:833)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:877)
at java.awt.Component.processMouseEvent(Component.java:6504)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6269)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4860)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4686)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2713)
at java.awt.Component.dispatchEvent(Component.java:4686)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
at java.awt.EventQueue.access$000(EventQueue.java:101)
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:680)
at java.awt.EventQueue$4.run(EventQueue.java:678)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at functiiwebcam.Componente(functiiwebcam.java:48)
at Webcam.<init>(Webcam.java:30)
at MediaPlayer.jMenuItem1ActionPerformed(MediaPlayer.java:476)
at MediaPlayer.access$900(MediaPlayer.java:17)
at MediaPlayer$11.actionPerformed(MediaPlayer.java:325)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.AbstractButton.doClick(AbstractButton.java:376)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:833)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:877)
at java.awt.Component.processMouseEvent(Component.java:6504)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6269)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4860)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4686)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2713)
at java.awt.Component.dispatchEvent(Component.java:4686)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
at java.awt.EventQueue.access$000(EventQueue.java:101)
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:680)
at java.awt.EventQueue$4.run(EventQueue.java:678)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)