Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
D
device-back
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
1
议题
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
Matrix
device-back
Commits
e8964dbe
提交
e8964dbe
authored
7月 20, 2021
作者:
zhoushaopan
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[二维码]生成二维码
上级
e674b9e9
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
180 行增加
和
0 行删除
+180
-0
ZXingUtil.java
...c/main/java/com/tykj/dev/device/zxing/util/ZXingUtil.java
+180
-0
没有找到文件。
dev-zxing/src/main/java/com/tykj/dev/device/zxing/util/ZXingUtil.java
0 → 100644
浏览文件 @
e8964dbe
package
com
.
tykj
.
dev
.
device
.
zxing
.
util
;
import
com.google.zxing.*
;
import
com.google.zxing.client.j2se.BufferedImageLuminanceSource
;
import
com.google.zxing.client.j2se.MatrixToImageWriter
;
import
com.google.zxing.common.BitMatrix
;
import
com.google.zxing.common.HybridBinarizer
;
import
javassist.NotFoundException
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.web.multipart.MultipartFile
;
import
org.springframework.web.multipart.MultipartRequest
;
import
sun.misc.BASE64Encoder
;
import
javax.imageio.ImageIO
;
import
javax.servlet.http.HttpServletRequest
;
import
java.awt.image.BufferedImage
;
import
java.io.*
;
import
java.nio.file.FileSystems
;
import
java.nio.file.Path
;
import
java.util.HashMap
;
import
java.util.Map
;
import
java.util.UUID
;
/**
* DATE:2021-7-20
* Author:zsp
*/
public
class
ZXingUtil
{
/** 二维码上传位置 */
private
static
String
codePath
=
"src/"
;
/** 二维码宽度 */
private
static
Integer
width
=
50
;
/** 二维码高度 */
private
static
Integer
height
=
50
;
/**
* 解析二维码
*
* @param name 文件名
* @return 二维码内容
* @throws IOException
* @throws NotFoundException
*/
// public ResponseEntity upload(HttpServletRequest request) {
// MultipartRequest multiRequest = (MultipartRequest) request;
// MultipartFile file = multiRequest.getFile("file");
// return ResponseEntity.ok(fileUpload(file));
// }
// private void fileUpload(MultipartFile file) {
// String originalFilename = file.getOriginalFilename();
// File file1 = new File(url);
// if (!file1.exists()) {
// file1.mkdirs();
// }
// FileOutputStream fos = null;
// InputStream inputStream = null;
// OutputStream os = null;
//
// String[] split = originalFilename.split("\\.");
// String newName = UUID.randomUUID().toString().replace("-", "") + "." + split[split.length - 1];
// byte[] typeByte = new byte[1024 * 1024 * 10];
// try {
// inputStream = file.getInputStream();
// fos = new FileOutputStream(url + newName);
// int i = 0;
// while ((i = inputStream.read(typeByte)) != -1) {
// fos.write(typeByte, 0, i);
// }
// fos.flush();
// } catch (IOException e) {
// e.printStackTrace();
// }
// if (inputStream != null) {
// try {
// inputStream.close();
// } catch (IOException e) {
// e.printStackTrace();
// } finally {
// inputStream = null;
// }
// }
//
// if (fos != null) {
// try {
// fos.close();
// } catch (IOException e) {
// e.printStackTrace();
// } finally {
// fos = null;
// }
// }
//
// if (os != null) {
// try {
// os.close();
// } catch (IOException e) {
// e.printStackTrace();
// } finally {
// os = null;
// }
// }
//
// return new FileRet(originalFilename, url + newName, preview + newName);
// }
// public static String decode(String name) throws IOException, com.google.zxing.NotFoundException {
// String filepath = codePath + name + ".png";
// BufferedImage bufferedImage = ImageIO.read(new FileInputStream(filepath));
// LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);
// Binarizer binarizer = new HybridBinarizer(source);
// BinaryBitmap bitmap = new BinaryBitmap(binarizer);
// HashMap<DecodeHintType, Object> decodeHints = new HashMap<DecodeHintType, Object>();
// decodeHints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
// Result result = new MultiFormatReader().decode(bitmap, decodeHints);
// return result.getText();
// }
// public static String decode() throws IOException, com.google.zxing.NotFoundException {
// String filepath = codePath + name + ".png";
// BufferedImage bufferedImage = ImageIO.read(new FileInputStream(filepath));
// LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);
// Binarizer binarizer = new HybridBinarizer(source);
// BinaryBitmap bitmap = new BinaryBitmap(binarizer);
// HashMap<DecodeHintType, Object> decodeHints = new HashMap<DecodeHintType, Object>();
// decodeHints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
// Result result = new MultiFormatReader().decode(bitmap, decodeHints);
// return result.getText();
// }
/**
* 生成二维码
*
* @param taskId 任务id
* @return 二维码路径
* @throws WriterException
* @throws IOException
*/
public
static
String
encode
(
Integer
taskId
)
throws
WriterException
,
IOException
{
Map
<
EncodeHintType
,
Object
>
encodeHints
=
new
HashMap
<
EncodeHintType
,
Object
>();
encodeHints
.
put
(
EncodeHintType
.
CHARACTER_SET
,
"UTF-8"
);
BitMatrix
bitMatrix
=
new
MultiFormatWriter
().
encode
(
String
.
valueOf
(
taskId
),
BarcodeFormat
.
QR_CODE
,
width
,
height
,
encodeHints
);
String
filepath
=
codePath
+
UUID
.
randomUUID
()
+
".png"
;
Path
path
=
FileSystems
.
getDefault
().
getPath
(
filepath
);
MatrixToImageWriter
.
writeToPath
(
bitMatrix
,
"png"
,
path
);
return
ImageToBase64ByLocal
(
filepath
);
}
/**
* 本地图片转换成base64字符串
* @param imgFile 图片本地路径
* @return
*
* @author ZHANGJL
* @dateTime 2018-02-23 14:40:46
*/
public
static
String
ImageToBase64ByLocal
(
String
imgFile
)
{
// 将图片文件转化为字节数组字符串,并对其进行Base64编码处理
InputStream
in
=
null
;
byte
[]
data
=
null
;
// 读取图片字节数组
try
{
in
=
new
FileInputStream
(
imgFile
);
data
=
new
byte
[
in
.
available
()];
in
.
read
(
data
);
in
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
// 对字节数组Base64编码
BASE64Encoder
encoder
=
new
BASE64Encoder
();
return
encoder
.
encode
(
data
);
// 返回Base64编码过的字节数组字符串
}
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论