提交 e8964dbe authored 作者: zhoushaopan's avatar zhoushaopan

[二维码]生成二维码

上级 e674b9e9
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 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论