android圖片轉視訊
阿新 • • 發佈:2018-12-19
1、許可權
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
2、main
package com.fpt.imagetovideomaster; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import com.googlecode.javacv.FFmpegFrameRecorder; import com.googlecode.javacv.FrameRecorder; import com.googlecode.javacv.cpp.opencv_core; import java.io.File; import java.io.IOException; import static com.googlecode.javacv.cpp.opencv_highgui.cvLoadImage; public class TwoMain extends Activity { public static final String IMAGE_TYPE = ".jpg"; private Button btn; FFmpegFrameRecorder recorder; File file; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn=findViewById(R.id.main_btn_start); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { imageToMp4(); } }); } private void imageToMp4() { // 生成的新檔名 final String newFileName = "/sdcard/1/test_" + System.currentTimeMillis() + ".mp4"; final double frameRate = 1;//1表示1秒1個照片, // new Thread() { @Override public void run() { Log.d("test", "開始將圖片轉成視訊啦...frameRate=" + frameRate); try { file=new File(newFileName); if (!file.exists()){ try { file.createNewFile(); Log.d("main", "建立"); } catch (IOException e) { } } // for (int i=0;i<100;i++) { // file = new File("/sdcard/1/ok" + i + ".mp4");//houq、、後期優化 // if (!file.exists()) { // try { // file.createNewFile(); // Log.d("main", "建立"); // break; // } catch (IOException e) { // // } // } // Log.e("main", "continiu"); // continue;//終結本次迴圈, // } try{ Log.e("main", "luzhi"+file.getPath()); Bitmap bitmap=BitmapFactory.decodeFile("/sdcard/1/1.jpg"); recorder=new FFmpegFrameRecorder(file,bitmap.getWidth(),bitmap.getHeight(),0); //設定視訊的寬高,這裡設定的是以第一張照片為寬高為基準的。 } catch (Exception e){ // Toast.makeText(getApplicationContext(), "recorder", Toast.LENGTH_LONG).show(); } recorder.setFormat("mp4"); // 錄影幀率 recorder.setFrameRate(frameRate); // recorder.setPixelFormat(avutil.AV_PIX_FMT_YUV420P); // 記錄開始 recorder.start(); int index = 0; while (index < 21) { // 獲取圖片--圖片格式為head1.png,head2.png...head8.png opencv_core.IplImage image = cvLoadImage("/sdcard/1/" + index + TwoMain.IMAGE_TYPE); recorder.record(image); index++; } Log.d("test", "錄製完成...."); // 錄製結束 recorder.stop(); } catch (FrameRecorder.Exception e) { e.printStackTrace(); } } }.start(); } }
3、需要.so檔案;javacpp、javacv,不然會報錯