Skip to content
切换导航条
切换导航条
当前项目
正在载入...
登录
孙克
/
smf-core
转到一个项目
切换导航栏
切换导航栏固定状态
项目
群组
代码片段
帮助
项目
活动
版本库
流水线
图表
问题
0
合并请求
0
维基
网络
创建新的问题
作业
提交
问题看板
文件
提交
网络
比较
分支
标签
Commit 4a4e77da
由
LN
编写于
2021-12-28 09:24:22 +0800
浏览文件
选项
浏览文件
标签
下载
电子邮件补丁
差异文件
元器件增加生成条码,条形码增加打印
1 个父辈
a1a331a3
显示空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
331 行增加
和
7 行删除
pom.xml
src/main/java/com/neotel/smfcore/core/barcode/rest/BarcodeController.java
src/main/java/com/neotel/smfcore/core/barcode/rest/ComponentController.java
src/main/java/com/neotel/smfcore/core/barcode/rest/bean/dto/BarcodeRuleDto.java
src/main/java/com/neotel/smfcore/core/barcode/utils/QrcodeUtils.java
pom.xml
查看文件 @
4a4e77d
...
...
@@ -210,6 +210,16 @@
<artifactId>
jcifs
</artifactId>
<version>
1.3.3
</version>
</dependency>
<dependency>
<groupId>
com.lowagie
</groupId>
<artifactId>
itext
</artifactId>
<version>
2.1.7
</version>
</dependency>
<dependency>
<groupId>
com.google.zxing
</groupId>
<artifactId>
core
</artifactId>
<version>
3.2.1
</version>
</dependency>
</dependencies>
...
...
src/main/java/com/neotel/smfcore/core/barcode/rest/BarcodeController.java
查看文件 @
4a4e77d
...
...
@@ -8,10 +8,12 @@ import com.neotel.smfcore.common.exception.ValidateException;
import
com.neotel.smfcore.common.utils.FileUtil
;
import
com.neotel.smfcore.common.utils.QueryHelp
;
import
com.neotel.smfcore.core.barcode.rest.bean.dto.BarcodeDto
;
import
com.neotel.smfcore.core.barcode.rest.bean.dto.BarcodeRuleDto
;
import
com.neotel.smfcore.core.barcode.rest.bean.mapstruct.BarcodeMapper
;
import
com.neotel.smfcore.core.barcode.rest.bean.query.BarcodeQueryCriteria
;
import
com.neotel.smfcore.core.barcode.service.manager.IBarcodeManager
;
import
com.neotel.smfcore.core.barcode.service.po.Barcode
;
import
com.neotel.smfcore.core.barcode.utils.QrcodeUtils
;
import
com.neotel.smfcore.core.device.util.DataCache
;
import
com.neotel.smfcore.core.system.service.po.Settings
;
import
com.neotel.smfcore.security.bean.FileProperties
;
...
...
@@ -30,10 +32,12 @@ import org.springframework.validation.annotation.Validated;
import
org.springframework.web.bind.annotation.*
;
import
org.springframework.web.multipart.MultipartFile
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
import
java.io.File
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Set
;
@Slf4j
...
...
@@ -48,7 +52,8 @@ public class BarcodeController {
private
final
BarcodeMapper
barcodeMapper
;
@Autowired
private
final
DataCache
dataCache
;
@Autowired
private
final
QrcodeUtils
qrcodeUtils
;
@Autowired
private
final
FileProperties
properties
;
...
...
@@ -108,27 +113,58 @@ public class BarcodeController {
@ApiOperation
(
"获取条码规则"
)
@GetMapping
(
"/rule"
)
@PreAuthorize
(
"@el.check('barcode')"
)
public
List
<
String
>
getRules
(){
return
dataCache
.
getSettings
().
getCodeRuleList
();
public
BarcodeRuleDto
getRules
()
{
Settings
settings
=
dataCache
.
getSettings
();
BarcodeRuleDto
ruleDto
=
new
BarcodeRuleDto
();
ruleDto
.
setCodeRuleList
(
settings
.
getCodeRuleList
());
ruleDto
.
setPageHeight
(
settings
.
getPageHeight
());
ruleDto
.
setPageWidth
(
settings
.
getPageWidth
());
return
ruleDto
;
}
@ApiOperation
(
"修改条码规则"
)
@PutMapping
(
"/rule"
)
@PreAuthorize
(
"@el.check('barcode')"
)
public
ResponseEntity
<
Object
>
updateRules
(
@Validated
@RequestBody
List
<
String
>
codeRules
)
{
if
(
codeRules
.
isEmpty
()){
public
ResponseEntity
<
Object
>
updateRules
(
@RequestBody
BarcodeRuleDto
params
)
{
// public ResponseEntity<Object> updateRules(@Validated @RequestBody List<String> codeRules) {
if
(
params
.
getCodeRuleList
().
isEmpty
()){
throw
new
ValidateException
(
"smfcore.valueCanotNull"
,
"{0}不能为空"
,
new
String
[]{
"codeRuleList"
}
);
}
if
(
codeRules
.
size
()>
10
){
if
(
params
.
getCodeRuleList
()
.
size
()>
10
){
throw
new
ValidateException
(
"smfcore.valueCanotNull"
,
"{0}不能为空"
,
new
String
[]{
"codeRuleList"
}
);
}
if
(
params
.
getPageHeight
()<=
0
){
throw
new
ValidateException
(
"smfcore.valueCanotNull"
,
"{0}不能为空"
,
new
String
[]{
"PageHeight"
}
);
}
if
(
params
.
getPageWidth
()<=
0
){
throw
new
ValidateException
(
"smfcore.valueCanotNull"
,
"{0}不能为空"
,
new
String
[]{
"PageWidth"
}
);
}
Settings
settings
=
dataCache
.
getSettings
();
settings
.
setCodeRuleList
(
codeRules
);
settings
.
setCodeRuleList
(
params
.
getCodeRuleList
());
settings
.
setPageHeight
(
params
.
getPageHeight
());
settings
.
setPageWidth
(
params
.
getPageWidth
());
dataCache
.
updateSettings
(
settings
);
//修改设置
return
new
ResponseEntity
<>(
HttpStatus
.
OK
);
}
@ApiOperation
(
"打印条码"
)
@PostMapping
(
value
=
"/printQrcode"
)
public
void
printQrcode
(
@RequestBody
Map
<
String
,
String
>
params
,
HttpServletResponse
response
)
{
try
{
String
barcodeStr
=
params
.
get
(
"barcode"
);
List
<
String
>
qrcodeContents
=
new
ArrayList
<
String
>();
for
(
String
bar
:
barcodeStr
.
split
(
","
))
{
qrcodeContents
.
add
(
bar
);
}
qrcodeUtils
.
writePdfToResponse
(
response
,
qrcodeContents
);
}
catch
(
Exception
e
)
{
log
.
error
(
"打印条码错误:"
+
e
.
toString
());
}
}
@ApiOperation
(
"上传条码数据"
)
@PostMapping
(
value
=
"/upload"
)
public
ResultBean
upload
(
@RequestParam
MultipartFile
uploadFile
)
throws
Exception
{
...
...
src/main/java/com/neotel/smfcore/core/barcode/rest/ComponentController.java
查看文件 @
4a4e77d
...
...
@@ -11,6 +11,7 @@ import com.neotel.smfcore.core.barcode.service.manager.IBarcodeManager;
import
com.neotel.smfcore.core.barcode.service.manager.IComponentManager
;
import
com.neotel.smfcore.core.barcode.service.po.Barcode
;
import
com.neotel.smfcore.core.barcode.service.po.Component
;
import
com.neotel.smfcore.core.barcode.utils.QrcodeUtils
;
import
com.neotel.smfcore.core.storage.service.manager.IStoragePosManager
;
import
io.swagger.annotations.Api
;
import
io.swagger.annotations.ApiOperation
;
...
...
@@ -25,9 +26,12 @@ import org.springframework.security.access.prepost.PreAuthorize;
import
org.springframework.validation.annotation.Validated
;
import
org.springframework.web.bind.annotation.*
;
import
javax.servlet.ServletOutputStream
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Set
;
@Slf4j
...
...
@@ -40,6 +44,10 @@ public class ComponentController {
private
final
IComponentManager
componentManager
;
@Autowired
private
final
ComponentMapper
componentMapper
;
@Autowired
private
final
IBarcodeManager
barcodeManager
;
@Autowired
private
final
QrcodeUtils
qrcodeUtils
;
@Autowired
private
final
IStoragePosManager
storagePosManager
;
...
...
@@ -118,4 +126,43 @@ public class ComponentController {
}
return
array
;
}
@ApiOperation
(
"生成条码"
)
@GetMapping
(
value
=
"/codeGen"
)
@PreAuthorize
(
"@el.check('component:list')"
)
public
void
codeGen
(
String
pnId
,
String
num
,
HttpServletResponse
response
)
{
try
{
// String pnId = params.get("pnId");
Component
component
=
componentManager
.
get
(
pnId
);
List
<
String
>
qrcodeContents
=
new
ArrayList
<
String
>();
if
(
component
!=
null
){
int
codeNum
=
1
;
if
(
ObjectUtil
.
isNotEmpty
(
num
)){
codeNum
=
Integer
.
valueOf
(
num
);
}
for
(
int
i
=
0
;
i
<
codeNum
;
i
++){
Barcode
barcode
=
new
Barcode
();
//先用一个不重重的,再用ID替换
String
reelId
=
System
.
currentTimeMillis
()
+
""
;
barcode
.
setBarcode
(
reelId
);
barcode
.
setPartNumber
(
component
.
getPartNumber
());
barcode
.
setInitialAmount
(
component
.
getAmount
());
barcode
.
setAmount
(
component
.
getAmount
());
barcode
=
barcodeManager
.
save
(
barcode
);
reelId
=
String
.
format
(
String
.
format
(
"%08d"
,
Long
.
valueOf
(
barcode
.
getId
())));
barcode
.
setBarcode
(
reelId
);
barcode
.
setMemo
(
component
.
getDescription
());
barcodeManager
.
save
(
barcode
);
qrcodeContents
.
add
(
reelId
);
}
}
qrcodeUtils
.
writePdfToResponse
(
response
,
qrcodeContents
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
log
.
error
(
"生成条码错误:"
+
e
.
toString
());
}
}
}
src/main/java/com/neotel/smfcore/core/barcode/rest/bean/dto/BarcodeRuleDto.java
0 → 100644
查看文件 @
4a4e77d
package
com
.
neotel
.
smfcore
.
core
.
barcode
.
rest
.
bean
.
dto
;
import
com.google.common.collect.Lists
;
import
io.swagger.annotations.ApiModelProperty
;
import
lombok.Getter
;
import
lombok.Setter
;
import
lombok.ToString
;
import
java.io.Serializable
;
import
java.util.List
;
@Getter
@Setter
@ToString
public
class
BarcodeRuleDto
implements
Serializable
{
@ApiModelProperty
(
"多条条码规则"
)
private
List
<
String
>
codeRuleList
=
Lists
.
newArrayList
();
@ApiModelProperty
(
"打印条码的纸张宽度(条码为方形,大小是长宽中最小的)"
)
private
int
pageWidth
=
500
;
@ApiModelProperty
(
"打印条码的纸张高度"
)
private
int
pageHeight
=
160
;
}
src/main/java/com/neotel/smfcore/core/barcode/utils/QrcodeUtils.java
0 → 100644
查看文件 @
4a4e77d
package
com
.
neotel
.
smfcore
.
core
.
barcode
.
utils
;
import
com.google.zxing.BarcodeFormat
;
import
com.google.zxing.EncodeHintType
;
import
com.google.zxing.MultiFormatWriter
;
import
com.google.zxing.common.BitMatrix
;
import
com.google.zxing.qrcode.decoder.ErrorCorrectionLevel
;
import
com.lowagie.text.pdf.BaseFont
;
import
com.lowagie.text.pdf.PdfPCell
;
import
com.lowagie.text.pdf.PdfPTable
;
import
com.lowagie.text.pdf.PdfWriter
;
import
com.neotel.smfcore.core.barcode.service.manager.IBarcodeManager
;
import
com.neotel.smfcore.core.barcode.service.po.Barcode
;
import
com.neotel.smfcore.core.device.util.DataCache
;
import
com.neotel.smfcore.core.system.service.po.Settings
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
javax.imageio.ImageIO
;
import
javax.servlet.ServletOutputStream
;
import
javax.servlet.http.HttpServletResponse
;
import
com.lowagie.text.*
;
import
org.springframework.stereotype.Service
;
import
java.awt.image.BufferedImage
;
import
java.io.ByteArrayOutputStream
;
import
java.io.IOException
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
@Service
@Slf4j
public
class
QrcodeUtils
{
@Autowired
private
DataCache
dataCache
;
@Autowired
private
IBarcodeManager
barcodeManager
;
/**
* 二维码图片格式
*/
private
static
String
QRCODE_FORMAT
=
"PNG"
;
/**
* 将字符串转化为二维码,写入PDF(每个二维码一页),输出到html页面
*/
public
void
writePdfToResponse
(
HttpServletResponse
response
,
List
<
String
>
qrcodeContents
)
throws
Exception
{
response
.
setContentType
(
"application/pdf;charset=UTF-8"
);
response
.
setCharacterEncoding
(
"UTF-8"
);
response
.
setHeader
(
"Content-isposition"
,
"attachment;filename=\""
+
new
String
((
"qrcode.pdf"
).
getBytes
(
"GBK"
),
"ISO-8859-1"
)
+
"\""
);
// PrintWriter writer = response.getWriter();
ServletOutputStream
outputStream
=
response
.
getOutputStream
();
Settings
settings
=
dataCache
.
getSettings
();
Rectangle
rectPageSize
=
new
Rectangle
(
settings
.
getPageWidth
(),
settings
.
getPageHeight
());
// 纸张大小
rectPageSize
.
rotate
();
//旋转纸张
int
pageSpace
=
settings
.
getPageSpace
();
Document
document
=
new
Document
(
rectPageSize
,
pageSpace
,
pageSpace
,
pageSpace
,
pageSpace
);
// 左、右、上、下间距
PdfWriter
.
getInstance
(
document
,
outputStream
);
document
.
open
();
int
codeSize
=
0
;
//水平放置文字
int
baseColumnCount
=
0
;
//宽大于高,水平放置
if
(
settings
.
getPageWidth
()
>
settings
.
getPageHeight
()){
baseColumnCount
=
2
;
codeSize
=
settings
.
getPageHeight
();
}
else
{
baseColumnCount
=
1
;
codeSize
=
settings
.
getPageWidth
();
}
codeSize
=
codeSize
-
pageSpace
*
2
;
int
fontSize
=
settings
.
getFontSize
();
int
index
=
0
;
// 生成二维码图片
for
(
String
qrcodeContent
:
qrcodeContents
)
{
PdfPTable
baseTable
=
new
PdfPTable
(
baseColumnCount
);
//baseTable.setSpacingBefore(2f);
if
(
baseColumnCount
==
2
){
int
remainSize
=
settings
.
getPageWidth
()
-
codeSize
-
pageSpace
*
2
;
float
[]
baseColumnWidths
=
{
codeSize
,
remainSize
};
baseTable
.
setWidths
(
baseColumnWidths
);
}
baseTable
.
setWidthPercentage
(
98
f
);
//baseTable.setSpacingBefore(15);
baseTable
.
getDefaultCell
().
setBorder
(
PdfPCell
.
NO_BORDER
);
byte
[]
qrcodeData
=
toQrcodeData
(
qrcodeContent
,
codeSize
);
Image
qrcodeImg
=
Image
.
getInstance
(
qrcodeData
);
qrcodeImg
.
scaleAbsolute
(
codeSize
,
codeSize
);
baseTable
.
addCell
(
qrcodeImg
);
if
(
settings
.
getPageWidth
()
!=
settings
.
getPageHeight
()){
//Font normalFont = new Font(Font.NORMAL,fontSize);
Font
boldFont
=
new
Font
(
Font
.
BOLD
,
fontSize
);
try
{
BaseFont
baseFont
=
BaseFont
.
createFont
(
"C:/WINDOWS/Fonts/simsun.ttc,1"
,
BaseFont
.
IDENTITY_H
,
BaseFont
.
NOT_EMBEDDED
);
boldFont
=
new
Font
(
baseFont
,
fontSize
);
}
catch
(
Exception
e
){
log
.
error
(
"未找到字体simsun.ttc"
);
}
PdfPTable
table
=
new
PdfPTable
(
2
);
table
.
getDefaultCell
().
setVerticalAlignment
(
Element
.
ALIGN_CENTER
);
if
(
baseColumnCount
==
2
){
table
.
getDefaultCell
().
setMinimumHeight
(
codeSize
/
4.3f
);
}
else
{
table
.
setSpacingBefore
(
15
);
}
float
[]
columnWidths
=
{
0.6f
,
1
f
};
table
.
setWidths
(
columnWidths
);
table
.
addCell
(
new
Phrase
(
"Reel Id:"
,
boldFont
));
table
.
addCell
(
new
Phrase
(
qrcodeContent
,
boldFont
));
Barcode
barcode
=
barcodeManager
.
findByBarcode
(
qrcodeContent
);
String
partNumber
=
""
;
String
qty
=
""
;
String
desc
=
""
;
if
(
barcode
!=
null
){
partNumber
=
barcode
.
getPartNumber
();
//size = barcode.getPlateSize() + " x " +barcode.getHeight();
qty
=
barcode
.
getAmount
()+
""
;
desc
=
barcode
.
getMemo
();
}
table
.
addCell
(
new
Phrase
(
"PN:"
,
boldFont
));
table
.
addCell
(
new
Phrase
(
partNumber
,
boldFont
));
table
.
addCell
(
new
Phrase
(
"QTY:"
,
boldFont
));
table
.
addCell
(
new
Phrase
(
qty
,
boldFont
));
table
.
addCell
(
new
Phrase
(
"DESC:"
,
boldFont
));
table
.
addCell
(
new
Phrase
(
desc
,
boldFont
));
// Paragraph paragraph = new Paragraph(textContent,font);
// paragraph.setAlignment(Element.ALIGN_LEFT);
//Font font = new Font(FontFactory.HELVETICA_BOLD);
//font.setSize(25F);
//paragraph.setFont(font);
//document.add(table);
baseTable
.
addCell
(
table
);
}
document
.
add
(
baseTable
);
index
=
index
+
1
;
// 不是最后一个图,新开一页
if
(
index
!=
qrcodeContents
.
size
())
{
document
.
newPage
();
}
}
// 关闭文档
document
.
close
();
}
/**
* 将字符串转为二维码图片数据
*
* @param content 二维码内容字符串
* @return 图片数据
*/
private
static
byte
[]
toQrcodeData
(
String
content
,
int
codeSize
)
throws
Exception
{
Map
<
EncodeHintType
,
Object
>
hints
=
new
HashMap
<
EncodeHintType
,
Object
>();
// 指定纠错等级
hints
.
put
(
EncodeHintType
.
ERROR_CORRECTION
,
ErrorCorrectionLevel
.
L
);
// 指定编码格式
hints
.
put
(
EncodeHintType
.
CHARACTER_SET
,
"UTF-8"
);
//设置边框
hints
.
put
(
EncodeHintType
.
MARGIN
,
0
);
int
codeWidth
=
codeSize
;
int
codeHeight
=
codeWidth
;
BitMatrix
matrix
=
new
MultiFormatWriter
().
encode
(
content
,
BarcodeFormat
.
QR_CODE
,
codeWidth
,
codeHeight
,
hints
);
BufferedImage
image
=
new
BufferedImage
(
codeWidth
,
codeHeight
,
BufferedImage
.
TYPE_INT_RGB
);
for
(
int
x
=
0
;
x
<
codeWidth
;
x
++)
{
for
(
int
y
=
0
;
y
<
codeHeight
;
y
++)
{
image
.
setRGB
(
x
,
y
,
matrix
.
get
(
x
,
y
)
?
0xFF000000
:
0xFFFFFFFF
);
}
}
//ImageIO.write(image, "png", new File("D://2.png"));
ByteArrayOutputStream
stream
=
new
ByteArrayOutputStream
();
if
(!
ImageIO
.
write
(
image
,
QRCODE_FORMAT
,
stream
))
{
throw
new
IOException
(
"Could not write an image of format "
+
QRCODE_FORMAT
);
}
byte
[]
buffs
=
stream
.
toByteArray
();
stream
.
close
();
return
buffs
;
}
}
编写
预览
支持
Markdown
格式
附加文件
你添加了
0
人
到此讨论。请谨慎行事。
Finish editing this message first!
Cancel
请
注册
或
登录
后发表评论