From fa89f5c4b746a41efee0362bd3a42ea2c97e2cff Mon Sep 17 00:00:00 2001
From: rain <167982779@qq.com>
Date: Mon, 22 Apr 2024 18:13:46 +0800
Subject: [PATCH] 在加水印前对文件进行音视频处理,分别执行不同方法。增加图片压缩、视频压缩方法。

---
 src/main/java/com/dji/sample/territory/service/impl/TbFjServiceImpl.java |   64 ++++++++++++++++++++-----------
 1 files changed, 41 insertions(+), 23 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 27724eb..0f534bd 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
@@ -4,13 +4,17 @@
 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.patches.utils.MultipartFileTOFileUtil;
 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.VideoZipUtil;
+import com.dji.sample.territory.utils.WaterMark;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 
 import java.io.*;
 import java.net.HttpURLConnection;
@@ -23,7 +27,9 @@
 @DS("sqlite-ret")
 public class TbFjServiceImpl implements ITbFJService {
     @Autowired
-    private ITbFjMapper mapper;
+    private ITbFjMapper tbFjMapper;
+    @Autowired
+    private PatchesConfigPojo patchesConfigPojo;
 
     /**
      * 按照地块编号所对应的信息和音视频文件存入sqlite数据库
@@ -33,6 +39,7 @@
      * @return
      * @throws IOException
      */
+    @Transactional
     public int insertData(List<MediaFileEntity> mediaFile, LotInfo lotInfo) throws IOException {
         int count = 0;
         List<TbFjEntity> list = new ArrayList<>();
@@ -41,7 +48,7 @@
             MediaFileEntity file = mediaFile.get(i);
             tbFj = dbConvertToEntity(file, lotInfo);
             list.add(tbFj);
-            mapper.insert(tbFj);
+            tbFjMapper.insert(tbFj);
             count++;
         }
         return count;
@@ -56,6 +63,8 @@
      * @throws IOException
      */
     private TbFjEntity dbConvertToEntity(MediaFileEntity mediaFile, LotInfo lotInfo) throws IOException {
+        File file1 = null;
+        byte[] FJ = null;
         String jsonString = JSONObject.toJSONString(mediaFile.getMetadata());
         JSONObject jsonObject = JSONObject.parseObject(jsonString);
         Double absoluteAltitude = jsonObject.getDouble("absoluteAltitude");
@@ -64,6 +73,7 @@
         JSONObject shootPosition = jsonObject.getJSONObject("shootPosition");
         Double lat = shootPosition.getDouble("lat");
         Double lng = shootPosition.getDouble("lng");
+        //本地数据库没有的数据在FJ表非空用0
         int psjd = 0;
         String fjhxz = "0";
         int pshgj = 0;
@@ -72,15 +82,23 @@
         String zsdm = "0";
         String dklx = "0";
         String xzqdm = "0";
+        int fjlx = 1;
         Long pssj = mediaFile.getCreateTime();
         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;
-        File file =downloadFile(url);
-        File file1=WaterMark.addWatermark(file,patchesConfigPojo.getUnzip(),pssj,lat,lng);
-        byte[] FJ = fileToByteArray(file1);
+        File file = downloadFile(url);
+        //对应图片和视频文件进行不同处理
+        boolean endsWith = key.endsWith(".mp4");
+        if (!endsWith) {
+            file1 = WaterMark.addWatermark(file, patchesConfigPojo.getUnzip(), pssj, lat, lng);
+            FJ = fileToByteArray(file1);
+        } else {
+            file1 = VideoZipUtil.compressVideo(file, 800000, 128000, 1280, 720);
+            FJ = fileToByteArray(file1);
+        }
         TbFjEntity.TbFjEntityBuilder builder = TbFjEntity.builder();
         if (lotInfo != null) {
             builder.bsm(bsm)
@@ -91,9 +109,9 @@
                     .jdgd(absoluteAltitude)
                     .Latitude(lat)
                     .longitude(lng)
-//                    .fj(FJ)
+                    .fj(FJ)
                     .fjmc(fjmc)
-                    .fjlx(1)
+                    .fjlx(fjlx)
                     .psfyj(gimbalYawDegree)
                     .pssj(String.valueOf(pssj))
                     .psjd(psjd)
@@ -116,28 +134,22 @@
      * @param fileUrl
      * @return
      */
-    public  File downloadFile(String fileUrl) {
-
+    public File downloadFile(String fileUrl) {
         File downloadedFile = null;
-        String localFilePath =  patchesConfigPojo.getUnzip()+ "tmp.jpg";
+        String localFilePath = patchesConfigPojo.getUnzip() + "tmp.jpg";
         try {
             URL url = new URL(fileUrl);
             HttpURLConnection connection = (HttpURLConnection) url.openConnection();
             connection.setRequestMethod("GET");
-
             downloadedFile = new File(localFilePath);
+            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();
-                 OutputStream outputStream = new FileOutputStream(downloadedFile)) {
-                byte[] buffer = new byte[1024];
-                int bytesRead;
-                while ((bytesRead = inputStream.read(buffer)) != -1) {
-                    outputStream.write(buffer, 0, bytesRead);
-                }
             }
-
-            System.out.println("File downloaded and saved at: " + downloadedFile.getAbsolutePath());
-
         } catch (IOException e) {
             e.printStackTrace();
         }
@@ -145,6 +157,13 @@
         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()];
@@ -152,5 +171,4 @@
         fis.close();
         return data;
     }
-
 }
\ No newline at end of file

--
Gitblit v1.9.3