Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
datax-cloud
Overview
Overview
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
黄营
datax-cloud
Commits
2d1d32a3
Commit
2d1d32a3
authored
May 03, 2020
by
yuwei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
2.0.0项目初始化
parent
f2afa283
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
268 additions
and
1 deletions
+268
-1
pom.xml
datax-common/datax-common-core/pom.xml
+1
-1
pom.xml
datax-common/datax-common-qrcode/pom.xml
+27
-0
QrCodeUtil.java
...code/src/main/java/cn/datax/common/qrcode/QrCodeUtil.java
+237
-0
pom.xml
datax-common/pom.xml
+2
-0
pom.xml
pom.xml
+1
-0
No files found.
datax-common/datax-common-core/pom.xml
View file @
2d1d32a3
...
...
@@ -15,7 +15,7 @@
<!-- SpringWeb模块 -->
<dependency>
<groupId>
org.springframework
</groupId>
<artifactId>
spring-web
</artifactId>
<artifactId>
spring-web
mvc
</artifactId>
<scope>
provided
</scope>
</dependency>
<!--io常用工具类 -->
...
...
datax-common/datax-common-qrcode/pom.xml
0 → 100644
View file @
2d1d32a3
<?xml version="1.0" encoding="UTF-8"?>
<project
xmlns=
"http://maven.apache.org/POM/4.0.0"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
>
<parent>
<artifactId>
datax-common
</artifactId>
<groupId>
cn.datax
</groupId>
<version>
2.0.0
</version>
</parent>
<modelVersion>
4.0.0
</modelVersion>
<version>
2.0.0
</version>
<artifactId>
datax-common-qrcode
</artifactId>
<dependencies>
<dependency>
<groupId>
com.google.zxing
</groupId>
<artifactId>
core
</artifactId>
<version>
${zxing.version}
</version>
</dependency>
<dependency>
<groupId>
com.google.zxing
</groupId>
<artifactId>
javase
</artifactId>
<version>
${zxing.version}
</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
datax-common/datax-common-qrcode/src/main/java/cn/datax/common/qrcode/QrCodeUtil.java
0 → 100644
View file @
2d1d32a3
package
cn
.
datax
.
common
.
qrcode
;
import
java.awt.*
;
import
java.awt.geom.RoundRectangle2D
;
import
java.awt.image.BufferedImage
;
import
java.io.*
;
import
java.util.HashMap
;
import
java.util.Map
;
import
javax.imageio.ImageIO
;
import
com.google.zxing.BarcodeFormat
;
import
com.google.zxing.BinaryBitmap
;
import
com.google.zxing.DecodeHintType
;
import
com.google.zxing.EncodeHintType
;
import
com.google.zxing.MultiFormatReader
;
import
com.google.zxing.MultiFormatWriter
;
import
com.google.zxing.NotFoundException
;
import
com.google.zxing.Result
;
import
com.google.zxing.WriterException
;
import
com.google.zxing.client.j2se.BufferedImageLuminanceSource
;
import
com.google.zxing.client.j2se.MatrixToImageConfig
;
import
com.google.zxing.client.j2se.MatrixToImageWriter
;
import
com.google.zxing.common.BitMatrix
;
import
com.google.zxing.common.HybridBinarizer
;
import
com.google.zxing.qrcode.decoder.ErrorCorrectionLevel
;
import
lombok.extern.slf4j.Slf4j
;
/**
* 二维码 工具类
*/
@Slf4j
public
class
QrCodeUtil
{
/**
* 生成带图片的二维码(到destImagePath指向的File)
*
* @param content 二维码的内容
* @param width 二维码的宽度(px)
* @param height 二维码的高度(px)
* @param embeddedImgPath 被镶嵌的图片的地址(null表示不带logo图片)
* @param destImagePath 生成二维码图片的地址
*
* @return 生成的二维码文件path
* @throws IOException IOException
* @throws WriterException WriterException
*/
public
static
String
QREncode
(
String
content
,
int
width
,
int
height
,
String
embeddedImgPath
,
String
destImagePath
)
throws
IOException
,
WriterException
{
File
dest
=
getFile
(
destImagePath
);
// 图像类型
String
format
=
"jpg"
;
Map
<
EncodeHintType
,
Object
>
hints
=
new
HashMap
<>(
4
);
//设置UTF-8, 防止中文乱码
hints
.
put
(
EncodeHintType
.
CHARACTER_SET
,
"UTF-8"
);
//设置二维码四周白色区域的大小
hints
.
put
(
EncodeHintType
.
MARGIN
,
1
);
//设置二维码的容错性
hints
.
put
(
EncodeHintType
.
ERROR_CORRECTION
,
ErrorCorrectionLevel
.
H
);
//画二维码,记得调用multiFormatWriter.encode()时最后要带上hints参数,不然上面设置无效
BitMatrix
bitMatrix
=
new
MultiFormatWriter
().
encode
(
content
,
BarcodeFormat
.
QR_CODE
,
width
,
height
,
hints
);
//开始画二维码
MatrixToImageWriter
.
writeToPath
(
bitMatrix
,
format
,
dest
.
toPath
());
if
(
null
!=
embeddedImgPath
){
MatrixToImageConfig
matrixToImageConfig
=
new
MatrixToImageConfig
(
0xFF000001
,
0xFFFFFFFF
);
BufferedImage
bufferedImage
=
LogoMatrix
(
MatrixToImageWriter
.
toBufferedImage
(
bitMatrix
,
matrixToImageConfig
),
embeddedImgPath
);
//输出带logo图片
ImageIO
.
write
(
bufferedImage
,
format
,
dest
);
}
log
.
info
(
"generate Qr code file {}"
,
destImagePath
);
return
destImagePath
;
}
/**
* 生成带图片的二维码(到outputStream流)
*
* @param content 二维码的内容
* @param width 二维码的宽度(px)
* @param height 二维码的高度(px)
* @param embeddedImgPath 被镶嵌的图片的地址(null表示不带logo图片)
* @param outputStream 生成二维码图片的地址
*
* @return 生成的二维码文件path
* @throws IOException IOException
* @throws WriterException WriterException
*/
public
static
void
QREncode
(
String
content
,
int
width
,
int
height
,
String
embeddedImgPath
,
OutputStream
outputStream
)
throws
IOException
,
WriterException
{
// 图像类型
String
format
=
"jpg"
;
Map
<
EncodeHintType
,
Object
>
hints
=
new
HashMap
<>(
4
);
//设置UTF-8, 防止中文乱码
hints
.
put
(
EncodeHintType
.
CHARACTER_SET
,
"UTF-8"
);
//设置二维码四周白色区域的大小
hints
.
put
(
EncodeHintType
.
MARGIN
,
1
);
//设置二维码的容错性
hints
.
put
(
EncodeHintType
.
ERROR_CORRECTION
,
ErrorCorrectionLevel
.
H
);
//画二维码,记得调用multiFormatWriter.encode()时最后要带上hints参数,不然上面设置无效
BitMatrix
bitMatrix
=
new
MultiFormatWriter
().
encode
(
content
,
BarcodeFormat
.
QR_CODE
,
width
,
height
,
hints
);
//开始画二维码
MatrixToImageConfig
matrixToImageConfig
=
new
MatrixToImageConfig
(
0xFF000001
,
0xFFFFFFFF
);
BufferedImage
bufferedImage
=
MatrixToImageWriter
.
toBufferedImage
(
bitMatrix
,
matrixToImageConfig
);
if
(
null
!=
embeddedImgPath
){
bufferedImage
=
LogoMatrix
(
bufferedImage
,
embeddedImgPath
);
}
//输出带logo图片
boolean
result
=
ImageIO
.
write
(
bufferedImage
,
format
,
outputStream
);
log
.
info
(
"generate Qr code file to OutputStream {}"
,
result
?
"success"
:
"fail"
);
}
/**
* 识别二维码内容信息
*
* @param file 二维码图片文件
*
* @return 二维码内容
* @throws NotFoundException NotFoundException
* @throws IOException IOException
*/
public
static
String
QRReader
(
File
file
)
throws
NotFoundException
,
IOException
{
BufferedImage
bufferedImage
;
bufferedImage
=
ImageIO
.
read
(
file
);
if
(
bufferedImage
==
null
)
{
return
null
;
}
String
data
=
decodeQrCode
(
bufferedImage
);
bufferedImage
.
flush
();
log
.
info
(
"Qr code from [{}] data is -> {}"
,
file
.
getAbsolutePath
(),
data
);
return
data
;
}
/**
* 识别二维码内容信息
*
* @param is 二维码图片文件流
*
* @return 二维码内容
* @throws NotFoundException NotFoundException
* @throws IOException IOException
*/
public
static
String
QRReader
(
InputStream
is
)
throws
NotFoundException
,
IOException
{
BufferedImage
bufferedImage
;
bufferedImage
=
ImageIO
.
read
(
is
);
if
(
bufferedImage
==
null
)
{
return
null
;
}
String
data
=
decodeQrCode
(
bufferedImage
);
bufferedImage
.
flush
();
log
.
info
(
"Qr code from InputStream data is -> {}"
,
data
);
return
data
;
}
// ---------------------------------------------------以下为辅助方法、辅助类-----------------------------------------
/**
* 获取文件(顺带创建文件夹,如果需要的话)
*
* @param filePath 文件path
* @return 文件对象
*/
private
static
File
getFile
(
String
filePath
)
{
File
file
=
new
File
(
filePath
);
if
(!
file
.
getParentFile
().
exists
())
{
boolean
result
=
file
.
getParentFile
().
mkdirs
();
log
.
info
(
"create directory {} {}"
,
file
.
getParent
(),
result
);
}
return
file
;
}
/**
* 二维码添加logo
*
* @param matrixImage 源二维码图片
* @param embeddedImgPath logo图片地址
* @return 返回带有logo的二维码图片
*/
public
static
BufferedImage
LogoMatrix
(
BufferedImage
matrixImage
,
String
embeddedImgPath
)
throws
IOException
{
/**
* 读取二维码图片,并构建绘图对象
*/
Graphics2D
g2
=
matrixImage
.
createGraphics
();
int
matrixWidth
=
matrixImage
.
getWidth
();
int
matrixHeigh
=
matrixImage
.
getHeight
();
/**
* 读取Logo图片
*/
File
logoFile
=
getFile
(
embeddedImgPath
);
BufferedImage
logo
=
ImageIO
.
read
(
logoFile
);
// 开始绘制图片
g2
.
drawImage
(
logo
,
matrixWidth
/
5
*
2
,
matrixHeigh
/
5
*
2
,
matrixWidth
/
5
,
matrixHeigh
/
5
,
null
);
BasicStroke
stroke
=
new
BasicStroke
(
5
,
BasicStroke
.
CAP_ROUND
,
BasicStroke
.
JOIN_ROUND
);
// 设置笔画对象
g2
.
setStroke
(
stroke
);
// 指定弧度的圆角矩形
RoundRectangle2D
.
Float
round
=
new
RoundRectangle2D
.
Float
(
matrixWidth
/
5
*
2
,
matrixHeigh
/
5
*
2
,
matrixWidth
/
5
,
matrixHeigh
/
5
,
20
,
20
);
g2
.
setColor
(
Color
.
white
);
// 绘制圆弧矩形
g2
.
draw
(
round
);
// 设置logo 有一道灰色边框
BasicStroke
stroke2
=
new
BasicStroke
(
1
,
BasicStroke
.
CAP_ROUND
,
BasicStroke
.
JOIN_ROUND
);
// 设置笔画对象
g2
.
setStroke
(
stroke2
);
RoundRectangle2D
.
Float
round2
=
new
RoundRectangle2D
.
Float
(
matrixWidth
/
5
*
2
+
2
,
matrixHeigh
/
5
*
2
+
2
,
matrixWidth
/
5
-
4
,
matrixHeigh
/
5
-
4
,
20
,
20
);
g2
.
setColor
(
new
Color
(
128
,
128
,
128
));
// 绘制圆弧矩形
g2
.
draw
(
round2
);
g2
.
dispose
();
matrixImage
.
flush
();
return
matrixImage
;
}
/**
* 识别二维码内容信息
*二维码图片信息BufferedImage
* @param image
*
* @return 二维码内容
* @throws NotFoundException NotFoundException
*/
private
static
String
decodeQrCode
(
BufferedImage
image
)
throws
NotFoundException
{
BufferedImageLuminanceSource
source
=
new
BufferedImageLuminanceSource
(
image
);
BinaryBitmap
bitmap
=
new
BinaryBitmap
(
new
HybridBinarizer
(
source
));
HashMap
<
DecodeHintType
,
Object
>
hints
=
new
HashMap
<>(
4
);
hints
.
put
(
DecodeHintType
.
CHARACTER_SET
,
"UTF-8"
);
Result
result
=
new
MultiFormatReader
().
decode
(
bitmap
,
hints
);
return
result
.
getText
();
}
public
static
void
main
(
String
[]
args
)
throws
IOException
,
WriterException
,
NotFoundException
{
// 生成二维码
// QrCodeUtil.QREncode("java开发", 500, 500, null, "F://qrcode2//二维码.jpg");
// // 生成带图片的二维码
// QrCodeUtil.QREncode("java开发", 500, 500, "F://qrcode2//1.jpg", "F://qrcode2//带图片的二维码.jpg");
// // 识别二维码
// QrCodeUtil.QRReader(new File("F://qrcode2//二维码.jpg"));
// QrCodeUtil.decodeQrCode(new FileInputStream("F://qrcode//带文字带图片的二维码.jpg"));
}
}
datax-common/pom.xml
View file @
2d1d32a3
...
...
@@ -21,5 +21,6 @@
<module>
datax-common-database
</module>
<module>
datax-common-office
</module>
<module>
datax-common-dictionary
</module>
<module>
datax-common-qrcode
</module>
</modules>
</project>
\ No newline at end of file
pom.xml
View file @
2d1d32a3
...
...
@@ -46,6 +46,7 @@
<oracle.version>
19.3.0.0
</oracle.version>
<postgresql.version>
42.2.11
</postgresql.version>
<sqlserver.version>
8.2.1.jre8
</sqlserver.version>
<zxing.version>
3.4.0
</zxing.version>
<aspose.version>
20.3
</aspose.version>
</properties>
...
...
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