Java:JMF 从player截图

来源:互联网 发布:淘宝店官网 编辑:程序博客网 时间:2024/06/16 23:20
package implementation;import java.awt.Graphics2D;import java.awt.Image;import java.awt.image.BufferedImage;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import javax.media.Buffer;import javax.media.Player;import javax.media.control.FrameGrabbingControl;import javax.media.format.VideoFormat;import javax.media.util.BufferToImage;import com.sun.image.codec.jpeg.ImageFormatException;import com.sun.image.codec.jpeg.JPEGCodec;import com.sun.image.codec.jpeg.JPEGEncodeParam;import com.sun.image.codec.jpeg.JPEGImageEncoder;public class PhotoCatch {private Buffer buffer;private BufferToImage bufferToImage;private Image image;private FrameGrabbingControl fgc;public PhotoCatch(Player player){fgc = (FrameGrabbingControl) player.getControl("javax.media.control.FrameGrabbingControl");this.buffer = fgc.grabFrame();bufferToImage = new BufferToImage((VideoFormat) buffer.getFormat());image = bufferToImage.createImage(this.buffer);}public void upDate(){this.buffer = fgc.grabFrame();bufferToImage = new BufferToImage((VideoFormat) buffer.getFormat());image = bufferToImage.createImage(this.buffer);}public int getImageWidth(){return image.getWidth(null);}public int getImageHeight(){return image.getHeight(null);}public void saveImage(String address){saveImage(image,address);}private void saveImage(Image image, String path) {if(this.image == null)System.out.println("image is null");BufferedImage bi = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB);Graphics2D g2 = bi.createGraphics();g2.drawImage(image, null, null);FileOutputStream fos = null;try {fos = new FileOutputStream(path);} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}JPEGImageEncoder je = JPEGCodec.createJPEGEncoder(fos);JPEGEncodeParam jp = je.getDefaultJPEGEncodeParam(bi);jp.setQuality(0.5f, false);je.setJPEGEncodeParam(jp);try {je.encode(bi);fos.close();} catch (ImageFormatException e) {// TODO Auto-generated catch blockSystem.out.println("错误4");} catch (IOException e) {// TODO Auto-generated catch block//e.printStackTrace();System.out.println("错误5");}}}

每次需要重新截图时候,一定要进行更新!因为JMF API 中

Class BufferToImage

This is a utility class to convert a video Buffer object to an AWTImage object that you can then render using AWT methods.

You can use this class in conjunction with the FrameGrabbingControl to grab frames from the video renderer and manipulate the image. 


bufferToImage是共用的,如果不进行更新的话。buffer会失效,导致无法转换为image,也就无法成功截图了!

原创粉丝点击