Add: save video thumbnail ad photo

pull/209/head
xufuji456 3 years ago
parent fea1c92ac9
commit 2a5285140a
  1. 8
      app/src/main/java/com/frank/ffmpeg/activity/ProbeFormatActivity.kt
  2. 8
      app/src/main/java/com/frank/ffmpeg/metadata/FFmpegMediaRetriever.java
  3. 36
      app/src/main/java/com/frank/ffmpeg/util/BitmapUtil.kt

@ -3,6 +3,7 @@ package com.frank.ffmpeg.activity
import android.annotation.SuppressLint
import android.graphics.Bitmap
import android.os.Bundle
import android.os.Environment
import android.os.Handler
import android.os.Message
import android.util.Log
@ -23,6 +24,7 @@ import com.frank.ffmpeg.util.FFmpegUtil
import com.frank.ffmpeg.util.FileUtil
import com.frank.ffmpeg.metadata.FFmpegMediaRetriever
import com.frank.ffmpeg.util.BitmapUtil
import java.lang.StringBuilder
/**
@ -41,6 +43,8 @@ class ProbeFormatActivity : BaseActivity() {
private val MSG_FRAME = 9099
private val savePhoto = false
@SuppressLint("HandlerLeak")
private val mHandler = object : Handler() {
override fun handleMessage(msg: Message) {
@ -190,6 +194,10 @@ class ProbeFormatActivity : BaseActivity() {
if (bitmap != null) {
Log.e("FFmpegRetriever", "bitmap width=${bitmap.width}--height=${bitmap.height}")
mHandler.obtainMessage(MSG_FRAME, bitmap).sendToTarget()
if (savePhoto) {
val thumbPath = Environment.getExternalStorageDirectory().path + "/thumb_" + System.currentTimeMillis() + ".png"
BitmapUtil.savePhoto(bitmap, thumbPath, this)
}
}
retriever.release()

@ -145,11 +145,9 @@ public class FFmpegMediaRetriever {
throw new IllegalArgumentException("Unsupported option: " + option);
}
BitmapFactory.Options bitmapOptions= new BitmapFactory.Options();
bitmapOptions.inScaled = true;
byte [] picture = native_getFrameAtTime(timeUs, option);
if (picture != null) {
return BitmapFactory.decodeByteArray(picture, 0, picture.length, bitmapOptions);
return BitmapFactory.decodeByteArray(picture, 0, picture.length, null);
}
return null;
@ -189,11 +187,9 @@ public class FFmpegMediaRetriever {
throw new IllegalArgumentException("Unsupported option: " + option);
}
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmapOptions.inScaled = true;
byte [] picture = native_getScaleFrameAtTime(timeUs, option, width, height);
if (picture != null) {
return BitmapFactory.decodeByteArray(picture, 0, picture.length, bitmapOptions);
return BitmapFactory.decodeByteArray(picture, 0, picture.length, null);
}
return null;

@ -1,11 +1,11 @@
package com.frank.ffmpeg.util
import android.content.Context
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Paint
import android.text.TextUtils
import java.io.File
import java.io.FileNotFoundException
import java.io.FileOutputStream
import java.io.IOException
@ -74,15 +74,29 @@ object BitmapUtil {
return true
}
/**
* delete file
*
* @param filePath filePath
* @return result of deletion
*/
fun deleteTextFile(filePath: String): Boolean {
val file = File(filePath)
return file.exists() && file.delete()
fun savePhoto(bitmap: Bitmap?, path: String?, context: Context?): Boolean {
if (bitmap == null || TextUtils.isEmpty(path) || context == null) {
return false
}
var fileOutputStream: FileOutputStream? = null
try {
fileOutputStream = FileOutputStream(path)
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream)
fileOutputStream.flush()
} catch (e: FileNotFoundException) {
return false
} catch (e: IOException) {
return false
} finally {
if (fileOutputStream != null) {
try {
fileOutputStream.close()
} catch (e: IOException) {
e.printStackTrace()
}
}
}
return true
}
}

Loading…
Cancel
Save