From 73c208b75b066e5b464d3dbbabcaedfe657ea0ac Mon Sep 17 00:00:00 2001
From: rain <167982779@qq.com>
Date: Tue, 14 May 2024 16:40:01 +0800
Subject: [PATCH] 方法调用测试
---
src/main/java/com/dji/sample/territory/service/impl/TbFjServiceImpl.java | 195 +++++++++++++++++++++++++++++++++++++-----------
1 files changed, 148 insertions(+), 47 deletions(-)
diff --git a/src/main/java/com/dji/sample/territory/service/impl/TbFjServiceImpl.java b/src/main/java/com/dji/sample/territory/service/impl/TbFjServiceImpl.java
index 04b9f54..1622950 100644
--- a/src/main/java/com/dji/sample/territory/service/impl/TbFjServiceImpl.java
+++ b/src/main/java/com/dji/sample/territory/service/impl/TbFjServiceImpl.java
@@ -1,28 +1,49 @@
package com.dji.sample.territory.service.impl;
-import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.dji.sample.media.model.MediaFileEntity;
+import com.dji.sample.patches.config.pojo.PatchesConfigPojo;
import com.dji.sample.patches.model.entity.LotInfo;
import com.dji.sample.territory.dao.ITbFjMapper;
import com.dji.sample.territory.model.entity.TbFjEntity;
import com.dji.sample.territory.service.ITbFJService;
-import com.google.gson.JsonObject;
+import com.dji.sample.territory.utils.HashUtil;
+import com.dji.sample.territory.utils.Sm3Util;
+import com.dji.sample.territory.utils.VideoZipUtil;
+import com.dji.sample.territory.utils.WaterMarkUtil;
+import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+import javax.imageio.ImageIO;
+import javax.xml.crypto.Data;
+import java.awt.image.BufferedImage;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
+import java.text.SimpleDateFormat;
+import java.time.Instant;
+import java.time.LocalDateTime;
+import java.time.ZoneId;
+import java.time.format.DateTimeFormatter;
+import java.util.ArrayList;
+import java.util.Date;
import java.util.List;
import java.util.UUID;
+import static com.dji.sample.territory.utils.CoordinateSystemUtil.*;
+import static com.dji.sample.territory.utils.SM2SignUtil.getSM2PrivateKey;
+import static com.dji.sample.territory.utils.SM2SignUtil.signWithSM2;
+
@Service
-@DS("sqlite")
+@DS("sqlite-ret")
public class TbFjServiceImpl implements ITbFJService {
@Autowired
- private ITbFjMapper mapper;
+ private ITbFjMapper tbFjMapper;
+ @Autowired
+ private PatchesConfigPojo patchesConfigPojo;
/**
* 按照地块编号所对应的信息和音视频文件存入sqlite数据库
@@ -32,18 +53,29 @@
* @return
* @throws IOException
*/
- public TbFjEntity insertData(List<MediaFileEntity> mediaFile, LotInfo lotInfo) throws IOException {
- TbFjEntity tbFj = new TbFjEntity();
+ @Transactional
+ public int insertData(List<MediaFileEntity> mediaFile, LotInfo lotInfo) throws Exception {
+ int count = 0;
+ List<TbFjEntity> list = new ArrayList<>();
+ TbFjEntity tbFj;
for (int i = 0; i < mediaFile.size(); i++) {
MediaFileEntity file = mediaFile.get(i);
tbFj = dbConvertToEntity(file, lotInfo);
- try {
- mapper.insert(tbFj);
- } catch (Exception e) {
- e.printStackTrace();
- }
+ list.add(tbFj);
+ tbFjMapper.insert(tbFj);
+ count++;
}
- return tbFj;
+ return count;
+ }
+
+ public void insertOneData(MediaFileEntity mediaFile, LotInfo lotInfo) throws Exception {
+ TbFjEntity tbFj = dbConvertToEntity(mediaFile, lotInfo);
+ tbFjMapper.insert(tbFj);
+ }
+
+ @Override
+ public void deleteData() {
+ tbFjMapper.delete(null);
}
/**
@@ -54,7 +86,9 @@
* @return
* @throws IOException
*/
- private TbFjEntity dbConvertToEntity(MediaFileEntity mediaFile, LotInfo lotInfo) {
+ private TbFjEntity dbConvertToEntity(MediaFileEntity mediaFile, LotInfo lotInfo) throws Exception {
+ File file1 = null;
+ byte[] FJ = null;
String jsonString = JSONObject.toJSONString(mediaFile.getMetadata());
JSONObject jsonObject = JSONObject.parseObject(jsonString);
Double absoluteAltitude = jsonObject.getDouble("absoluteAltitude");
@@ -63,32 +97,75 @@
JSONObject shootPosition = jsonObject.getJSONObject("shootPosition");
Double lat = shootPosition.getDouble("lat");
Double lng = shootPosition.getDouble("lng");
- Long pssj = mediaFile.getCreateTime();
- String bsm= UUID.randomUUID().toString();
- String fjmc= mediaFile.getFileName();
+ double[] coordinates = extractCoordinates(pointWGStoCGCS(lng, lat));
+ double x = Double.parseDouble(((formatCoordinate(coordinates[1]))));
+ double y = Double.parseDouble(((formatCoordinate(coordinates[0]))));
+ //本地数据库没有的数据在FJ表非空用0
+ int psjd = 0;
+ int pshgj = 0;
+ String psry = "中图智绘无人机";
+ String zsdm = "23C8CCC61E3042FBA6A658F319337B1A";
+ String dklx = "0";
+ int fjlx = 1;
+ String pstz="Y";
+ Long sj = mediaFile.getCreateTime();
+ String pssj=convertTimestampToDateTime(sj);
+ String bsm = UUID.randomUUID().toString();
+ String fjmc = mediaFile.getFileName();
String key = mediaFile.getObjectKey();
String head = "http://dev.jxpskj.com:9000/cloud-bucket";
String url = head + key;
- byte[] FJ = downloadFileAsBytes(url);
- TbFjEntity.TbFjEntityBuilder builder = TbFjEntity.builder();
- if (lotInfo != null){
- builder.bsm(bsm)
- .dkbsm(lotInfo.getDkbh())
- .xzqdm(lotInfo.getXzqdm())
- .dklx(lotInfo.getDklx())
- .xdgd(relativeAltitude)
- .jdgd(absoluteAltitude)
- .Latitude(lat)
- .longitude(lng)
- .fj(FJ)
- .fjmc(fjmc)
- .fjlx(1)
- .psfyj(gimbalYawDegree)
- .pssj(String.valueOf(pssj))
- .build();}
- else {
- throw new IllegalArgumentException("未匹配到相关地块");
+ File file = downloadFile(url);
+ String fjhxz = HashUtil.SM3Hash(file);
+ BufferedImage image = ImageIO.read(file);
+ int width = image.getWidth();
+ int height = image.getHeight();
+ //对应图片和视频文件进行不同处理
+ boolean endsWith = key.endsWith(".mp4");
+ if (!endsWith) {
+ file1 = WaterMarkUtil.addWatermark(file, patchesConfigPojo.getUnzip(), sj, lat, lng,gimbalYawDegree);
+ FJ = fileToByteArray(file1);
+ } else {
+ file1 = VideoZipUtil.compressVideo(file, 800000, 128000, 1280, 720);
+ FJ = fileToByteArray(file1);
}
+ String sm3 = Sm3Util.calculateSM3Hash(fjhxz+","+pssj+","+y+","+x+","+gimbalYawDegree+","+psjd+","+pshgj+","+psry+","+zsdm);
+
+ byte[] hash = sm3.getBytes(); // 注意:使用SM3或其他哈希算法来计算数据的哈希值
+ // 加载私钥
+ ECPrivateKeyParameters sm2PrivateKey = getSM2PrivateKey();
+ // 使用SM2私钥对哈希值进行签名
+ String signatureHex = signWithSM2(hash, sm2PrivateKey);
+ TbFjEntity.TbFjEntityBuilder builder = TbFjEntity.builder();
+ if (lotInfo != null) {
+ builder.bsm(bsm)
+ .dkbsm(lotInfo.getBsm())
+ .xzqdm(lotInfo.getXzqdm())
+ .dklx(dklx)
+ .xdgd(relativeAltitude)
+ .jdgd(absoluteAltitude)
+ .Latitude(x)
+ .longitude(y)
+ .fj(FJ)
+ .fjmc(fjmc)
+ .fjlx(fjlx)
+ .psfyj(gimbalYawDegree)
+ .pssj(pssj)
+ .psjd(psjd)
+ .fjhxz(fjhxz)
+ .pshgj(pshgj)
+ .zsdm(zsdm)
+ .psry(psry)
+ .jym(signatureHex)
+ .url(url)
+ .pstz(pstz)
+ .fjyskd(width)
+ .fjysgd(height)
+ .build();
+ }
+// else {
+//// throw new IllegalArgumentException("未匹配到相关地块");
+// }
return builder.build();
}
@@ -98,27 +175,51 @@
* @param fileUrl
* @return
*/
- public static byte[] downloadFileAsBytes(String fileUrl) {
- ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
-
+ public File downloadFile(String fileUrl) {
+ File downloadedFile = null;
try {
URL url = new URL(fileUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
+ downloadedFile = new File(File.createTempFile("temp", ".jpeg").toURI());
+ InputStream inputStream = connection.getInputStream();
+ OutputStream outputStream = new FileOutputStream(downloadedFile);
+ byte[] buffer = new byte[1024];
+ int bytesRead;
+ while ((bytesRead = inputStream.read(buffer)) != -1) {
+ outputStream.write(buffer, 0, bytesRead);
- try (InputStream inputStream = connection.getInputStream()) {
- byte[] buffer = new byte[1024];
- int bytesRead;
- while ((bytesRead = inputStream.read(buffer)) != -1) {
- outputStream.write(buffer, 0, bytesRead);
- }
}
-
} catch (IOException e) {
e.printStackTrace();
}
- return outputStream.toByteArray();
+ return downloadedFile;
}
-}
+ /**
+ * 将文件转换为字节数组。
+ *
+ * @param file 需要转换的文件对象。
+ * @return 文件内容的字节数组。
+ * @throws IOException 如果读取文件发生错误。
+ */
+ public static byte[] fileToByteArray(File file) throws IOException {
+ FileInputStream fis = new FileInputStream(file);
+ byte[] data = new byte[(int) file.length()];
+ fis.read(data);
+ fis.close();
+ return data;
+ }
+ public static String convertTimestampToDateTime(long timestamp) {
+ // 将时间戳转换为 Instant 对象
+ Instant instant = Instant.ofEpochMilli(timestamp);
+
+ // 将 Instant 对象转换为 LocalDateTime 对象
+ LocalDateTime dateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
+
+ // 格式化日期时间
+ DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
+ return dateTime.format(formatter);
+ }
+}
\ No newline at end of file
--
Gitblit v1.9.3