我创建了这个简单的程序,它使用JMF捕获网络摄像头图像,并将其保存到带有参数的本地硬盘驱动器。使用ecplipse中的com.sun.image.codec.jpeg.*
时效果非常好。但JDK7不再支持这一点。无法从命令行编译。相反,我必须使用javax.imageio
。但我被困在这里了。无法完成代码。我用javax.imageio
替换了com.sun.image.codec.jpeg
。
ImageIO.write(buffImg, "png", new File("c:\byder_"+imagebydernr+".png"));
当从ecplipse运行它时,它会给出这个错误
"java.lang.NoSuchMethodError: main
Exception in thread "main"
不知道该怎么办。我需要为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);
}
}
}
尝试。。
publicstaticvoid main(String[]args)
试试这个:
public static void main(String[] args)
和
public static void playerclose()
那么它应该起作用。