Android JavaCV(图片转视频)-阿里云开发者社区

开发者社区> 开发与运维> 正文
登录阅读全文

Android JavaCV(图片转视频)

简介: JavaCV JavaCpp 如果下载不了,可以下载此处 1. 使用 注意:存储8张源图的路径是"/sdcard/ScreenRecord/temp/", 存储输出的视频文件路径为"/sdcard/ScreenRecord/",事先应现在"/sdca...
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;
    }
}

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

分享:
开发与运维
使用钉钉扫一扫加入圈子
+ 订阅

集结各类场景实战经验,助你开发运维畅行无忧

其他文章