Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
Y
yzg-util
Project
Project
Details
Activity
Releases
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
YZG
yzg-util
Commits
a57f74da
Commit
a57f74da
authored
May 29, 2020
by
yanzg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
压缩视频
parent
ae22eb78
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
110 additions
and
5 deletions
+110
-5
pom.xml
yzg-util-image/pom.xml
+6
-0
MediaHelper.java
...image/src/main/java/com/yanzuoguang/util/MediaHelper.java
+70
-2
TestMediaHelper.java
yzg-util-image/src/test/java/helper/TestMediaHelper.java
+34
-3
No files found.
yzg-util-image/pom.xml
View file @
a57f74da
...
...
@@ -37,6 +37,12 @@
<version>
1.0.2
</version>
</dependency>
<dependency>
<groupId>
net.coobird
</groupId>
<artifactId>
thumbnailator
</artifactId>
<version>
0.4.11
</version>
</dependency>
<dependency>
<groupId>
com.yanzuoguang
</groupId>
<artifactId>
yzg-util-base
</artifactId>
...
...
yzg-util-image/src/main/java/com/yanzuoguang/util/MediaHelper.java
View file @
a57f74da
...
...
@@ -2,6 +2,7 @@ package com.yanzuoguang.util;
import
com.yanzuoguang.util.log.Log
;
import
it.sauronsoftware.jave.*
;
import
net.coobird.thumbnailator.Thumbnails
;
import
org.bytedeco.javacv.FFmpegFrameGrabber
;
import
org.bytedeco.javacv.Frame
;
import
org.bytedeco.javacv.Java2DFrameConverter
;
...
...
@@ -11,6 +12,7 @@ import javax.imageio.ImageIO;
import
javax.imageio.ImageWriteParam
;
import
javax.imageio.ImageWriter
;
import
javax.imageio.stream.ImageOutputStream
;
import
java.awt.*
;
import
java.awt.image.BufferedImage
;
import
java.awt.image.RenderedImage
;
import
java.io.File
;
...
...
@@ -30,6 +32,7 @@ public class MediaHelper {
public
static
final
int
FIRST_FRAME
=
5
;
public
static
final
int
DEFAULT_BIT_RATE
=
372
*
1000
;
public
static
final
float
DEFAULT_SIZE
=
1
f
;
public
static
final
float
DEFAULT_QUALITY
=
1
f
;
public
static
final
String
FORMAT_EMPTY
=
""
;
public
static
final
String
FORMAT_FLV
=
"flv"
;
...
...
@@ -357,13 +360,32 @@ public class MediaHelper {
*
* @param srcFilePath 来源路径
* @param descFilePath 目标路径
* @param quality 压缩程度,参数quality是取值0~1范围内
* @param quality 质量程度,取值0~1范围内
* @param size 大小程度,取值0~1范围内
* @throws IOException
*/
public
static
void
compressPic
(
String
srcFilePath
,
String
descFilePath
,
float
quality
)
throws
IOException
{
public
static
void
compressPicBySystem
(
String
srcFilePath
,
String
descFilePath
,
float
quality
,
float
size
)
throws
IOException
{
// 设置默认值
if
(
size
==
0
)
{
size
=
1
;
}
if
(
quality
==
0
)
{
quality
=
1
;
}
File
input
=
new
File
(
srcFilePath
);
BufferedImage
image
=
ImageIO
.
read
(
input
);
// 压缩图片大小
if
(
size
<
1
)
{
int
toWidth
=
(
int
)
(
image
.
getWidth
()
*
size
);
int
toHeight
=
(
int
)
(
image
.
getHeight
()
*
size
);
BufferedImage
buffImage
=
new
BufferedImage
(
toWidth
,
toHeight
,
BufferedImage
.
TYPE_INT_RGB
);
buffImage
.
getGraphics
().
drawImage
(
image
.
getScaledInstance
(
toWidth
,
toHeight
,
Image
.
SCALE_SMOOTH
),
0
,
0
,
null
);
image
=
buffImage
;
}
// 先指定Output,才能调用writer.write方法
File
output
=
new
File
(
descFilePath
);
...
...
@@ -382,6 +404,7 @@ public class MediaHelper {
if
(
param
.
canWriteCompressed
())
{
// 指定压缩方式为MODE_EXPLICIT
param
.
setCompressionMode
(
ImageWriteParam
.
MODE_EXPLICIT
);
// param.set
// 压缩程度,参数quality是取值0~1范围内
param
.
setCompressionQuality
(
quality
);
}
...
...
@@ -399,4 +422,49 @@ public class MediaHelper {
}
}
}
/**
* 压缩图片
*
* @param srcFilePath 来源路径
* @param descFilePath 目标路径
* @throws IOException
*/
public
static
void
compressPic
(
String
srcFilePath
,
String
descFilePath
)
throws
IOException
{
compressPic
(
srcFilePath
,
descFilePath
,
DEFAULT_QUALITY
,
DEFAULT_SIZE
);
}
/**
* 压缩图片
*
* @param srcFilePath 来源路径
* @param descFilePath 目标路径
* @param quality 压缩程度,参数quality是取值0~1范围内
* @throws IOException
*/
public
static
void
compressPic
(
String
srcFilePath
,
String
descFilePath
,
float
quality
)
throws
IOException
{
compressPic
(
srcFilePath
,
descFilePath
,
quality
,
DEFAULT_SIZE
);
}
/**
* 压缩图片
*
* @param srcFilePath 来源路径
* @param descFilePath 目标路径
* @param quality 质量程度,取值0~1范围内
* @param size 大小程度,取值0~1范围内
* @throws IOException
*/
public
static
void
compressPic
(
String
srcFilePath
,
String
descFilePath
,
float
quality
,
float
size
)
throws
IOException
{
Thumbnails
// 来源目录
.
of
(
srcFilePath
)
// 按比例缩小
.
scale
(
size
)
// 质量压缩
.
outputQuality
(
quality
)
// 目标文件
.
toFile
(
descFilePath
);
}
}
yzg-util-image/src/test/java/helper/TestMediaHelper.java
View file @
a57f74da
...
...
@@ -39,7 +39,8 @@ public class TestMediaHelper {
return
;
}
String
file
=
getFile
();
MediaHelper
.
getVideoFirstImage
(
file
,
file
+
".jpg"
);
String
targetFile
=
getTargetFile
();
MediaHelper
.
getVideoFirstImage
(
file
,
targetFile
+
".jpg"
);
isFirstImage
=
true
;
}
...
...
@@ -47,10 +48,40 @@ public class TestMediaHelper {
public
void
testImageZip
()
throws
IOException
{
// 生成测试图片
testVideoFirstImage
();
String
file
=
getFile
();
MediaHelper
.
compressPic
(
file
+
".jpg"
,
file
+
".zip.jpg"
,
0.25f
);
List
<
Runnable
>
list
=
new
ArrayList
<>();
for
(
int
i
=
0
;
i
<
sizes
.
length
;
i
++)
{
list
.
add
(
testImageZipThread
(
sizes
[
i
],
quotes
[
i
]));
}
RunnableListAuto
.
run
(
list
);
}
private
Runnable
testImageZipThread
(
float
size
,
float
quote
)
{
return
new
Runnable
()
{
@Override
public
void
run
()
{
try
{
testImageZip
(
size
,
quote
);
}
catch
(
IOException
e
)
{
throw
new
RuntimeException
(
e
);
}
}
};
}
private
void
testImageZip
(
float
size
,
float
quote
)
throws
IOException
{
String
file
=
getTargetFile
()
+
".jpg"
;
String
targetFile
=
getTargetFile
();
String
toName
=
String
.
format
(
"%s.size_%d.quot_%d.jpg"
,
targetFile
,
(
int
)
(
size
*
100
),
(
int
)
(
quote
*
100
));
String
toNameSystem
=
String
.
format
(
"%s.size_%d.quot_%d.system.jpg"
,
targetFile
,
(
int
)
(
size
*
100
),
(
int
)
(
quote
*
100
));
MediaHelper
.
compressPic
(
file
,
toName
,
quote
,
size
);
MediaHelper
.
compressPicBySystem
(
file
,
toNameSystem
,
quote
,
size
);
}
@Test
public
void
testVideoZipFlv
()
{
List
<
Runnable
>
list
=
new
ArrayList
<>();
...
...
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