Android 获取视频第一帧

今天药忘吃喽~ 2023-01-13 08:56 353阅读 0赞

1.本地视频

  1. MediaMetadataRetriever media = new MediaMetadataRetriever();
  2. media.setDataSource(videoPath);// videoPath 本地视频的路径
  3. Bitmap bitmap = media.getFrameAtTime(1, MediaMetadataRetriever.OPTION_CLOSEST_SYNC);
  4. ivAddPhoto.setImageBitmap(bitmap);

2.网络视频

  1. //网络
  2. public static Bitmap createVideoThumbnail(String filePath, int kind)
  3. {
  4. Bitmap bitmap = null;
  5. MediaMetadataRetriever retriever = new MediaMetadataRetriever();
  6. try
  7. {
  8. if (filePath.startsWith("http://")
  9. || filePath.startsWith("https://")
  10. || filePath.startsWith("widevine://"))
  11. {
  12. retriever.setDataSource(filePath, new Hashtable<String, String>());
  13. }
  14. else
  15. {
  16. retriever.setDataSource(filePath);
  17. }
  18. bitmap = retriever.getFrameAtTime(0, MediaMetadataRetriever.OPTION_CLOSEST_SYNC); //retriever.getFrameAtTime(-1);
  19. }
  20. catch (IllegalArgumentException ex)
  21. {
  22. // Assume this is a corrupt video file
  23. ex.printStackTrace();
  24. }
  25. catch (RuntimeException ex)
  26. {
  27. // Assume this is a corrupt video file.
  28. ex.printStackTrace();
  29. }
  30. finally
  31. {
  32. try
  33. {
  34. retriever.release();
  35. }
  36. catch (RuntimeException ex)
  37. {
  38. // Ignore failures while cleaning up.
  39. ex.printStackTrace();
  40. }
  41. }
  42. if (bitmap == null)
  43. {
  44. return null;
  45. }
  46. if (kind == MediaStore.Images.Thumbnails.MINI_KIND)
  47. { //压缩图片 开始处
  48. // Scale down the bitmap if it's too large.
  49. int width = bitmap.getWidth();
  50. int height = bitmap.getHeight();
  51. int max = Math.max(width, height);
  52. if (max > 512)
  53. {
  54. float scale = 512f / max;
  55. int w = Math.round(scale * width);
  56. int h = Math.round(scale * height);
  57. bitmap = Bitmap.createScaledBitmap(bitmap, w, h, true);
  58. }//压缩图片 结束处
  59. }
  60. else if (kind == MediaStore.Images.Thumbnails.MICRO_KIND)
  61. {
  62. bitmap = ThumbnailUtils.extractThumbnail(bitmap,
  63. 96,
  64. 96,
  65. ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
  66. }
  67. return bitmap;
  68. }
  69. //调用
  70. mIvBigShow.setImageBitmap(createVideoThumbnail(videoPath,MediaStore.Images.Thumbnails.MINI_KIND));

3.使用Glide获取视频第一帧

  1. //版本4+之后才有的功能
  2. /** * 加载第四秒的帧数作为封面 * url就是视频的地址 */
  3. public static void loadCover(ImageView imageView, String url, Context context) {
  4. imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
  5. Glide.with(context)
  6. .setDefaultRequestOptions(
  7. new RequestOptions()
  8. .frame(4000000)
  9. .centerCrop()
  10. .error(R.mipmap.eeeee)//可以忽略
  11. .placeholder(R.mipmap.ppppp)//可以忽略
  12. )
  13. .load(url)
  14. .into(imageView);
  15. }

发表评论

表情:
评论列表 (有 0 条评论,353人围观)

还没有评论,来说两句吧...

相关阅读

    相关 javacv获取视频第一

    javacv获取视频第一帧 公司网站需要上传视频,上传视频后在电脑浏览器上可以显示视频的封面,但是在手机微信浏览器上看不到封面, 在网上找了半天,说的最多的就俩中方法,这方