AndroidJavaCV(圖片轉視訊)

凌浩雨發表於2018-01-11
JavaCV
JavaCpp
如果下載不了,可以下載此處

1. 使用

注意:儲存8張源圖的路徑是”/sdcard/ScreenRecord/temp/”, 儲存輸出的視訊檔案路徑為”/sdcard/ScreenRecord/”,事先應現在”/sdcard/ScreenRecord/temp/”放置8張圖片,當然本例中是8張,可以根據自己的需求變化

public class MainActivity extends AppCompatActivity {
    public static final String IMAGE_TYPE = ".png";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void start(View view) {
        imageToMp4();
    }

    /**
     * 圖片轉視訊
     */
    private void imageToMp4() {
        // 生成的新檔名
        String newFileName = "test_" + System.currentTimeMillis() + ".mp4";
        // 儲存的路徑
        String temp = null;
        final double frameRate = 5;
        try {
            temp = new FileUtils().getSDCardRoot() + "ScreenRecord"
                    + File.separator + newFileName;
        } catch (FileUtils.NoSdcardException e1) {
            e1.printStackTrace();
        }
        final String savePath = temp;
        new Thread() {
            @Override
            public void run() {
                Log.d("test", "開始將圖片轉成視訊啦...frameRate=" + frameRate);
                try {
                    // 臨時檔案路徑即儲存源圖片的路徑
                    String tempFilePath = new FileUtils().getSDCardRoot()
                            + "ScreenRecord/temp" + File.separator;
                    Log.i("test", "tempFilePath=" + tempFilePath);
                    Bitmap testBitmap = BitmapFactory.decodeFile(tempFilePath
                            + "head1" + MainActivity.IMAGE_TYPE);
                     //建立一個記錄者
                    FFmpegFrameRecorder recorder = new FFmpegFrameRecorder(
                            savePath, testBitmap.getWidth(),
                            testBitmap.getHeight());
                    // 設定視訊格式
                    recorder.setFormat("mp4");
                    // 錄影幀率
                    recorder.setFrameRate(frameRate);
                    // 記錄開始
                    recorder.start();

                    int index = 0;
                    while (index < 8) {
                         // 獲取圖片--圖片格式為head1.png,head2.png...head8.png
                        opencv_core.IplImage image = cvLoadImage(tempFilePath
                                + "head" + index
                                + MainActivity.IMAGE_TYPE);
                        recorder.record(image);
                        index++;
                    }
                    Log.d("test", "錄製完成....");
                    // 錄製結束
                    recorder.stop();
                } catch (FileUtils.NoSdcardException | FrameRecorder.Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }
}

FileUtils.java


public class FileUtils {
    private String SDCardRoot;
    private static boolean isCardExist;
    
    public FileUtils() throws NoSdcardException {
        getSDCardRoot();
    }
    
    public String getSDCardRoot() throws NoSdcardException{
        if(isCardExist()){
            SDCardRoot = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator;
        }else{
            throw new NoSdcardException();
        }
        return SDCardRoot;
    }
    
    public static boolean isCardExist(){
        isCardExist = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)?true:false;
        return isCardExist; 
    }
    public File createFileInSDCard(String fileName, String dir)
            throws IOException {
        File file = new File(SDCardRoot + dir + File.separator + fileName);
        if(!file.exists()){ 
            file.getParentFile().mkdirs();
            file.createNewFile();
        }
        return file;
    }
    public File creatSDDir(String dir) {
        File dirFile = new File(SDCardRoot + dir + File.separator);
        if(!dirFile.exists()){
            dirFile.mkdirs();
        }

        return dirFile;
    }
    public boolean filterFileExist(String path, String filter) {
        File file = new File(SDCardRoot + path + File.separator);
        if (file.exists() && file.isDirectory()) {

            String[] fileNames = file.list(new FilenameFilter() {
                public boolean accept(File dir, String filename) {
                    return filename.endsWith(".png");
                }
            });
            if (fileNames.length > 0) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }
    
    /**
     * 
     */
    public boolean isFileExist(String fileName, String path) {
        File file = new File(SDCardRoot + path + File.separator + fileName);
        return file.exists();
    }
    public File getFile(String fileName,String path){
        File file = new File(SDCardRoot + path + File.separator + fileName);
        return file;
    }
    public void deleteFile(String fileName, String path) {
        File file = new File(SDCardRoot + path + File.separator + fileName);
        boolean result = file.delete();
    }
    
    public void closeInputStream(InputStream inputStream){
        if(inputStream!=null){
            try {
                inputStream.close();
            } catch (IOException e) {
                Log.e("error", "close failed");
                e.printStackTrace();
            }
        }
    }
    public class NoSdcardException extends Exception{
        
    }
    
    
    /**
     * 刪除資料夾中的內容,不刪除資料夾本身
     * @param path
     */
    public static void deleteDirectoryContent(String path){
        Log.w("test","deleteDirectory.."+path);
        File file=new File(path);
        if(!file.exists()){
            return;
        }
        String fPath=file.getAbsolutePath();
        if(file.isDirectory()){
            String[] files=getDirectoryFiles(path);
            if(files==null){
                deleteFile(path);
                return;
            }
            for(String str:files){
                str=fPath+"/"+str;
                file=new File(str);
                if(file.isDirectory()){
                    deleteDirectory(str);
                }else if(file.isFile()){
                    deleteFile(str);
                }
            }
//          deleteFile(path);
        }else if(file.isFile()){
            deleteFile(path);
        }
    }
    
    /**
     * 刪除資料夾中的內容
     * @param path
     */
    public static void deleteDirectory(String path){
        Log.w("test","deleteDirectory.."+path);
        File file=new File(path);
        if(!file.exists()){
            return;
        }
        String fPath=file.getAbsolutePath();
        if(file.isDirectory()){
            String[] files=getDirectoryFiles(path);
            if(files==null){
                deleteFile(path);
                return;
            }
            for(String str:files){
                str=fPath+"/"+str;
                file=new File(str);
                if(file.isDirectory()){
                    deleteDirectory(str);
                }else if(file.isFile()){
                    deleteFile(str);
                }
            }
            deleteFile(path);
        }else if(file.isFile()){
            deleteFile(path);
        }
    }
    
    /**
     * 刪除指定路徑的檔案
     * @param filePath
     *        檔案路徑
     */
    public static void deleteFile(String filePath){
        Log.w("test","deleteFile:filePath="+filePath);
        if(filePath==null){
            return;
        }
        File file=new File(filePath);
        if(file.exists()){
            file.delete();
        }
    }
    
    
    /**
     * 獲取資料夾下面的所有檔案
     * @param path
     * @return
     */
    public static String[] getDirectoryFiles(String path){
        if(path==null){
            return null;
        }
        File file=new File(path);
        if(!file.exists()){
            return null;
        }
        String[] files=file.list();
        if(files==null || files.length<=0){
            return null;
        }
        return files;
    }
}


相關文章