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

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

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