2012-03-01 4 views
0

JMF를 사용하여 웹캠 이미지를 캡처하고 인수를 사용하여 로컬 하드 드라이브에 저장하는 간단한 프로그램을 만들었습니다. ecplipse에서 com.sun.image.codec.jpeg.*을 사용할 때 훌륭하게 작동합니다. 그러나 JDK7은 더 이상 이것을 지원하지 않습니다. 커맨드 라인에서 컴파일 할 수 없습니다. 대신 javax.imageio을 사용해야합니다. 하지만 난 여기 붙어있어. 코드를 마칠 수 없습니다. 나는 com.sun.image.codec.jpegjavax.imageio으로 바꿨다.javax.imageio를 사용하는 JMF 문제

ImageIO.write(buffImg, "png", new File("c:\\byder_"+imagebydernr+".png")); 

가 무엇을 해야할지하지 않습니다이 오류

"java.lang.NoSuchMethodError: main 
Exception in thread "main" 

에게 제공 ecplipse에서 실행하는 경우. imageIO 클래스를 만들어야합니까?

package SwingCapture; 


import javax.swing.*; 
import java.io.*; 
import java.util.Date; 
import javax.media.*; 
import javax.media.format.*; 
import javax.media.util.*; 
import javax.media.control.*; 
import java.awt.*; 
import java.awt.image.*; 
import java.awt.event.*; 
import javax.imageio.*; 



public class SwingCapture extends Panel implements ActionListener 
{ 
/** 
* 
*/ 
private static final long serialVersionUID = 1L; 
public static Player player = null; 
public CaptureDeviceInfo di = null; 
public MediaLocator ml = null; 
public JButton capture_take = null; 
public JButton capture_accept = null; 
public static Buffer buf = null; 
public Image img = null; 
public VideoFormat vf = null; 
public BufferToImage btoi = null; 
public ImagePanel imgpanel = null; 
public BufferedImage buffImg = null; 
public static String imagebydernr = null; 



public SwingCapture() 
{ 
setLayout(new BorderLayout()); 
setSize(320,550); 

imgpanel = new ImagePanel(); 
capture_take = new JButton("Take picture"); 
capture_accept = new JButton("Accept picture"); 
capture_take.addActionListener(this); 
capture_accept.addActionListener(this); 

String str2 = "vfw:Microsoft WDM Image Capture (Win32):0"; 
di = CaptureDeviceManager.getDevice(str2); 
ml = di.getLocator(); 


try 
{ 
    player = Manager.createRealizedPlayer(ml); 
    player.start(); 
    Component comp; 

    if ((comp = player.getVisualComponent()) != null) 
    { 
    add(comp,BorderLayout.NORTH); 
    } 
    add(capture_take,BorderLayout.CENTER); 
    add(capture_accept,BorderLayout.EAST); 
    add(imgpanel,BorderLayout.SOUTH); 
} 
catch (Exception e) 
{ 
    e.printStackTrace(); 
} 
} 



public void main(String[] args) 
{ 
Frame f = new Frame("C5snap"); 
SwingCapture cf = new SwingCapture(); 

imagebydernr = args[0]; 


f.addWindowListener(new WindowAdapter() { 
    public void windowClosing(WindowEvent e) { 
    playerclose(); 
    System.exit(0);}}); 

f.add("Center",cf); 
f.pack(); 
f.setSize(new Dimension(320,550)); 
f.setVisible(true); 
} 


public void playerclose() 
{ 
player.close(); 
player.deallocate(); 
} 


public void actionPerformed(ActionEvent e) 
{ 
JComponent c = (JComponent) e.getSource(); 

if (c == capture_take) 
{ 


    // Grab a frame from the capture device 
    FrameGrabbingControl frameGrabber =     (FrameGrabbingControl)player.getControl("javax.media.control.FrameGrabbingControl"); 
    Buffer buf = frameGrabber.grabFrame(); 


    // Convert frame to an buffered image so it can be processed and saved 
    Image img = (new BufferToImage((VideoFormat)buf.getFormat()).createImage(buf)); 
    BufferedImage buffImg = new BufferedImage(img.getWidth(null), img.getHeight(null),  BufferedImage.TYPE_INT_RGB); 
    Graphics2D g = buffImg.createGraphics(); 
    g.drawImage(img, null, null); 

    // Overlay current time on image 
    g.setColor(Color.RED); 
    g.setFont(new Font("Verdana", Font.BOLD, 16)); 
    g.drawString((new Date()).toString(), 10, 25); 


// Show picture 
    imgpanel.setImage(img); 



} else if (c == capture_accept) { 

    File f = new File("c:\\byder_"+imagebydernr+".png"); 


    if(f.exists()){ 
      System.out.println("File existed"); 


      /* Warning box, if file exist.*/ 
      Object[] options = { "close" }; 
      int choice = JOptionPane.showOptionDialog(null, 
       "File exist. Close and call new picture", 
       "Advarsel", 
       JOptionPane.YES_NO_OPTION, 
       JOptionPane.QUESTION_MESSAGE, 
       null, 
       options, 
       options[0]); 

      // Closing applet 
     if (choice == JOptionPane.YES_OPTION) 
     { 
     System.exit(0); 
     } 

    }else{ 
      System.out.println("File not found!"); 

      // Save image to disk as PNG 
      ImageIO.write(buffImg, "png", new File("c:\\byder_"+imagebydernr+".png")); 

    } 
    // Close webcam 
    player.close(); 
    player.deallocate(); 
    System.exit(0); 
    }  
} 


class ImagePanel extends Panel 
{ 
    /** 
* 
*/ 
private static final long serialVersionUID = 1L; 
public Image myimg = null; 

    public ImagePanel() 
{ 
setLayout(null); 
setSize(320,240); 
} 

public void setImage(Image img) 
{ 
this.myimg = img; 
repaint(); 
} 

    public void paint(Graphics g) 
{ 
if (myimg != null) 
{ 
    g.drawImage(myimg, 0, 0, this); 
} 
} 
} 
+0

스택 오버플로에 오신 것을 환영합니다. [How to Ask] (http://stackoverflow.com/questions/how-to-ask), [무엇을 시도 했습니까?] (http://mattgemmell.com/2008/12/08/what-have -you-tried /), [질문하는 방법 (Smart Way)] (http://catb.org/esr/faqs/smart-questions.html). –

+1

'JPanel'에서'paint()'를 오버라이드하지 말고 대신'paintComponent()'를 사용하십시오. –

답변

1

해보 ..

공개 정적 무효 메인 (문자열 []에 args)

0

이 시도 :

public static void main(String[] args) 

public static void playerclose() 

... 그러면 효과가 있습니다.

+0

사용하기 쉽고 멋지다 Stackoverflow에 의해 주어진 서식 도구를 사용하십시오. 또한 미리보기를 사용하여 대답이 어떻게 표시되는지 확인할 수 있습니다. 여기에 명시된 바와 같이 귀하의 답변은 거의 읽을 수 없으며 도움이되지 않을 수 있습니다. – JMax