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 |   30 +++++++++++++++++++++++-------
 1 files changed, 23 insertions(+), 7 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 6e85ecb..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
@@ -55,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");
@@ -78,7 +78,9 @@
         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)
@@ -113,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) {
@@ -129,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