这是我使用JMF(java媒体框架)从视频中获取图像的代码:
public class ImageCaptureFromVideo {
private static final String outputFilePrefix = "C:/Pics/";
public static void main(String ar[]) {
try
{
URL url=new File("C:/Pics/daddy_baby.mp4").toURI().oURL();
MediaLocator mediaLocator = new MediaLocator(url);
//Player p =Manager.createRealizedPlayer(newURL("file:///C:/Pics/daddy_baby.mp4"));
Player p = Manager.createRealizedPlayer(mediaLocator);
FramePositioningControl fpc = (FramePositioningControl)
p.getControl("javax.media.control.FramePositioningControl");
FrameGrabbingControl fg =(FrameGrabbingControl)p.getControl("javax.media.control.FrameGrabbingControl");
p.prefetch();
fpc.seek(100);
Buffer buf = fg.grabFrame();
VideoFormat vf = (VideoFormat) buf.getFormat();
BufferToImage bufferToImage = new BufferToImage(vf);
Image im = bufferToImage.createImage(buf);
BufferedImage formatImg = new BufferedImage(360, 360, BufferedImage.TYPE_3BYTE_BGR);
dumpImageToFile(formatImg);
Graphics g = formatImg.getGraphics();
g.drawImage(im, 0, 0, 360, 360, null);
g.dispose();
}
catch (Exception e) {
System.out.println("exception i " + e);
}
}
private static String dumpImageToFile(BufferedImage image) {
try
{
String outputFilename = outputFilePrefix + System.currentTimeMillis() + ".png";
ImageIO.write(image, "png", new File(outputFilename));
return outputFilename;
}
catch (IOException e)
{
e.printStackTrace();
return null;
}
}
}
但它显示:
javax.media.NoPlayerException:找不到以下播放器:file:/ C:/Pics/daddy_baby.mp4
在Player p = Manager.createRealizedPlayer(mediaLocator);