Thumbnail.java
12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package com.sw.laryngoscope.utils;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaMetadataRetriever;
import android.media.ThumbnailUtils;
import android.os.Build;
import android.provider.MediaStore;
import com.bumptech.glide.annotation.GlideModule;
import java.io.FileDescriptor;
import java.io.FileOutputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
//import wseemann.media.FFmpegMediaMetadataRetriever;
@GlideModule
public class Thumbnail {
public static Bitmap createVideoThumbnailBitmap(FileDescriptor fd, int targetWidth) {
return createVideoThumbnailBitmap(null, fd, targetWidth);
}
public static Bitmap createVideoThumbnailBitmap(String filePath, int targetWidth) {
return createVideoThumbnailBitmap(filePath, null, targetWidth);
}
private static Bitmap createVideoThumbnailBitmap(String filePath, FileDescriptor fd,
int targetWidth) {
Bitmap bitmap = null;
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
try {
if (filePath != null) {
retriever.setDataSource(filePath);
} else {
retriever.setDataSource(fd);
}
bitmap = retriever.getFrameAtTime(-1);
} catch (IllegalArgumentException ex) {
// Assume this is a corrupt video file
} catch (RuntimeException ex) {
// Assume this is a corrupt video file.
} finally {
try {
retriever.release();
} catch (RuntimeException ex) {
// Ignore failures while cleaning up.
}
}
if (bitmap == null) return null;
// Scale down the bitmap if it is bigger than we need.
int width = bitmap.getWidth();
int height = bitmap.getHeight();
if (width > targetWidth) {
float scale = (float) targetWidth / width;
int w = Math.round(scale * width);
int h = Math.round(scale * height);
Bitmap b1 = Bitmap.createScaledBitmap(bitmap, w, h, true);
//Matrix mx = new Matrix();
//Bitmap.createBitmap(b1, 0, 0, b1.getWidth(), b1.getHeight(), mx, true);
if (b1 != bitmap) {
bitmap.recycle();
bitmap = b1;
}
}
return bitmap;
}
public static void createVideoThumbnailBitmap1(String filePath, String toFile) {
new Thread(new Runnable() {
@Override
public void run() {
Bitmap bitmap = null;
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
try {
if (filePath != null) {
retriever.setDataSource(filePath);
}
//bitmap = retriever.getFrameAtTime(-1);
bitmap = retriever.getFrameAtTime(-1);
} catch (IllegalArgumentException ex) {
// Assume this is a corrupt video file
} catch (RuntimeException ex) {
// Assume this is a corrupt video file.
} finally {
try {
retriever.release();
} catch (RuntimeException ex) {
// Ignore failures while cleaning up.
}
}
FileOutputStream fos = null;
Logger.d("----createVideoThumbnailBitmap--------2" + toFile);
try {
fos = new FileOutputStream(toFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
} catch (Exception e) {
e.printStackTrace();
Logger.d("----createVideoThumbnailBitmap--------1");
}
try {
//if ( bitmap != null ) {
bitmap.recycle();
//}
} catch (Exception ex) {
}
}
}).start();
}
public static void createVideoThumbnailBitmap(String filePath, String toFile, Bitmap mBitmap) {
//final String filePath = "/sdcard/DCIM/OCamera/VID_20221103_002616.mp4";
new Thread(new Runnable() {
@Override
public void run() {
try {
Bitmap bitmap = null;
/*try {
bitmap = ThumbnailUtils.createVideoThumbnail(filePath, ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
} catch (IllegalArgumentException ex) {
// Assume this is a corrupt video file
} catch (RuntimeException ex) {
// Assume this is a corrupt video file.
} finally {
}*/
Logger.d("----createVideoThumbnailBitmap-----bitmap---0" + bitmap);
if ( bitmap == null || bitmap.getAllocationByteCount() == 0 ) {
try {
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.setDataSource(filePath);
//String duration = retriever.extractMetadata(
// android.media.MediaMetadataRetriever.METADATA_KEY_DURATION);
//String timeString = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
bitmap = retriever.getFrameAtTime(0, MediaMetadataRetriever.OPTION_CLOSEST_SYNC);
retriever.release();
} catch (Exception ex) {
}
}
Logger.d("----createVideoThumbnailBitmap-----bitmap---1" + bitmap);
if ( bitmap == null || bitmap.getAllocationByteCount() == 0 ) {
bitmap = ThumbnailUtils.createVideoThumbnail(filePath, MediaStore.Images.Thumbnails.MINI_KIND);
}
/*if ( bitmap == null || bitmap.getAllocationByteCount() == 0 ) {
FFmpegMediaMetadataRetriever retriever = new FFmpegMediaMetadataRetriever();
try {
retriever.setDataSource(filePath);
//String timeString = retriever.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_DURATION);
// 获取总长度
//long totalTime = Long.parseLong(timeString) * 1000;
//if (totalTime > 0) {
bitmap = retriever.getFrameAtTime(0);
Logger.d("----createVideoThumbnailBitmap-----bitmap---2" + bitmap);
//}
} catch (RuntimeException ex) {
ex.printStackTrace();
} finally {
try {
retriever.release();
} catch (RuntimeException ex) {
ex.printStackTrace();
}
}
}*/
if ( bitmap == null || bitmap.getAllocationByteCount() == 0 ) {
bitmap = mBitmap;
}
Logger.d("----createVideoThumbnailBitmap-----bitmap---3" + bitmap);
FileOutputStream fos = null;
Logger.d("----createVideoThumbnailBitmap--------2" + toFile);
try {
fos = new FileOutputStream(toFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
} catch (Exception e) {
e.printStackTrace();
Logger.d("----createVideoThumbnailBitmap--------1");
}
try {
//if ( bitmap != null ) {
bitmap.recycle();
//}
} catch (Exception ex) {
}
} catch (Exception ex) {
// Assume this is a corrupt video file.
}
}
}).start();
}
public static Bitmap createVideoThumbnail(String filePath) {
// MediaMetadataRetriever is available on API Level 8
// but is hidden until API Level 10
Class<?> clazz = null;
Object instance = null;
try {
clazz = Class.forName("android.media.MediaMetadataRetriever");
instance = clazz.newInstance();
Method method = clazz.getMethod("setDataSource", String.class);
method.invoke(instance, filePath);
// The method name changes between API Level 9 and 10.
if (Build.VERSION.SDK_INT <= 9) {
return (Bitmap) clazz.getMethod("captureFrame").invoke(instance);
} else {
byte[] data = (byte[]) clazz.getMethod("getEmbeddedPicture").invoke(instance);
if (data != null) {
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
if (bitmap != null) return bitmap;
}
return (Bitmap) clazz.getMethod("getFrameAtTime").invoke(instance);
}
} catch (IllegalArgumentException ex) {
// Assume this is a corrupt video file
} catch (RuntimeException ex) {
// Assume this is a corrupt video file.
} catch (InstantiationException e) {
Logger.e("createVideoThumbnail" + e);
} catch (InvocationTargetException e) {
Logger.e("createVideoThumbnail" + e);
} catch (ClassNotFoundException e) {
Logger.e("createVideoThumbnail" + e);
} catch (NoSuchMethodException e) {
Logger.e("createVideoThumbnail" + e);
} catch (IllegalAccessException e) {
Logger.e("createVideoThumbnail" + e);
} finally {
try {
if (instance != null) {
clazz.getMethod("release").invoke(instance);
}
} catch (Exception ignored) {
}
}
return null;
}
public static int getVideoDur(String filePath) {
int dur = 0;
try {
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.setDataSource(filePath);
String timeString = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
retriever.release();
dur = Integer.parseInt(timeString);
} catch (Exception ex) {
}
return dur;
}
public static int getVideoRate(String filePath) {
int rate = 0;
try {
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.setDataSource(filePath);
String rateString = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_CAPTURE_FRAMERATE);
retriever.release();
rate = Integer.parseInt(rateString);
} catch (Exception ex) {
}
return rate;
}
public static int getVideoWidth(String filePath) {
int width = 0;
try {
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.setDataSource(filePath);
String widthString = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH);
retriever.release();
width = Integer.parseInt(widthString);
} catch (Exception ex) {
}
return width;
}
public static int getVideoHeight(String filePath) {
int height = 0;
try {
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.setDataSource(filePath);
String heightString = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT);
retriever.release();
height = Integer.parseInt(heightString);
} catch (Exception ex) {
}
return height;
}
/*bitmap = Glide.with(MyApplication.getContext())
.setDefaultRequestOptions(
new RequestOptions()
.frame(4000000)
.centerCrop()
)
.asBitmap() //必须
.load(filePath)
.into(100, 100)
.get();*/
}