设备激活二维码打印相关接口
This commit is contained in:
parent
5e76aa6305
commit
679b651706
@ -134,6 +134,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
||||
.antMatchers("/prod-api/**").permitAll()
|
||||
.antMatchers("/system/district/tree").permitAll()
|
||||
.antMatchers("/sse/**").permitAll()
|
||||
.antMatchers("/common/upload").permitAll()
|
||||
// 除上面外的所有请求全部需要鉴权认证
|
||||
.anyRequest().authenticated()
|
||||
|
||||
|
@ -1,16 +1,24 @@
|
||||
package com.fastbee.data.controller.printer;
|
||||
|
||||
import com.fastbee.common.core.controller.BaseController;
|
||||
import com.fastbee.common.core.domain.AjaxResult;
|
||||
import com.fastbee.common.core.domain.AjaxResultPro;
|
||||
import com.fastbee.common.exception.ServiceException;
|
||||
import com.fastbee.data.controller.printer.yilianyun.GenerateQRCodeImage;
|
||||
import com.fastbee.data.controller.printer.yilianyun.YiLianYunPrintService;
|
||||
import com.fastbee.iot.domain.DeviceReportInfo;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/printer")
|
||||
public class PrinterController {
|
||||
public class PrinterController extends BaseController {
|
||||
@Autowired
|
||||
private YiLianYunPrintService printerService;
|
||||
|
||||
@ -18,16 +26,35 @@ public class PrinterController {
|
||||
* 文本内容打印
|
||||
*/
|
||||
@PostMapping("/text/do")
|
||||
public AjaxResultPro textPrint(@RequestBody String content){
|
||||
public AjaxResult textPrint(@RequestBody String content){
|
||||
printerService.textPrint(content);
|
||||
return AjaxResultPro.success();
|
||||
return AjaxResult.success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 图片打印
|
||||
*/
|
||||
@PostMapping("/image/do")
|
||||
public AjaxResultPro imagePrint(@RequestBody String imageUrl){
|
||||
public AjaxResult imagePrint(@RequestBody String imageUrl,@Param("printNum") Integer printNum) throws InterruptedException {
|
||||
if(Objects.isNull(printNum)){
|
||||
throw new ServiceException("打印次数不能为空!");
|
||||
}
|
||||
if(printNum>2){
|
||||
throw new ServiceException("打印次数不能超过2!");
|
||||
}
|
||||
for (int i = 0; i < printNum; i++) {
|
||||
printerService.imagePrint(imageUrl);
|
||||
return AjaxResultPro.success();
|
||||
Thread.sleep(200);
|
||||
}
|
||||
return AjaxResult.success();
|
||||
}
|
||||
/**
|
||||
* 生成待打印图片
|
||||
*/
|
||||
@PostMapping("/image/msg/generate")
|
||||
public AjaxResult generateImage(@RequestBody DeviceReportInfo deviceReportInfo ){
|
||||
System.err.println(deviceReportInfo);
|
||||
String qrCodeUrl = GenerateQRCodeImage.generateQRCodeUrl(deviceReportInfo.getName(), deviceReportInfo.getSerialNumber(), "山东翰臻物联公司");
|
||||
return AjaxResult.success("操作成功",qrCodeUrl);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,133 @@
|
||||
package com.fastbee.data.controller.printer.yilianyun;
|
||||
|
||||
import cn.hutool.http.HttpRequest;
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.fastbee.common.exception.ServiceException;
|
||||
import com.google.zxing.BarcodeFormat;
|
||||
import com.google.zxing.EncodeHintType;
|
||||
import com.google.zxing.MultiFormatWriter;
|
||||
import com.google.zxing.WriterException;
|
||||
import com.google.zxing.client.j2se.MatrixToImageWriter;
|
||||
import com.google.zxing.common.BitMatrix;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class GenerateQRCodeImage {
|
||||
|
||||
//设备激活接口地址
|
||||
private static final String ACTIVATE_URL = "https://open.10ss.net:8443/api/activate";
|
||||
|
||||
public static String generateQRCodeUrl(String deviceName, String deviceCode, String companyName){
|
||||
try {
|
||||
// 二维码内容
|
||||
String qrCodeContent = ACTIVATE_URL+"?deviceName="+deviceName+"&deviceCode="+deviceCode;
|
||||
|
||||
// 生成二维码
|
||||
BitMatrix bitMatrix = generateQRCode(qrCodeContent, 220, 220);
|
||||
|
||||
// 将二维码转换为BufferedImage
|
||||
BufferedImage qrCodeImage = MatrixToImageWriter.toBufferedImage(bitMatrix);
|
||||
|
||||
// 创建一个新的BufferedImage,包含二维码和文字
|
||||
int qrCodeWidth = qrCodeImage.getWidth();
|
||||
int qrCodeHeight = qrCodeImage.getHeight();
|
||||
int textWidth = 200; // 文字区域宽度
|
||||
int imageWidth = qrCodeWidth + textWidth + 30; // 间距20像素
|
||||
int imageHeight = qrCodeHeight;
|
||||
|
||||
BufferedImage finalImage = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_ARGB);
|
||||
Graphics2D g2d = finalImage.createGraphics();
|
||||
|
||||
// 绘制背景颜色
|
||||
g2d.setColor(Color.WHITE);
|
||||
g2d.fillRect(0, 0, imageWidth, imageHeight);
|
||||
|
||||
// 绘制二维码
|
||||
g2d.drawImage(qrCodeImage, 30, 0, qrCodeWidth, qrCodeHeight, null);
|
||||
|
||||
// 绘制文字
|
||||
g2d.setColor(Color.BLACK);
|
||||
g2d.setFont(new Font("黑体", Font.BOLD, 18));
|
||||
String text1 = "设备:"+deviceName;
|
||||
FontMetrics fontMetrics = g2d.getFontMetrics();
|
||||
int textX = qrCodeWidth+5; // 间距20像素
|
||||
int textY1 = (imageHeight - fontMetrics.getHeight()) / 2 + fontMetrics.getAscent()-40;
|
||||
g2d.drawString(text1, textX, textY1);
|
||||
|
||||
// 绘制第二段文字
|
||||
String text2 = "编码:"+deviceCode;
|
||||
int textY2 = textY1 + fontMetrics.getHeight() + 20; // 在第一段文字下方添加一些间距
|
||||
g2d.drawString(text2, textX, textY2);
|
||||
// 绘制第三段文字
|
||||
String text3 = ""+companyName;
|
||||
int textY3 = textY2 + fontMetrics.getHeight() + 20; // 在第一段文字下方添加一些间距
|
||||
g2d.drawString(text3, textX, textY3);
|
||||
// 释放Graphics2D资源
|
||||
g2d.dispose();
|
||||
|
||||
// 保存图片到临时文件
|
||||
Path tempFilePath = Files.createTempFile("qrCode", ".png");
|
||||
File outputFile = tempFilePath.toFile();
|
||||
ImageIO.write(finalImage, "png", outputFile);
|
||||
|
||||
// 上传图片到服务器
|
||||
String uploadUrl = "http://101.201.244.214:13088/common/upload";
|
||||
JSONObject uploaded = uploadImage(uploadUrl, outputFile);
|
||||
String url = uploaded.getStr("url");
|
||||
|
||||
// 删除临时文件
|
||||
Files.delete(tempFilePath);
|
||||
// System.out.println("二维码和文字图片已生成:" + outputFile.getAbsolutePath());
|
||||
return url;
|
||||
} catch (WriterException | IOException e) {
|
||||
e.printStackTrace();
|
||||
throw new RuntimeException("设备激活二维码图片生成失败!");
|
||||
}
|
||||
}
|
||||
|
||||
private static BitMatrix generateQRCode(String text, int width, int height) throws WriterException {
|
||||
Map<EncodeHintType, Object> hints = new HashMap<>();
|
||||
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
|
||||
return new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, hints);
|
||||
}
|
||||
|
||||
private static JSONObject uploadImage(String uploadUrl, File file) throws IOException {
|
||||
// 创建一个表单请求
|
||||
HttpRequest request = HttpUtil.createPost(uploadUrl);
|
||||
// 添加文件参数
|
||||
request.form("file", getFileBytes(file), file.getName());
|
||||
// 发送请求
|
||||
String response = request.execute().body();
|
||||
// System.out.println("服务器响应:" + response);
|
||||
JSONObject resp = JSONUtil.parseObj(response);
|
||||
if(!resp.getStr("code").equals("200")){
|
||||
throw new RuntimeException("设备激活二维码上传失败!");
|
||||
}
|
||||
return resp;
|
||||
}
|
||||
/**
|
||||
* 获取文件的字节数组
|
||||
*/
|
||||
public static byte[] getFileBytes(File file) throws IOException {
|
||||
if (!file.exists() || file.isDirectory()) {
|
||||
throw new IllegalArgumentException("输入的不是有效的文件");
|
||||
}
|
||||
return Files.readAllBytes(Paths.get(file.getAbsolutePath()));
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
String url = GenerateQRCodeImage.generateQRCodeUrl("测试水电双计设备001","FD7894455454545","山东翰臻物联公司");
|
||||
System.err.println(url);
|
||||
}
|
||||
}
|
@ -11,7 +11,9 @@ import java.util.Map;
|
||||
public class YiLianYunPrintService {
|
||||
@Autowired
|
||||
private YiLianYunAuthorizationService authorizationService;
|
||||
private String endpoint = "https://open-api.10ss.net/v2/print/index";
|
||||
private final String endpoint_text = "https://open-api.10ss.net/v2/print/index";
|
||||
private final String endpoint_image = "https://open-api.10ss.net/v2/pictureprint/index";
|
||||
|
||||
|
||||
/**
|
||||
* 文本打印
|
||||
@ -22,17 +24,19 @@ public class YiLianYunPrintService {
|
||||
//构建请求参数
|
||||
// 创建一个 HashMap 来存储属性
|
||||
Map<String, Object> attributes = new HashMap<>();
|
||||
String accessToken = authorizationService.getAccessToken();
|
||||
System.err.println("accessToken:"+accessToken);
|
||||
// 添加属性到 HashMap
|
||||
Long timestamp = System.currentTimeMillis();
|
||||
attributes.put("client_id", YiLianYunUtil.clientId);
|
||||
attributes.put("sign", YiLianYunUtil.getSign(timestamp));
|
||||
attributes.put("timestamp", timestamp);
|
||||
attributes.put("id", "550e8400-e29b-41d4-a716-446655440000");
|
||||
attributes.put("access_token", authorizationService.getAccessToken());
|
||||
attributes.put("access_token", accessToken);
|
||||
attributes.put("machine_code", "4004899747");
|
||||
attributes.put("origin_id", "123");
|
||||
attributes.put("content", content);
|
||||
JSONObject resp = yiLianYunBaseService.baseRequest(endpoint, attributes);
|
||||
JSONObject resp = yiLianYunBaseService.baseRequest(endpoint_text, attributes);
|
||||
}
|
||||
/**
|
||||
* 图片打印
|
||||
@ -42,16 +46,18 @@ public class YiLianYunPrintService {
|
||||
//构建请求参数
|
||||
// 创建一个 HashMap 来存储属性
|
||||
Map<String, Object> attributes = new HashMap<>();
|
||||
String accessToken = authorizationService.getAccessToken();
|
||||
System.err.println("accessToken:"+accessToken);
|
||||
// 添加属性到 HashMap
|
||||
Long timestamp = System.currentTimeMillis();
|
||||
attributes.put("client_id", YiLianYunUtil.clientId);
|
||||
attributes.put("sign", YiLianYunUtil.getSign(timestamp));
|
||||
attributes.put("timestamp", timestamp);
|
||||
attributes.put("id", "550e8400-e29b-41d4-a716-446655440000");
|
||||
attributes.put("access_token", authorizationService.getAccessToken());
|
||||
attributes.put("access_token", accessToken);
|
||||
attributes.put("machine_code", "4004899747");
|
||||
attributes.put("origin_id", "123");
|
||||
attributes.put("picture_url", pictureUrl);
|
||||
JSONObject resp = yiLianYunBaseService.baseRequest(endpoint, attributes);
|
||||
JSONObject resp = yiLianYunBaseService.baseRequest(endpoint_image, attributes);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user