學習javacv之八:拖動進度條,控制播放視頻

package com.csdn.linghu.javacvlean04;

import java.nio.IntBuffer;

import org.bytedeco.javacpp.opencv_core.IplImage;
import org.bytedeco.javacpp.opencv_highgui.CvCapture;
import org.bytedeco.javacpp.opencv_highgui.CvTrackbarCallback;

import static org.bytedeco.javacpp.opencv_highgui.*;

public class Trackbar {
public static void main(String[] args) {
	//進度條最大值
	int switchValue =30;
	//每幀要顯示的圖像
	IplImage frammeImage = null;
	//回調方法載體
	SwitchCallBack switchCallBack =  new SwitchCallBack();
	//讀取視頻
	CvCapture cvCapture = cvCreateFileCapture("resources/when.avi");
	if (cvCapture==null) {
		System.out.println("讀取文件出錯!");
		return;
	}
	//創建窗口,大小可調整
	cvNamedWindow("Trackbar", 0);
	cvCreateTrackbar(
			//進度條的名稱
		    "Progress",
		    //窗口的名稱
		    "Trackbar",
		    //當前進度條的值
		    switchCallBack.getIntBuffer(),
		    //進度條最大值
		    switchValue,
		    //回調函數載體
		    switchCallBack
		  );
	while(true) {
		//只要進度條,沒有拖到最後,就進行播放
	    if(switchCallBack.getIntBuffer().get(0)!=30&&switchCallBack.getIntBuffer().get(0)!=0){
	    	frammeImage = cvQueryFrame(cvCapture);
	   	 	if(frammeImage==null) break;
	    }
	    //展示當前幀,以實現視頻的播放
	    cvShowImage( "Trackbar", frammeImage);
	    if(cvWaitKey(10)==27 ){
	    	break;
	    } 
	  }
	//釋放資源
	cvReleaseCapture(cvCapture);
	cvDestroyWindow("Trackbar");
}

}
/**
 * @功能說明:實現拖動進度條的過程中的函數回調
 * @time:2014年7月19日下午6:05:28
 * @version:1.0
 *
 */
class SwitchCallBack extends CvTrackbarCallback{
	private IntBuffer intBuffer = IntBuffer.allocate(30);
	@Override
	public void call(int position){
		//進度條當前位置在0,提示關閉
		if (position ==0) {
			switchBegin();
			intBuffer.clear();
			intBuffer.put(position);
		//進度條當前位置是30,播放結束
		}else if(position==30){
			switchEnd();
			intBuffer.clear();
			intBuffer.put(position);
		//清空進度條緩存值,放入當前值
		}else{
			intBuffer.clear();
			intBuffer.put(position);
			switchOn();
		}
		
	}
	public void switchOn(){
		System.out.println("正在播放:");
	}
	public void switchBegin(){
		System.out.println("即將開始播放!");
	}
	public void switchEnd(){
		System.out.println("播放完畢");
	}
	public IntBuffer getIntBuffer() {
		return intBuffer;
	}
	public void setIntBuffer(IntBuffer intBuffer) {
		this.intBuffer = intBuffer;
	}
	
}

實現效果:





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章