Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Z
zhny
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
王夏晖
zhny
Commits
998fe633
Commit
998fe633
authored
May 24, 2018
by
张大伟
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
增加二维码生成工具
parent
4eb5a4bc
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
256 additions
and
1 deletion
+256
-1
pom.xml
pom.xml
+5
-1
QRCodeUtils.java
src/main/java/org/rcisoft/core/util/QRCodeUtils.java
+251
-0
No files found.
pom.xml
View file @
998fe633
...
...
@@ -340,7 +340,11 @@
<artifactId>
weixin-java-mp
</artifactId>
<version>
3.0.0
</version>
</dependency>
<dependency>
<groupId>
com.google.zxing
</groupId>
<artifactId>
javase
</artifactId>
<version>
3.3.2
</version>
</dependency>
</dependencies>
...
...
src/main/java/org/rcisoft/core/util/QRCodeUtils.java
0 → 100644
View file @
998fe633
package
org
.
rcisoft
.
core
.
util
;
import
java.awt.Color
;
import
java.awt.Graphics
;
import
java.awt.Graphics2D
;
import
java.awt.Image
;
import
java.awt.font.ImageGraphicAttribute
;
import
java.awt.image.BufferedImage
;
import
java.io.File
;
import
java.io.IOException
;
import
java.util.HashMap
;
import
java.util.Hashtable
;
import
java.util.List
;
import
javax.imageio.ImageIO
;
import
com.google.zxing.BarcodeFormat
;
import
com.google.zxing.EncodeHintType
;
import
com.google.zxing.MultiFormatWriter
;
import
com.google.zxing.WriterException
;
import
com.google.zxing.client.j2se.BufferedImageLuminanceSource
;
import
com.google.zxing.common.BitMatrix
;
import
com.google.zxing.qrcode.decoder.ErrorCorrectionLevel
;
public
class
QRCodeUtils
{
/**
* 定义二维码的参数
*/
HashMap
hints
=
new
HashMap
();
/** 数据字符编码格式 **/
private
static
String
codeFormate
=
"UTF-8"
;
/** 二维码纠错等级 **/
private
static
ErrorCorrectionLevel
QRCodeErrorLevel
=
ErrorCorrectionLevel
.
M
;
/** 图片边距 **/
private
static
int
imagePadding
=
0
;
/** 二维码尺寸 **/
private
static
int
QRCODE_SIZE
=
400
;
/** 输出图片格式 **/
private
static
String
FORMAT_NAME
=
"JPG"
;
/** 是否去除外部的边框 **/
private
static
boolean
cleanPadding
=
false
;
private
static
BufferedImage
createQRCodeImage
(
String
dataInfo
,
Hashtable
<
EncodeHintType
,
Object
>
hints
)
throws
WriterException
{
if
(
hints
==
null
||
hints
.
isEmpty
()){
hints
=
new
Hashtable
<
EncodeHintType
,
Object
>();
hints
.
put
(
EncodeHintType
.
ERROR_CORRECTION
,
QRCodeErrorLevel
);
hints
.
put
(
EncodeHintType
.
CHARACTER_SET
,
codeFormate
);
hints
.
put
(
EncodeHintType
.
MARGIN
,
imagePadding
);
}
BitMatrix
bitMatrix
=
new
MultiFormatWriter
().
encode
(
dataInfo
,
BarcodeFormat
.
QR_CODE
,
QRCODE_SIZE
,
QRCODE_SIZE
,
hints
);
//此处判断是否需要将图片两边的留白进行处理
BufferedImage
resultImage
=
null
;
if
(
cleanPadding
){
//1.1去白边
int
[]
rec
=
bitMatrix
.
getEnclosingRectangle
();
int
resWidth
=
rec
[
2
]
+
5
;
int
resHeight
=
rec
[
3
]
+
5
;
BitMatrix
resMatrix
=
new
BitMatrix
(
resWidth
,
resHeight
);
resMatrix
.
clear
();
for
(
int
i
=
0
;
i
<
resWidth
;
i
++)
{
for
(
int
j
=
0
;
j
<
resHeight
;
j
++)
{
if
(
bitMatrix
.
get
(
i
+
rec
[
0
],
j
+
rec
[
1
]))
{
resMatrix
.
set
(
i
,
j
);
}
}
}
//2
int
width
=
resMatrix
.
getWidth
();
int
height
=
resMatrix
.
getHeight
();
resultImage
=
new
BufferedImage
(
width
,
height
,
BufferedImage
.
TYPE_INT_RGB
);
for
(
int
x
=
0
;
x
<
width
;
x
++)
{
for
(
int
y
=
0
;
y
<
height
;
y
++)
{
resultImage
.
setRGB
(
x
,
y
,
resMatrix
.
get
(
x
,
y
)
==
true
?
Color
.
BLACK
.
getRGB
():
Color
.
WHITE
.
getRGB
());
}
}
}
else
{
int
width
=
bitMatrix
.
getWidth
();
int
height
=
bitMatrix
.
getHeight
();
resultImage
=
new
BufferedImage
(
width
,
height
,
BufferedImage
.
TYPE_INT_RGB
);
for
(
int
x
=
0
;
x
<
width
;
x
++)
{
for
(
int
y
=
0
;
y
<
height
;
y
++)
{
resultImage
.
setRGB
(
x
,
y
,
bitMatrix
.
get
(
x
,
y
)
?
0xFF000000
:
0xFFFFFFFF
);
}
}
}
//此处开始将图片输出出来
return
resultImage
;
}
public
static
String
createQRCodeFile
(
String
dataInfo
,
String
filePath
,
String
fileName
)
throws
WriterException
,
IOException
{
//输出二维码
BufferedImage
QRCodeImage
=
createQRCodeImage
(
dataInfo
,
null
);
//判断文件路径是否存在
File
saveDir
=
new
File
(
filePath
);
if
(!
saveDir
.
exists
()){
saveDir
.
mkdirs
();
}
String
filepathStr
=
filePath
+
"/"
+
fileName
+
"."
+
FORMAT_NAME
;
//开始写文件啦
ImageIO
.
write
(
QRCodeImage
,
FORMAT_NAME
,
new
File
(
filepathStr
));
return
filepathStr
;
}
/**
* 批量生成纯二维码
* @param dataList
* @param filePath
* @param fileName
* @return
* @throws WriterException
* @throws IOException
*/
public
static
String
createQRCodeFileList
(
List
<
String
>
dataList
,
String
filePath
,
List
<
String
>
fileNameList
)
throws
WriterException
,
IOException
{
Hashtable
<
EncodeHintType
,
Object
>
hints
=
new
Hashtable
<
EncodeHintType
,
Object
>();
hints
.
put
(
EncodeHintType
.
ERROR_CORRECTION
,
QRCodeErrorLevel
);
hints
.
put
(
EncodeHintType
.
CHARACTER_SET
,
codeFormate
);
hints
.
put
(
EncodeHintType
.
MARGIN
,
imagePadding
);
//判断文件路径是否存在
File
saveDir
=
new
File
(
filePath
);
if
(!
saveDir
.
exists
()){
saveDir
.
mkdirs
();
}
if
(
dataList
!=
null
&&
!
dataList
.
isEmpty
()
&&
fileNameList
!=
null
&&
!
dataList
.
isEmpty
()){
if
(
dataList
.
size
()==
fileNameList
.
size
()){
for
(
int
i
=
0
;
i
<
dataList
.
size
();
i
++){
String
dataInfo
=
dataList
.
get
(
i
);
//输出二维码
BufferedImage
QRCodeImage
=
createQRCodeImage
(
dataInfo
,
hints
);
String
filepathStr
=
filePath
+
"/"
+
fileNameList
.
get
(
i
)+
"."
+
FORMAT_NAME
;
//开始写文件啦
ImageIO
.
write
(
QRCodeImage
,
FORMAT_NAME
,
new
File
(
filepathStr
));
System
.
out
.
println
(
"当前输出第"
+(
i
+
1
)+
"个,共计:"
+
dataList
.
size
()+
"个"
);
}
return
"SUCCESS"
;
}
else
{
return
"ERROR"
;
}
}
else
{
return
"ERROR"
;
}
}
public
static
String
createQRCordAndCompositeImage
(
String
dataInfo
,
String
backgroundFilePath
,
String
filePath
,
String
fileName
,
int
x
,
int
y
,
int
QRCodeSize
)
throws
IOException
,
WriterException
{
//判断文件路径是否存在
File
saveDir
=
new
File
(
filePath
);
if
(!
saveDir
.
exists
()){
saveDir
.
mkdirs
();
}
//读取背景图
BufferedImage
backGroundImage
=
ImageIO
.
read
(
new
File
(
backgroundFilePath
));
//获取背景图大小
Graphics
graphics
=
backGroundImage
.
getGraphics
();
BufferedImage
QRCodeImage
=
createQRCodeImage
(
dataInfo
,
null
);
graphics
.
drawImage
(
QRCodeImage
,
x
,
y
,
QRCodeSize
,
QRCodeSize
,
null
);
graphics
.
dispose
();
String
filepathStr
=
filePath
+
"/"
+
fileName
+
"."
+
FORMAT_NAME
;
ImageIO
.
write
(
backGroundImage
,
FORMAT_NAME
,
new
File
(
filepathStr
));
return
filepathStr
;
}
public
static
String
createQRCodeAndCompImageList
(
List
<
String
>
dataList
,
String
backgroundFilePath
,
String
filePath
,
List
<
String
>
fileNameList
,
int
x
,
int
y
,
int
QRCodeSize
)
throws
IOException
,
WriterException
{
//判断文件路径是否存在
File
saveDir
=
new
File
(
filePath
);
if
(!
saveDir
.
exists
()){
saveDir
.
mkdirs
();
}
//读取背景图
BufferedImage
backGroundImage
=
ImageIO
.
read
(
new
File
(
backgroundFilePath
));
//获取背景图大小
int
height
=
backGroundImage
.
getHeight
();
int
width
=
backGroundImage
.
getWidth
();
//构建二维码配置信息
Hashtable
<
EncodeHintType
,
Object
>
hints
=
new
Hashtable
<
EncodeHintType
,
Object
>();
hints
.
put
(
EncodeHintType
.
ERROR_CORRECTION
,
QRCodeErrorLevel
);
hints
.
put
(
EncodeHintType
.
CHARACTER_SET
,
codeFormate
);
hints
.
put
(
EncodeHintType
.
MARGIN
,
imagePadding
);
if
(
dataList
!=
null
&&
!
dataList
.
isEmpty
()
&&
fileNameList
!=
null
&&
!
dataList
.
isEmpty
()){
if
(
dataList
.
size
()==
fileNameList
.
size
()){
for
(
int
i
=
0
;
i
<
dataList
.
size
();
i
++){
String
dataInfo
=
dataList
.
get
(
i
);
//输出二维码
BufferedImage
QRCodeImage
=
createQRCodeImage
(
dataInfo
,
hints
);
Graphics
graphics
=
backGroundImage
.
getGraphics
();
graphics
.
drawImage
(
QRCodeImage
,
x
,
y
,
QRCodeSize
,
QRCodeSize
,
null
);
graphics
.
dispose
();
String
filepathStr
=
filePath
+
"/"
+
fileNameList
.
get
(
i
)+
"."
+
FORMAT_NAME
;
//开始写文件啦
ImageIO
.
write
(
backGroundImage
,
FORMAT_NAME
,
new
File
(
filepathStr
));
System
.
out
.
println
(
"当前输出第"
+(
i
+
1
)+
"个,共计:"
+
dataList
.
size
()+
"个"
);
}
return
"SUCCESS"
;
}
else
{
return
"ERROR"
;
}
}
else
{
return
"ERROR"
;
}
}
public
static
String
getCodeFormate
()
{
return
codeFormate
;
}
public
static
void
setCodeFormate
(
String
codeFormate
)
{
QRCodeUtils
.
codeFormate
=
codeFormate
;
}
public
static
ErrorCorrectionLevel
getQRCodeErrorLevel
()
{
return
QRCodeErrorLevel
;
}
public
static
void
setQRCodeErrorLevel
(
char
errorLevel
)
{
switch
(
errorLevel
)
{
case
'H'
:
QRCodeErrorLevel
=
ErrorCorrectionLevel
.
H
;
break
;
case
'L'
:
QRCodeErrorLevel
=
ErrorCorrectionLevel
.
L
;
break
;
case
'M'
:
QRCodeErrorLevel
=
ErrorCorrectionLevel
.
M
;
break
;
case
'Q'
:
QRCodeErrorLevel
=
ErrorCorrectionLevel
.
Q
;
break
;
}
}
public
static
int
getImagePadding
()
{
return
imagePadding
;
}
public
static
void
setImagePadding
(
int
imagePadding
)
{
QRCodeUtils
.
imagePadding
=
imagePadding
;
}
public
static
int
getQRCODE_SIZE
()
{
return
QRCODE_SIZE
;
}
public
static
void
setQRCODE_SIZE
(
int
qRCODE_SIZE
)
{
QRCODE_SIZE
=
qRCODE_SIZE
;
}
public
static
String
getFORMAT_NAME
()
{
return
FORMAT_NAME
;
}
public
static
void
setFORMAT_NAME
(
String
fORMAT_NAME
)
{
FORMAT_NAME
=
fORMAT_NAME
;
}
}
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