PageRenderTime 224ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/BabySelfie/app/src/main/java/babyselfie/mobi/bot/services/MovieCreatorService.java

https://gitlab.com/victorbotsales/babySelfie
Java | 174 lines | 81 code | 27 blank | 66 comment | 3 complexity | aad619fce15495913feb2687a5ca2d94 MD5 | raw file
  1. package babyselfie.mobi.bot.services;
  2. import android.app.IntentService;
  3. import android.app.ProgressDialog;
  4. import android.content.Intent;
  5. import android.os.IBinder;
  6. import android.support.annotation.Nullable;
  7. import android.util.Log;
  8. import com.R;
  9. import babyselfie.mobi.Util.CameraUtil;
  10. import babyselfie.mobi.bot.Tela9VideoActivity;
  11. import org.bytedeco.javacpp.avcodec;
  12. import org.bytedeco.javacpp.opencv_core;
  13. import org.bytedeco.javacv.FFmpegFrameRecorder;
  14. import org.bytedeco.javacv.Frame;
  15. import org.bytedeco.javacv.OpenCVFrameConverter;
  16. import java.io.File;
  17. import static org.bytedeco.javacpp.opencv_imgcodecs.cvLoadImage;
  18. /**
  19. * Created by Bot on 07/03/2017.
  20. */
  21. public class MovieCreatorService extends IntentService {
  22. private int imageHeight;
  23. private int imageWidth;
  24. private ProgressDialog progressDialog;
  25. /**
  26. * Creates an IntentService. Invoked by your subclass's constructor.
  27. *
  28. */
  29. public MovieCreatorService() {
  30. super("MovieCreatorService");
  31. }
  32. @Nullable
  33. @Override
  34. public IBinder onBind(Intent intent) {
  35. return null;
  36. }
  37. @Override
  38. protected void onHandleIntent(Intent intent) {
  39. // onPreExecute();
  40. createMovie();
  41. Log.v("Sucesso","video criado com sucesso!");
  42. onPostExecute();
  43. }
  44. protected String createMovie() {
  45. // File folderAll = Environment
  46. // .getExternalStorageDirectory();
  47. File folderBabySelfie = CameraUtil.getGalleryDirectory();
  48. // File folderMovies = Environment
  49. // .getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES);
  50. String path = CameraUtil.getVideosDirectory().getPath();
  51. File[] listOfFiles = folderBabySelfie.listFiles();
  52. int imgCounter = listOfFiles.length;
  53. // File[] listOfFiles2 = folderMovies.listFiles();
  54. // int imgCounter = 0;
  55. // System.out.println("listOfFiles size::" + listOfFiles.length);
  56. // if (listOfFiles.length > 0) {
  57. //
  58. //
  59. // for (int j = 0; j < listOfFiles.length; j++) {
  60. //
  61. // if (listOfFiles[j].getAbsolutePath().endsWith(".JPEG")
  62. // || listOfFiles[j].getAbsolutePath().endsWith(
  63. // ".jpeg")
  64. // || listOfFiles[j].getAbsolutePath()
  65. // .endsWith(".JPG")
  66. // || listOfFiles[j].getAbsolutePath()
  67. // .endsWith(".jpg")
  68. // || listOfFiles[j].getAbsolutePath()
  69. // .endsWith(".png")) {
  70. // System.out.println(j + " position listOfFiles::"
  71. // + listOfFiles[j].getAbsolutePath());
  72. // Bitmap bmp = BitmapFactory.decodeFile(listOfFiles[j]
  73. // .getAbsolutePath());
  74. //
  75. // if (bmp != null) {
  76. // imageHeight = bmp.getHeight();
  77. // imageWidth = bmp.getWidth();
  78. // System.out.println("bmp is not null");
  79. // ++imgCounter;
  80. // }
  81. //
  82. // }
  83. //
  84. // }
  85. //
  86. // }
  87. Log.v("pathReal", path);
  88. FFmpegFrameRecorder recorder = new FFmpegFrameRecorder(path + "/video.mp4", 768, 1024);
  89. OpenCVFrameConverter.ToIplImage converter = new OpenCVFrameConverter.ToIplImage();
  90. try {
  91. recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264);
  92. recorder.setVideoOption("preset", "ultrafast");
  93. recorder.setVideoQuality(10);
  94. recorder.setFormat("mp4");
  95. recorder.setFrameRate(1);
  96. recorder.setPixelFormat(org.bytedeco.javacpp.avutil.AV_PIX_FMT_YUV420P);
  97. recorder.start();
  98. // long imageTime = 50000000;
  99. for (int i = 0; i < imgCounter; i++) {
  100. opencv_core.IplImage img = cvLoadImage(listOfFiles[i].getAbsolutePath());
  101. /*
  102. Fazendo resize da imagem para a resolução do vídeo de 1024 x 768 criado! ~~ não
  103. é necessário, mas deve facilitar o processamento pois a imagem original tem um
  104. tamanho muito grande!
  105. */
  106. // IplImage dest = cvCreateImage(cvSize(768, 1024), img.depth(), img.nChannels());
  107. // cvResize(img, dest, CV_INTER_NN);
  108. Log.v("recordtimeStamp", recorder.getTimestamp() + "");
  109. /*
  110. Frame da imagem resized para 1024x768
  111. */
  112. // Frame frame = converter.convert(dest);
  113. /*
  114. Frame da imagem orignal
  115. */
  116. Frame frame = converter.convert(img);
  117. recorder.record(frame);
  118. }
  119. recorder.stop();
  120. // recorder.release();
  121. } catch (Exception e) {
  122. e.printStackTrace();
  123. } finally {
  124. // TODO Fazer isso performatico, essa ñ é a forma recomendada
  125. //sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES))));
  126. }
  127. return "video.mp4";
  128. }
  129. protected void onPreExecute() {
  130. progressDialog = new ProgressDialog(this, R.style.AppTheme_Dark_Dialog);
  131. progressDialog.setTitle(this.getString(R.string.carregando_video));
  132. progressDialog.setMessage(this.getString(R.string.espere_por_favor));
  133. progressDialog.setCancelable(false);
  134. progressDialog.setIndeterminate(true);
  135. progressDialog.show();
  136. }
  137. protected void onPostExecute() {
  138. if (progressDialog != null) {
  139. progressDialog.dismiss();
  140. }
  141. // Intent it = new Intent(this,Tela9VideoActivity.class);
  142. // this.startActivity(it);
  143. Intent dialogIntent = new Intent(this, Tela9VideoActivity.class);
  144. dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  145. startActivity(dialogIntent);
  146. }
  147. }