|
|
@@ -5,6 +5,8 @@ import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
|
|
|
import com.google.zxing.common.BitMatrix;
|
|
|
import com.google.zxing.common.HybridBinarizer;
|
|
|
import com.sixmac.common.vo.QRCodeImageBean;
|
|
|
+import com.sixmac.service.MinioUploadService;
|
|
|
+import com.sixmac.service.impl.MinioUploadServiceImpl;
|
|
|
import com.swetake.util.Qrcode;
|
|
|
import jp.sourceforge.qrcode.QRCodeDecoder;
|
|
|
import jp.sourceforge.qrcode.exception.DecodingFailedException;
|
|
|
@@ -12,6 +14,8 @@ import jp.sourceforge.qrcode.exception.DecodingFailedException;
|
|
|
import javax.imageio.ImageIO;
|
|
|
import java.awt.*;
|
|
|
import java.awt.image.BufferedImage;
|
|
|
+import java.io.ByteArrayInputStream;
|
|
|
+import java.io.ByteArrayOutputStream;
|
|
|
import java.io.File;
|
|
|
import java.io.IOException;
|
|
|
import java.util.HashMap;
|
|
|
@@ -38,13 +42,14 @@ public class QRCodeUtil {
|
|
|
* @param text 二维码内容
|
|
|
* @param width 二维码宽
|
|
|
* @param height 二维码高
|
|
|
- * @param outPutPath 二维码生成保存路径
|
|
|
+ * @param 文件名 name
|
|
|
* @param imageType 二维码生成格式
|
|
|
*/
|
|
|
- public static void zxingCodeCreate(String text, int width, int height, String outPutPath, String imageType) {
|
|
|
+ public static String zxingCodeCreate(String text, int width, int height, String name, String imageType) {
|
|
|
Map<EncodeHintType, String> his = new HashMap<EncodeHintType, String>();
|
|
|
//设置编码字符集
|
|
|
his.put(EncodeHintType.CHARACTER_SET, "utf-8");
|
|
|
+ String path="";
|
|
|
try {
|
|
|
//1、生成二维码
|
|
|
BitMatrix encode = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, his);
|
|
|
@@ -61,12 +66,14 @@ public class QRCodeUtil {
|
|
|
image.setRGB(i, j, encode.get(i, j) ? BLACK : WHITE);
|
|
|
}
|
|
|
}
|
|
|
- File outPutImage = new File(outPutPath);
|
|
|
- //如果图片不存在创建图片
|
|
|
- if (!outPutImage.exists())
|
|
|
- outPutImage.createNewFile();
|
|
|
- //5、将二维码写入图片
|
|
|
- ImageIO.write(image, imageType, outPutImage);
|
|
|
+ String imageFormat = "PNG"; // 图像格式(需与MIME类型对应)
|
|
|
+ ByteArrayInputStream imageStream = convertToInputStream(image, imageFormat);
|
|
|
+
|
|
|
+ // 3. 获取流的字节长度(用于MinIO上传)
|
|
|
+ long contentLength = imageStream.available();
|
|
|
+ MinioUploadService minioUploadService=new MinioUploadServiceImpl();
|
|
|
+ path= minioUploadService.uploadQRCodeToMinIO(imageStream,name,contentLength);
|
|
|
+
|
|
|
} catch (WriterException e) {
|
|
|
e.printStackTrace();
|
|
|
System.out.println("二维码生成失败");
|
|
|
@@ -74,6 +81,30 @@ public class QRCodeUtil {
|
|
|
e.printStackTrace();
|
|
|
System.out.println("生成二维码图片失败");
|
|
|
}
|
|
|
+ return path;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将BufferedImage转为输入流(ByteArrayInputStream)
|
|
|
+ * @param image 待转换的BufferedImage
|
|
|
+ * @param format 图像格式(如"PNG"、"JPG"、"JPEG")
|
|
|
+ * @return 转换后的输入流
|
|
|
+ * @throws IOException 流操作异常(如格式不支持)
|
|
|
+ */
|
|
|
+ public static ByteArrayInputStream convertToInputStream(BufferedImage image, String format) throws IOException {
|
|
|
+ // 1. 创建字节输出流,用于临时存储图像数据
|
|
|
+ ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
|
|
+
|
|
|
+ // 2. 将BufferedImage写入输出流(指定格式)
|
|
|
+ // 注意:format需为ImageIO支持的格式(如PNG、JPG),否则返回false且流为空
|
|
|
+ boolean isSuccess = ImageIO.write(image, format, outputStream);
|
|
|
+ if (!isSuccess) {
|
|
|
+ throw new IOException("不支持的图像格式:" + format);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 3. 将输出流转为输入流(MinIO需要InputStream)
|
|
|
+ return new ByteArrayInputStream(outputStream.toByteArray());
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -201,11 +232,11 @@ public class QRCodeUtil {
|
|
|
public static void main(String[] args) {
|
|
|
// QRCodeCreate("hah", "D:\\test//qrcode.jpg", 15, "D:\\test//icon.png");
|
|
|
// zxingCodeCreate("ServiceOrderingOrderOrderingWeekly{id=1, createDate=2017-10-17 14:24:47, updateDate=2017-10-18 09:28:12, copies=2, deleteKey=1, weeklyId=1, orderId=1, status=2}", 300, 300, "E://zxingcode.jpg", "jpg");
|
|
|
+//
|
|
|
+// String name = "code-"+String.valueOf(System.currentTimeMillis());
|
|
|
+// String uploadPath = "D:\\xuxiang\\tomcat-house\\webapps\\files\\upload";
|
|
|
+// String desFilePathName = uploadPath + name + ".jpg";
|
|
|
|
|
|
- String name = "code-"+String.valueOf(System.currentTimeMillis());
|
|
|
- String uploadPath = "D:\\xuxiang\\tomcat-house\\webapps\\files\\upload";
|
|
|
- String desFilePathName = uploadPath + name + ".jpg";
|
|
|
-
|
|
|
- zxingCodeCreate("ServiceOrderingOrderOrderingWeekly{id=1, createDate=2017-10-17 14:24:47, updateDate=2017-10-18 09:28:12, copies=2, deleteKey=1, weeklyId=1, orderId=1, status=2}", 300, 300, "D:\\xuxiang\\tomcat-house\\webapps\\files\\upload\\a.jpg", "jpg");
|
|
|
+ zxingCodeCreate("ServiceOrderingOrderOrderingWeekly{id=1, createDate=2017-10-17 14:24:47, updateDate=2017-10-18 09:28:12, copies=2, deleteKey=1, weeklyId=1, orderId=1, status=2}", 300, 300, "TTEESSTT.PNG", "PNG");
|
|
|
}
|
|
|
}
|