From 999c71172bc2135665bf84f5b345d59be01f28bb Mon Sep 17 00:00:00 2001
From: rain <167982779@qq.com>
Date: Fri, 19 Apr 2024 17:17:54 +0800
Subject: [PATCH] 新增照片水印,将照片转为二进制前先打上水印

---
 src/main/java/com/dji/sample/territory/service/impl/TbFjServiceImpl.java |   97 ++++++++++++++++++++++++++++++++----------------
 1 files changed, 64 insertions(+), 33 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..a9b3c0c 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
@@ -15,6 +15,7 @@
 import java.io.*;
 import java.net.HttpURLConnection;
 import java.net.URL;
+import java.util.ArrayList;
 import java.util.List;
 import java.util.UUID;
 
@@ -32,18 +33,18 @@
      * @return
      * @throws IOException
      */
-    public TbFjEntity insertData(List<MediaFileEntity> mediaFile, LotInfo lotInfo) throws IOException {
-        TbFjEntity tbFj = new TbFjEntity();
+    public int insertData(List<MediaFileEntity> mediaFile, LotInfo lotInfo) throws IOException {
+        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);
+            mapper.insert(tbFj);
+            count++;
         }
-        return tbFj;
+        return count;
     }
 
     /**
@@ -54,7 +55,7 @@
      * @return
      * @throws IOException
      */
-    private TbFjEntity dbConvertToEntity(MediaFileEntity mediaFile, LotInfo lotInfo) {
+    private TbFjEntity dbConvertToEntity(MediaFileEntity mediaFile, LotInfo lotInfo) throws IOException {
         String jsonString = JSONObject.toJSONString(mediaFile.getMetadata());
         JSONObject jsonObject = JSONObject.parseObject(jsonString);
         Double absoluteAltitude = jsonObject.getDouble("absoluteAltitude");
@@ -63,30 +64,46 @@
         JSONObject shootPosition = jsonObject.getJSONObject("shootPosition");
         Double lat = shootPosition.getDouble("lat");
         Double lng = shootPosition.getDouble("lng");
+        int psjd = 0;
+        String fjhxz = "0";
+        int pshgj = 0;
+        String jym = "0";
+        String psry = "0";
+        String zsdm = "0";
+        String dklx = "0";
+        String xzqdm = "0";
         Long pssj = mediaFile.getCreateTime();
-        String bsm= UUID.randomUUID().toString();
-        String fjmc= mediaFile.getFileName();
+        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);
+        File file =downloadFile(url);
+        File file1=WaterMark.addWatermark(file,patchesConfigPojo.getUnzip(),pssj,lat,lng);
+        byte[] FJ = fileToByteArray(file1);
         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 {
+        if (lotInfo != null) {
+            builder.bsm(bsm)
+                    .dkbsm(lotInfo.getDkbh())
+                    .xzqdm(xzqdm)
+                    .dklx(dklx)
+                    .xdgd(relativeAltitude)
+                    .jdgd(absoluteAltitude)
+                    .Latitude(lat)
+                    .longitude(lng)
+                    .fj(FJ)
+                    .fjmc(fjmc)
+                    .fjlx(1)
+                    .psfyj(gimbalYawDegree)
+                    .pssj(String.valueOf(pssj))
+                    .psjd(psjd)
+                    .fjhxz(fjhxz)
+                    .pshgj(pshgj)
+                    .zsdm(zsdm)
+                    .psry(psry)
+                    .jym(jym)
+                    .build();
+        } else {
             throw new IllegalArgumentException("未匹配到相关地块");
         }
         return builder.build();
@@ -98,15 +115,19 @@
      * @param fileUrl
      * @return
      */
-    public static byte[] downloadFileAsBytes(String fileUrl) {
-        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
+    public  File downloadFile(String fileUrl) {
 
+        File downloadedFile = null;
+        String localFilePath =  patchesConfigPojo.getUnzip()+ "tmp.jpg";
         try {
             URL url = new URL(fileUrl);
             HttpURLConnection connection = (HttpURLConnection) url.openConnection();
             connection.setRequestMethod("GET");
 
-            try (InputStream inputStream = connection.getInputStream()) {
+            downloadedFile = new File(localFilePath);
+
+            try (InputStream inputStream = connection.getInputStream();
+                 OutputStream outputStream = new FileOutputStream(downloadedFile)) {
                 byte[] buffer = new byte[1024];
                 int bytesRead;
                 while ((bytesRead = inputStream.read(buffer)) != -1) {
@@ -114,11 +135,21 @@
                 }
             }
 
+            System.out.println("File downloaded and saved at: " + downloadedFile.getAbsolutePath());
+
         } catch (IOException e) {
             e.printStackTrace();
         }
 
-        return outputStream.toByteArray();
+        return downloadedFile;
     }
 
-}
+    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;
+    }
+
+}
\ No newline at end of file

--
Gitblit v1.9.3