package com.mazaiting;
import java.io.File;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import org.jim2mov.core.DefaultMovieInfoProvider;
import org.jim2mov.core.FrameSavedListener;
import org.jim2mov.core.ImageProvider;
import org.jim2mov.core.Jim2Mov;
import org.jim2mov.core.MovieInfoProvider;
import org.jim2mov.core.MovieSaveException;
import org.jim2mov.utils.MovieUtils;
public class Water implements ImageProvider, FrameSavedListener{
// 文件数组
private ArrayList<String> fileArray = null;
// 文件类型
private int type = MovieInfoProvider.TYPE_QUICKTIME_JPEG;
// 每张图片显示多少秒 可以改!!!!!
private static int NUM = 2;
// 生成的图片地址 每个线程的名字都不能一样!!! 生成的视频在
private static String path = "G:\\image";
/**
* 读取总文件夹下所有文件夹中的图片 生成avi视频
*/
public static void main(String[] args) throws MovieSaveException {
// 文件夹地址 可自行更改
String path1 = path;
LinkedHashMap<String, String> map = getFiles(path1);
getAVI(map);
}
/**
* 读取某个目录下所有文件、文件夹
* @param path
* @return LinkedHashMap<String,String>
*/
public static LinkedHashMap<String,String> getFiles(String path) {
LinkedHashMap<String,String> files = new LinkedHashMap<String,String>();
File file = new File(path);
File[] tempList = file.listFiles();
for (int i = 0; i < tempList.length; i++) {
if (tempList[i].isDirectory()) {
files.put(tempList[i].getName(),tempList[i].getPath());
}
}
return files;
}
/**
* 生成avi文件
*/
public static void getAVI(LinkedHashMap<String,String> map) throws MovieSaveException{
for (String s : map.keySet()) {
System.out.println("文件名 : " + s + " 地址 : " + map.get(s));
ArrayList<String> fileArray = new ArrayList<>();
File[] listFiles = new File(map.get(s)).listFiles();
for (int i = 0; i < listFiles.length; i++) {
for (int j = 0; j < NUM*10; j++) {
fileArray.add(listFiles[i].getAbsolutePath());
}
}
String temp = path.substring(3, path.length());
String outPath = "..//"+temp+"//";
new Water(fileArray, MovieInfoProvider.TYPE_QUICKTIME_JPEG, outPath + s+".avi");
}
}
/**
* 图片转视
* @param filePaths 文件路径数组
* @param type 格式
* @param path 文件路径
* @throws MovieSaveException
*/
public Water(ArrayList<String> fileArray, int type, String path) throws MovieSaveException {
this.fileArray = fileArray;
this.type = type;
DefaultMovieInfoProvider dmip = new DefaultMovieInfoProvider(path);
// 设置帧频
dmip.setFPS(10);
// 设置帧数
dmip.setNumberOfFrames(fileArray.size());
// 设置视频高度
dmip.setMWidth(200);
// 设置视频宽度
dmip.setMHeight(200);
new Jim2Mov(this, dmip, this).saveMovie(this.type);;
}
@Override
public void frameSaved(int frameNumber) {
frameNumber = frameNumber+1;
System.out.println("正在保存第: " + frameNumber+"张");
}
@Override
public byte[] getImage(int frame) {
try {
return MovieUtils.convertImageToJPEG(new File(fileArray.get(frame)), 1.0f);
} catch (Exception e) {
System.out.println("可能文件目录错误,请检查输入path的图片目录!");
e.printStackTrace();
}
return null;
}
}
用了两个jar Jim2mov jmf