2012-05-11 2 views
2

언어 : 자바
그것은 또한 사용된다 : JMF 아래 이미지 저장

은 WEB-카메라로 이미지를 캡처 할 수 있습니다 (스윙 형태가없는) 코드입니다 :

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); 
    } 

웹 카메라로 이미지를 저장하는 방법을 알려주시겠습니까?

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. 
     } 

    } 

있는 유일한 방법은 어떤 사진을 웹 카메라로 저장되지 않고, 오른쪽에 세로 줄무늬와 흰색 배경 : 앞서 찾고

, 우리는 방법의 actionPerformed 확장.

답변

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

자세한 내용은 ImageIO.write(RenderedImage,String,File)을 참조하십시오.

+0

작동) 감사합니다.) – TorchTT