Commit 85df2882 authored by wdy's avatar wdy

Merge branch 'wangdingyi' into 'dev'

车型试验原始记录转为图片

See merge request !371
parents a01bd1ee 20424dcd
......@@ -178,6 +178,9 @@ public class Task {
@ApiModelProperty("子任务指针最大值")
private int maxPointer;
@ApiModelProperty("图片地址")
private String pictureUrl;
@ApiModelProperty("检验项目名称")
@TableField(exist = false)
private String inspectionItemName;
......
......@@ -68,4 +68,6 @@ public interface TaskService extends IService<Task> {
DataStatisticsResponse dataStatistics(TaskGetInfoRequest request);
String createProject(String request) throws IOException;
String getPicture(TaskPictureRequest request) throws IOException;
}
......@@ -23,13 +23,26 @@ import com.ruoyi.service.*;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.web.request.*;
import com.ruoyi.web.response.*;
import io.minio.MinioClient;
import io.minio.ObjectWriteArgs;
import io.minio.PutObjectArgs;
import okhttp3.*;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
......@@ -38,6 +51,18 @@ import java.util.stream.Collectors;
@Service
public class TaskServiceImpl extends ServiceImpl<TaskMapper, Task>implements TaskService {
@Value("${minio.bucketName}")
private String bucketName;
@Value("${minio.url}")
private String minioEndpoint;
@Value("${minio.accessKey}")
private String accessKey;
@Value("${minio.secretKey}")
private String secretKey;
@Autowired
private TaskMapper taskMapper;
......@@ -819,6 +844,98 @@ public class TaskServiceImpl extends ServiceImpl<TaskMapper, Task>implements Tas
return testSchemeId;
}
@Override
public String getPicture(TaskPictureRequest request) throws IOException {
Task task = taskMapper.selectById(request.getTaskId());
if (task.getPictureUrl() != null) {
return task.getPictureUrl();
} else {
String pictureUrl = processRemotePDF(request.getUrl());
task.setPictureUrl(pictureUrl);
taskMapper.updateById(task);
return pictureUrl;
}
}
public String processRemotePDF(String urlStr) throws IOException {
URL url = new URL(urlStr);
String pictureUrl = "";
// 打开远程连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
// 获取输入流
try (InputStream inputStream = connection.getInputStream()) {
// 加载PDF文档
PDDocument doc = PDDocument.load(inputStream);
PDFRenderer renderer = new PDFRenderer(doc);
// 遍历每页pdf
for (int i = 0; i < doc.getNumberOfPages(); i++) {
// dpi调到300左右即可,太小会模糊,太大会使图片变得很大
BufferedImage image = renderer.renderImageWithDPI(i, 300);
// 将 BufferedImage 转换为 ByteArrayOutputStream
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ImageIO.write(image, "jpg", outputStream);
pictureUrl += uploadMinio(outputStream,"车型审查原始记录_" + i + getReportName() + ".jpg") + ",";
}
// 关闭文档
doc.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭连接
connection.disconnect();
}
if (pictureUrl.endsWith(",")) {
pictureUrl = pictureUrl.substring(0, pictureUrl.length() - 1);
}
return pictureUrl;
}
/**
* 上传到minio
* @param outputStream 输出流
* @param fileName 文件名
* @return minio存储地址
*/
private String uploadMinio(ByteArrayOutputStream outputStream, String fileName) throws Exception {
// 1.连接Minio
MinioClient minioClient =MinioClient.builder()
.endpoint(minioEndpoint)
.credentials(accessKey, secretKey)
.build();
// 2.上传
// 把os流转为is流
ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
String newFileName = assemblyPdfName(fileName);
PutObjectArgs putObjectArgs = PutObjectArgs.builder()
.bucket(bucketName)
.object(newFileName)
.contentType("image/jpeg")
.stream(inputStream, inputStream.available(), ObjectWriteArgs.MIN_MULTIPART_SIZE).build();
minioClient.putObject(putObjectArgs);
return newFileName;
}
/**
* 拼接文件名
* @param name
* @return
*/
public String assemblyPdfName(String name) {
return StringUtils.format("{}/{}", DateUtils.datePath(), name);
}
private String getReportName() {
return DateUtils.parseDateToStr("yyyyMMddHHmm", new Date());
}
private void carTestStatistics(DataStatisticsResponse response, Task task) {
if (task.getModelTestTaskId() != null) {
......
......@@ -204,4 +204,10 @@ public class TaskController extends BaseController {
return R.ok(taskService.createProject(request));
}
@ApiOperation("查询车辆原始记录图片")
@RequestMapping(method = RequestMethod.POST, value = "/getPicture")
public R<String> getPicture(@Validated @RequestBody TaskPictureRequest request) throws IOException {
return R.ok(taskService.getPicture(request));
}
}
package com.ruoyi.web.request;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@ApiModel(value = "TaskPictureRequest", description = "查看车辆实验图片")
@Data
public class TaskPictureRequest {
@ApiModelProperty("任务id")
private Long taskId;
@ApiModelProperty("图片url")
private String url;
}
......@@ -37,6 +37,7 @@
<result property="taskList" column="task_list" jdbcType="VARCHAR"/>
<result property="pointer" column="pointer" jdbcType="BIGINT"/>
<result property="maxPointer" column="max_pointer" jdbcType="BIGINT"/>
<result property="pictureUrl" column="picture_url" jdbcType="VARCHAR"/>
</resultMap>
<resultMap id="ResponseMap" type="com.ruoyi.web.response.TaskFindResponse">
......
......@@ -66,6 +66,13 @@
<artifactId>quality-review</artifactId>
</dependency>
<!-- pdfbox -->
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.0</version>
</dependency>
</dependencies>
<build>
......
package com.ruoyi;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import io.minio.MinioClient;
import io.minio.ObjectWriteArgs;
import io.minio.PutObjectArgs;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
@SpringBootTest
public class TestPdfBox {
// @Value("${minio.bucketName}")
private static String bucketName = "vehicle-quality-review-oss";
// @Value("${minio.url}")
private static String minioEndpoint = "http://106.3.99.64:22013";
// @Value("${minio.accessKey}")
private static String accessKey = "FNGqDZnHJhrugLlZBh77";
// @Value("${minio.secretKey}")
private static String secretKey = "TvnWeQxAQ1OYiAN68qlzEDJl5RqZ7tQYTMJ8XM0M";
@Test
public static void main(String[] args) throws IOException {
String url = "http://106.3.99.64:22013/vehicle-quality-review-oss/2024/06/14/车型审查原始记录-202406141755.pdf";
String pictureUrl = processRemotePDF(url);
System.out.println(pictureUrl);
}
public static String processRemotePDF(String urlStr) throws IOException {
URL url = new URL(urlStr);
String pictureUrl = "";
// 打开远程连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
// 获取输入流
try (InputStream inputStream = connection.getInputStream()) {
// 加载PDF文档
PDDocument doc = PDDocument.load(inputStream);
PDFRenderer renderer = new PDFRenderer(doc);
// 遍历每页pdf
for (int i = 0; i < doc.getNumberOfPages(); i++) {
// dpi调到300左右即可,太小会模糊,太大会使图片变得很大
BufferedImage image = renderer.renderImageWithDPI(i, 300);
// 将 BufferedImage 转换为 ByteArrayOutputStream
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ImageIO.write(image, "jpg", outputStream);
pictureUrl += uploadMinio(outputStream,"车型审查原始记录_" + i + getReportName() + ".jpg") + ",";
}
// 关闭文档
doc.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭连接
connection.disconnect();
}
if (pictureUrl.endsWith(",")) {
pictureUrl = pictureUrl.substring(0, pictureUrl.length() - 1);
}
return pictureUrl;
}
/**
* 上传到minio
* @param outputStream 输出流
* @param fileName 文件名
* @return minio存储地址
*/
private static String uploadMinio(ByteArrayOutputStream outputStream, String fileName) throws Exception {
// 1.连接Minio
MinioClient minioClient =MinioClient.builder()
.endpoint(minioEndpoint)
.credentials(accessKey, secretKey)
.build();
// 2.上传
// 把os流转为is流
ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
String newFileName = assemblyPdfName(fileName);
PutObjectArgs putObjectArgs = PutObjectArgs.builder()
.bucket(bucketName)
.object(newFileName)
.contentType("image/jpeg")
.stream(inputStream, inputStream.available(), ObjectWriteArgs.MIN_MULTIPART_SIZE).build();
minioClient.putObject(putObjectArgs);
return newFileName;
}
/**
* 拼接文件名
* @param name
* @return
*/
public static String assemblyPdfName(String name) {
return StringUtils.format("{}/{}", DateUtils.datePath(), name);
}
private static String getReportName() {
return DateUtils.parseDateToStr("yyyyMMddHHmm", new Date());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment