Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
D
data-server
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
licc
data-server
Commits
c6a7ca65
Commit
c6a7ca65
authored
Mar 09, 2021
by
codezwjava
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
视频截取第一帧工具类注释
parent
bdac358f
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
88 additions
and
88 deletions
+88
-88
FrameGrabberKit.java
.../main/java/cn/wisenergy/common/utils/FrameGrabberKit.java
+88
-88
No files found.
wisenergy-common/src/main/java/cn/wisenergy/common/utils/FrameGrabberKit.java
View file @
c6a7ca65
package
cn
.
wisenergy
.
common
.
utils
;
//package cn.wisenergy.common.utils;
import
java.awt.Image
;
import
java.awt.image.BufferedImage
;
import
java.io.File
;
import
javax.imageio.ImageIO
;
import
org.bytedeco.javacpp.opencv_core
;
import
org.bytedeco.javacv.FFmpegFrameGrabber
;
import
org.bytedeco.javacv.Frame
;
/**
* 截取视频图片
* @ClassName: FrameGrabberKit
*/
public
class
FrameGrabberKit
{
/**
* 获取视频图片
* @param videofile 源视频文件路径
* @param framefile 截取帧的图片存放路径
* @param imgfile 视频封面图保存路径
* @return
*/
public
static
String
getVedioImg
(
String
videofile
,
String
framefile
,
String
imgfile
){
String
ImgUrl
=
""
;
//截取封面图
try
{
fetchFrame
(
videofile
,
framefile
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
// 完整的ImgUrl
ImgUrl
=
imgfile
;
//视频封面图保存路径
return
ImgUrl
;
}
/**
* 获取指定视频的帧并保存为图片至指定目录
* @param videofile 源视频文件路径
* @param framefile 截取帧的图片存放路径 例:F:\hfkjrecorder\target\4.jpg
* @throws Exception
*/
public
static
void
fetchFrame
(
String
videofile
,
String
framefile
)
throws
Exception
{
long
start
=
System
.
currentTimeMillis
();
File
targetFile
=
new
File
(
framefile
);
FFmpegFrameGrabber
ff
=
new
FFmpegFrameGrabber
(
videofile
);
ff
.
start
();
int
lenght
=
ff
.
getLengthInFrames
();
int
i
=
0
;
int
interceptionFrames
=
30
;
//截取第几帧
//默认截取第50帧,如果第50帧大于视频总帧数的8成直接取长度lenght * 0.3
if
(
interceptionFrames
>=
lenght
*
0.8
)
{
interceptionFrames
=
(
int
)(
lenght
*
0.3
);
}
Frame
f
=
null
;
while
(
i
<
lenght
)
{
// 过滤 前 interceptionFrames 帧,避免出现全黑的图片,依自己情况而定
f
=
ff
.
grabFrame
();
if
((
i
>
interceptionFrames
)
&&
(
f
.
image
!=
null
))
{
break
;
}
i
++;
}
opencv_core
.
IplImage
img
=
f
.
image
;
int
owidth
=
img
.
width
();
int
oheight
=
img
.
height
();
// 对截取的帧进行等比例缩放 宽350、高160
// if(owidth > oheight) {//宽大于高
//
//
// }else {//高大于宽
//
//
//import java.awt.Image;
//import java.awt.image.BufferedImage;
//import java.io.File;
//import javax.imageio.ImageIO;
//import org.bytedeco.javacpp.opencv_core;
//import org.bytedeco.javacv.FFmpegFrameGrabber;
//import org.bytedeco.javacv.Frame;
//
//
///**
//* 截取视频图片
//* @ClassName: FrameGrabberKit
//*/
//public class FrameGrabberKit {
//
// /**
// * 获取视频图片
// * @param videofile 源视频文件路径
// * @param framefile 截取帧的图片存放路径
// * @param imgfile 视频封面图保存路径
// * @return
// */
// public static String getVedioImg(String videofile, String framefile,String imgfile){
// String ImgUrl="";
// //截取封面图
// try {
// fetchFrame(videofile, framefile);
// } catch (Exception e) {
// e.printStackTrace();
// }
// // 完整的ImgUrl
// ImgUrl = imgfile;//视频封面图保存路径
// return ImgUrl;
// }
///**
// * 获取指定视频的帧并保存为图片至指定目录
// * @param videofile 源视频文件路径
// * @param framefile 截取帧的图片存放路径 例:F:\hfkjrecorder\target\4.jpg
// * @throws Exception
// */
// public static void fetchFrame(String videofile, String framefile) throws Exception {
// long start = System.currentTimeMillis();
// File targetFile = new File(framefile);
// FFmpegFrameGrabber ff = new FFmpegFrameGrabber(videofile);
// ff.start();
// int lenght = ff.getLengthInFrames();
// int i = 0;
// int interceptionFrames = 30;//截取第几帧
// //默认截取第50帧,如果第50帧大于视频总帧数的8成直接取长度lenght * 0.3
// if(interceptionFrames >= lenght * 0.8) {
// interceptionFrames = (int)(lenght * 0.3);
// }
// Frame f = null;
// while (i < lenght) {
// // 过滤 前 interceptionFrames 帧,避免出现全黑的图片,依自己情况而定
// f = ff.grabFrame();
// if ((i > interceptionFrames) && (f.image != null)) {
// break;
// }
// i++;
// }
// }
int
width
=
800
;
// opencv_core.IplImage img = f.image;
int
height
=
(
int
)
(((
double
)
width
/
owidth
)
*
oheight
);
// int owidth = img.width();
/**
// int oheight = img.height();
width - 所创建图像的宽度
// // 对截取的帧进行等比例缩放 宽350、高160
height - 所创建图像的高度
//// if(owidth > oheight) {//宽大于高
imageType - 所创建图像的类型
////
TYPE_3BYTE_BGR - 表示一个具有 8 位 RGB 颜色分量的图像,对应于 Windows 风格的 BGR 颜色模型,具有用 3 字节存储的 Blue、Green 和 Red 三种颜色。
//// }else {//高大于宽
*/
////
BufferedImage
bi
=
new
BufferedImage
(
width
,
height
,
BufferedImage
.
TYPE_3BYTE_BGR
);
//// }
//此方法返回 Graphics2D,但此处是出于向后兼容性的考虑。
// int width = 800;
bi
.
getGraphics
().
drawImage
(
f
.
image
.
getBufferedImage
().
getScaledInstance
(
width
,
height
,
Image
.
SCALE_SMOOTH
),
// int height = (int) (((double) width / owidth) * oheight);
0
,
0
,
null
);
// /**
ImageIO
.
write
(
bi
,
"jpg"
,
targetFile
);
// width - 所创建图像的宽度
ff
.
flush
();
// height - 所创建图像的高度
ff
.
stop
();
// imageType - 所创建图像的类型
System
.
out
.
println
(
System
.
currentTimeMillis
()
-
start
);
// TYPE_3BYTE_BGR - 表示一个具有 8 位 RGB 颜色分量的图像,对应于 Windows 风格的 BGR 颜色模型,具有用 3 字节存储的 Blue、Green 和 Red 三种颜色。
}
// */
}
// BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
// //此方法返回 Graphics2D,但此处是出于向后兼容性的考虑。
// bi.getGraphics().drawImage(f.image.getBufferedImage().getScaledInstance(width, height, Image.SCALE_SMOOTH),
// 0, 0, null);
// ImageIO.write(bi, "jpg", targetFile);
// ff.flush();
// ff.stop();
// System.out.println(System.currentTimeMillis() - start);
// }
//}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment