⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 seek.java

📁 SNP算法: 基于背景缩减和阴影检测的统计方法,适用于室内检测.
💻 JAVA
字号:
package bin;

//To seek to individual frames inside a movie(定位到影片中的帧)

import java.awt.*;
import java.awt.event.*;
import javax.media.*;
import java.io.*;
import javax.media.control.FrameGrabbingControl;
import javax.media.control.FramePositioningControl;
import javax.media.protocol.*;
import javax.media.protocol.DataSource;
import javax.media.format.VideoFormat;
import javax.media.util.BufferToImage;
import java.awt.image.BufferedImage; 
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import java.awt.Graphics;
 
public class Seek implements ControllerListener{
   
    //播放器接口
    Player p;
    
    //帧定位控制接口
    FramePositioningControl fpc;
    
    //获取帧控制接口
    FrameGrabbingControl fgc ;
    
    //convert a video Buffer object to an AWT Image object
    BufferToImage bti;
    
    Object waitSync = new Object();
    
    boolean stateTransitionOK = true;
    
    //影片总帧数
    int totalFrames = 0;
    
    //the name of bmp file saved
    int bmpName=1;
    
    int time=0;
    
    //影片格式
    VideoFormat vf = null;
    
    //利用管理器创建一个数据源
	Seek(String args) {
	
		//the location of media content
		MediaLocator ml;
	    
	    //找不到影片
		if ((ml = new MediaLocator(args)) == null) {
		    System.err.println("Cannot build media locator from: " + args);
		    System.err.println("file path error!");
		    System.exit(0);
		}
	    
	    //数据源
		DataSource ds = null;
	
		// Create a DataSource use given the media locator.
		try {
		    ds = Manager.createDataSource(ml);
		    totalFrames = FramePositioningControl.FRAME_UNKNOWN;
		} catch (Exception e) {
		    System.err.println("1Cannot create DataSource from: " + ml);
		}
		if (!open(ds))
		    System.err.println("2Cannot create DataSource from: " + ml);
		
	}
	
    /**
     * Given a DataSource, create a player and use that player
     * as a player to playback the media.
     */
     
     //创建播放器并且播放
    public boolean open(DataSource ds) {

		System.err.println("create player for: " + ds.getContentType());
	
	    //创建播放器
		try {
		    p = Manager.createPlayer(ds);
		} catch (Exception e) {
		    System.err.println("Failed to create a player from the given DataSource: " + e);
		    return false;
		}
	
		p.addControllerListener(this);
	    
	    //实例化播放器
		p.realize();
		if (!waitForState(p.Realized)) {
		    System.err.println("Failed to realize the player.");
		    return false;
		}
	   
		// Try to retrieve a FramePositioningControl from the player.
		fpc = (FramePositioningControl)p.getControl("javax.media.control.FramePositioningControl");
		fgc = (FrameGrabbingControl)p.getControl("javax.media.control.FrameGrabbingControl");
		
	
		if (fpc == null) {
		    System.err.println("The player does not support FramePositioningControl.");
		    System.err.println("There's no reason to go on for the purpose of this demo.");
		    return false;
		}
	
		Time duration = p.getDuration();
	
		if (duration != Duration.DURATION_UNKNOWN) {
		    System.err.println("Movie duration: " + duration.getSeconds());
			time=(int)duration.getSeconds();
		    totalFrames = fpc.mapTimeToFrame(duration);
		    if (totalFrames != FramePositioningControl.FRAME_UNKNOWN)
			System.err.println("Total # of video frames in the movies: " + totalFrames);
		    else
			System.err.println("The FramePositiongControl does not support mapTimeToFrame.");
	
		} else {
		    System.err.println("Movie duration: unknown"); 
		}
		
		// Prefetch the player.播放器预取资源
		p.prefetch();
		if (!waitForState(p.Prefetched)) {
		    System.err.println("Failed to prefetch the player.");
		    return false;
		}
	
		// Display the visual & control component if there's one.
	
		return true;
    }

    /**
     * Block until the player has transitioned to the given state.
     * Return false if the transition failed.
     */
    boolean waitForState(int state) {
		synchronized (waitSync) {
		    try {
			while (p.getState() < state && stateTransitionOK)
			    waitSync.wait();
		    } catch (Exception e) {}
		}
		return stateTransitionOK;
    }

	public void controllerUpdate(ControllerEvent evt) {

		if (evt instanceof ConfigureCompleteEvent ||
		    evt instanceof RealizeCompleteEvent ||
		    evt instanceof PrefetchCompleteEvent) {
		    synchronized (waitSync) {
			stateTransitionOK = true;
			waitSync.notifyAll();
		    }
		} else if (evt instanceof ResourceUnavailableEvent) {
		    synchronized (waitSync) {
			stateTransitionOK = false;
			waitSync.notifyAll();
		    }
		} else if (evt instanceof EndOfMediaEvent) {
		    p.setMediaTime(new Time(0));
		    //p.start();
		    //p.close();
		    //System.exit(0);
		} else if (evt instanceof SizeChangeEvent) {
		}
    }
    
    public int getTotal(){
    	return totalFrames;
    }
    public int getTime(){
    	return time;
    }
    public void initVF(){
		if (totalFrames == FramePositioningControl.FRAME_UNKNOWN)
			System.err.println("Cannot jump to a random frame.");
		else {
			int randomFrame = fpc.seek(1);
			//System.err.println("Jump to a random frame: " + randomFrame);
		}
	
		int currentFrame = fpc.mapTimeToFrame(p.getMediaTime());
		if (currentFrame != FramePositioningControl.FRAME_UNKNOWN ){
		  		Buffer buf = fgc.grabFrame(); //截取媒体当前播放的帧
			    vf = (VideoFormat) buf.getFormat(); 
		 }   
    }
    public Image capture(int a) {	//获取当前帧	
        Image im=null;
		if (totalFrames == FramePositioningControl.FRAME_UNKNOWN)
			System.err.println("Cannot jump to a random frame.");
		else {
			int randomFrame = fpc.seek(a);
			//System.err.println("Jump to a random frame: " + randomFrame);
		}
	
		int currentFrame = fpc.mapTimeToFrame(p.getMediaTime());
		if (currentFrame != FramePositioningControl.FRAME_UNKNOWN ){
		    //System.err.println("Current frame: " + currentFrame);
		  	
		  		Buffer buf = fgc.grabFrame(); //截取媒体当前播放的帧
			    //VideoFormat vf = (VideoFormat) buf.getFormat(); 
			    BufferToImage bti =new BufferToImage(vf);
			    //int wideth = vf.getSize().width;
			    //int height = vf.getSize().height; 
			    im = bti.createImage(buf); 
		 }   
	     return im;
	
     }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -