2012-05-11 23 views
2

言語:ジャワ
それを追加して使用される:JMF以下 保存画像

は、ウェブカメラで画像をキャプチャすることができ(スイングフォームなし)コードである。

MediaLocator getWebCam = new MediaLocator("vfw://0"); 
    private Player player; 
    Timer timer = new Timer(40, this); 

    public BufferedImage grabFrameImage() { 

     Image image = null; 
     FrameGrabbingControl frameGrabbingControl = null; 

     if (player != null) 
      frameGrabbingControl = (FrameGrabbingControl) player.getControl("javax.media.control.FrameGrabbingControl"); 
     Buffer buffer = frameGrabbingControl.grabFrame(); 
     if (buffer != null) 
      image = new BufferToImage((VideoFormat) buffer.getFormat()).createImage(buffer); 
     if (image != null) 
      return (BufferedImage) image; 

     return null; 
    } 

    public WorkWithWebCam() throws NoDataSourceException, IOException, NoPlayerException { 

      initComponents(); 
      player = Manager.createPlayer(Manager.createDataSource(getWebCam)); 
      player.start(); 

    } 
private void jButton1ActionPerformed(ActionEvent e) { 
     timer.start(); 
    } 

    public static void main(String args[]) { 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       try{ 
        new WorkWithWebCam().setVisible(true); 
       }catch(Exception ex){} 
      } 
     }); 
    } 

    public void actionPerformed(ActionEvent e) { 
     panelMain.getGraphics().drawImage(this.grabFrameImage(), 0, 0, 400, 300, null); 
    } 

ウェブカメラで画像を保存する方法を教えてください。我々はこの方法がactionPerformedの拡張今後

は、:

public void actionPerformed(ActionEvent e) { 
     panelMain.getGraphics().drawImage(this.grabFrameImage(), 0, 0, 400, 300, null); 
     BufferedImage image = new BufferedImage(400, 300, BufferedImage.TYPE_INT_RGB); 
     String fileOut = "temp.jpg"; 
     Graphics g = image.getGraphics(); 
     panelMain.paint(g); 
     try { 
      ImageIO.write(image, "jpg", new FileOutputStream(fileOut)); 
     } catch (IOException e1) { 
      e1.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. 
     } 

    } 

だけの事を、何の絵は、Webカメラ、および右側の縦ストライプと白の背景に保存されません。

答えて

2
image = new BufferToImage((VideoFormat) buffer.getFormat()).createImage(buffer); 
ImageIO.write(image, "png", new File("screengrab.png")); 

詳細については、ImageIO.write(RenderedImage,String,File)を参照してください。

+0

作品)ありがとうございます) – TorchTT