我已經創建了這個簡單的程序,它使用JMF捕捉攝像頭圖像並將其保存到具有參數的本地硬盤上。它使用ecplipse中的com.sun.image.codec.jpeg.*
時效果很好。但JDK7不再支持這一點。不能從命令行編譯。相反,我必須使用javax.imageio
。但我在這裏卡住了。無法完成代碼。我用javax.imageio
替換了com.sun.image.codec.jpeg
。JMF使用javax.imageio問題
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);
}
}
}
歡迎來到Stack Overflow。請閱讀[如何問](http://stackoverflow.com/questions/how-to-ask),[你有什麼嘗試?](http://mattgemmell.com/2008/12/08/what-have - 你試過/),和[如何問問題的智能方式](http://catb.org/esr/faqs/smart-questions.html)。 – 2012-03-01 12:42:29
不要在'JPanel'中重寫'paint()' - 而是使用'paintComponent()'。 – 2012-03-01 12:58:12