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
a9331864
Commit
a9331864
authored
Jul 09, 2020
by
yanzg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
SQL层级处理的支持
parent
2c507875
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
140 additions
and
123 deletions
+140
-123
ImageHelper.java
...image/src/main/java/com/yanzuoguang/util/ImageHelper.java
+139
-0
MediaHelper.java
...image/src/main/java/com/yanzuoguang/util/MediaHelper.java
+1
-123
No files found.
yzg-util-image/src/main/java/com/yanzuoguang/util/ImageHelper.java
0 → 100644
View file @
a9331864
package
com
.
yanzuoguang
.
util
;
import
net.coobird.thumbnailator.Thumbnails
;
import
javax.imageio.IIOImage
;
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.io.File
;
import
java.io.FileOutputStream
;
import
java.io.IOException
;
import
java.io.OutputStream
;
/**
* 图片处理程序
*
* @author 颜佐光
*/
public
class
ImageHelper
{
public
static
final
float
DEFAULT_SIZE
=
1
f
;
public
static
final
float
DEFAULT_QUALITY
=
1
f
;
/**
* 压缩图片
*
* @param srcFilePath 来源路径
* @param descFilePath 目标路径
* @param quality 质量程度,取值0~1范围内
* @param size 大小程度,取值0~1范围内
* @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
);
// 指定写图片的方式为 jpg
ImageWriter
writer
=
null
;
OutputStream
out
=
null
;
ImageOutputStream
ios
=
null
;
try
{
writer
=
ImageIO
.
getImageWritersByFormatName
(
"jpg"
).
next
();
out
=
new
FileOutputStream
(
output
);
ios
=
ImageIO
.
createImageOutputStream
(
out
);
writer
.
setOutput
(
ios
);
ImageWriteParam
param
=
writer
.
getDefaultWriteParam
();
if
(
param
.
canWriteCompressed
())
{
// 指定压缩方式为MODE_EXPLICIT
param
.
setCompressionMode
(
ImageWriteParam
.
MODE_EXPLICIT
);
// param.set
// 压缩程度,参数quality是取值0~1范围内
param
.
setCompressionQuality
(
quality
);
}
// 调用write方法,向输入流写图片
writer
.
write
(
null
,
new
IIOImage
(
image
,
null
,
null
),
param
);
}
finally
{
if
(
out
!=
null
)
{
out
.
close
();
}
if
(
ios
!=
null
)
{
ios
.
close
();
}
if
(
writer
!=
null
)
{
writer
.
dispose
();
}
}
}
/**
* 压缩图片
*
* @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/main/java/com/yanzuoguang/util/MediaHelper.java
View file @
a9331864
...
@@ -2,23 +2,15 @@ package com.yanzuoguang.util;
...
@@ -2,23 +2,15 @@ package com.yanzuoguang.util;
import
com.yanzuoguang.util.log.Log
;
import
com.yanzuoguang.util.log.Log
;
import
it.sauronsoftware.jave.*
;
import
it.sauronsoftware.jave.*
;
import
net.coobird.thumbnailator.Thumbnails
;
import
org.bytedeco.javacv.FFmpegFrameGrabber
;
import
org.bytedeco.javacv.FFmpegFrameGrabber
;
import
org.bytedeco.javacv.Frame
;
import
org.bytedeco.javacv.Frame
;
import
org.bytedeco.javacv.Java2DFrameConverter
;
import
org.bytedeco.javacv.Java2DFrameConverter
;
import
javax.imageio.IIOImage
;
import
javax.imageio.ImageIO
;
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.BufferedImage
;
import
java.awt.image.RenderedImage
;
import
java.awt.image.RenderedImage
;
import
java.io.File
;
import
java.io.File
;
import
java.io.FileOutputStream
;
import
java.io.IOException
;
import
java.io.IOException
;
import
java.io.OutputStream
;
/**
/**
* 视频帮助类
* 视频帮助类
...
@@ -27,12 +19,10 @@ import java.io.OutputStream;
...
@@ -27,12 +19,10 @@ import java.io.OutputStream;
*
*
* @author 颜佐光
* @author 颜佐光
*/
*/
public
class
MediaHelper
{
public
class
MediaHelper
extends
ImageHelper
{
public
static
final
int
FIRST_FRAME
=
5
;
public
static
final
int
FIRST_FRAME
=
5
;
public
static
final
int
DEFAULT_BIT_RATE
=
372
*
1000
;
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_EMPTY
=
""
;
public
static
final
String
FORMAT_FLV
=
"flv"
;
public
static
final
String
FORMAT_FLV
=
"flv"
;
...
@@ -365,116 +355,4 @@ public class MediaHelper {
...
@@ -365,116 +355,4 @@ public class MediaHelper {
return
bufferedImage
;
return
bufferedImage
;
}
}
/**
* 压缩图片
*
* @param srcFilePath 来源路径
* @param descFilePath 目标路径
* @param quality 质量程度,取值0~1范围内
* @param size 大小程度,取值0~1范围内
* @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
);
// 指定写图片的方式为 jpg
ImageWriter
writer
=
null
;
OutputStream
out
=
null
;
ImageOutputStream
ios
=
null
;
try
{
writer
=
ImageIO
.
getImageWritersByFormatName
(
"jpg"
).
next
();
out
=
new
FileOutputStream
(
output
);
ios
=
ImageIO
.
createImageOutputStream
(
out
);
writer
.
setOutput
(
ios
);
ImageWriteParam
param
=
writer
.
getDefaultWriteParam
();
if
(
param
.
canWriteCompressed
())
{
// 指定压缩方式为MODE_EXPLICIT
param
.
setCompressionMode
(
ImageWriteParam
.
MODE_EXPLICIT
);
// param.set
// 压缩程度,参数quality是取值0~1范围内
param
.
setCompressionQuality
(
quality
);
}
// 调用write方法,向输入流写图片
writer
.
write
(
null
,
new
IIOImage
(
image
,
null
,
null
),
param
);
}
finally
{
if
(
out
!=
null
)
{
out
.
close
();
}
if
(
ios
!=
null
)
{
ios
.
close
();
}
if
(
writer
!=
null
)
{
writer
.
dispose
();
}
}
}
/**
* 压缩图片
*
* @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
);
}
}
}
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