rain
2024-06-12 a0e7cf2d16dc23b1d6edf3b69678ab66ccacb54d
src/main/java/com/dji/sample/territory/service/impl/TbFjServiceImpl.java
@@ -4,24 +4,39 @@
import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.dji.sample.media.model.MediaFileEntity;
import com.dji.sample.media.util.ImgUtil;
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.dji.sample.territory.utils.VideoZipUtil;
import com.dji.sample.territory.utils.WaterMark;
import com.dji.sample.territory.utils.*;
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.math.BigDecimal;
import java.math.RoundingMode;
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-ret")
@@ -40,7 +55,7 @@
     * @throws IOException
     */
    @Transactional
    public int insertData(List<MediaFileEntity> mediaFile, LotInfo lotInfo) throws IOException {
    public int insertData(List<MediaFileEntity> mediaFile, LotInfo lotInfo) throws Exception {
        int count = 0;
        List<TbFjEntity> list = new ArrayList<>();
        TbFjEntity tbFj;
@@ -54,6 +69,18 @@
        return count;
    }
    public void insertOneData(MediaFileEntity mediaFile, LotInfo lotInfo) throws Exception {
        TbFjEntity tbFj = dbConvertToEntity(mediaFile, lotInfo);
        if (tbFj.getFjysgd() == 3000) {
            tbFjMapper.insert(tbFj);
        }
    }
    @Override
    public void deleteData() {
        tbFjMapper.delete(null);
    }
    /**
     * 将Media和Lotinfo实体类转化为TbFjEntity实体类
     *
@@ -62,7 +89,7 @@
     * @return
     * @throws IOException
     */
    private TbFjEntity dbConvertToEntity(MediaFileEntity mediaFile, LotInfo lotInfo) throws IOException {
    private TbFjEntity dbConvertToEntity(MediaFileEntity mediaFile, LotInfo lotInfo) throws Exception {
        File file1 = null;
        byte[] FJ = null;
        String jsonString = JSONObject.toJSONString(mediaFile.getMetadata());
@@ -70,61 +97,85 @@
        Double absoluteAltitude = jsonObject.getDouble("absoluteAltitude");
        Double relativeAltitude = jsonObject.getDouble("relativeAltitude");
        Double gimbalYawDegree = jsonObject.getDouble("gimbalYawDegree");
        Long sj = jsonObject.getLong("createdTime");
        JSONObject shootPosition = jsonObject.getJSONObject("shootPosition");
        Double lat = shootPosition.getDouble("lat");
        Double lng = shootPosition.getDouble("lng");
//        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;
        String fjhxz = "0";
        double psjd = WaterMarkUtil.convertAngle(gimbalYawDegree);
        int pshgj = 0;
        String jym = "0";
        String psry = "0";
        String zsdm = "0";
        String psry = "中图智绘无人机";
        String zsdm = "23C8CCC61E3042FBA6A658F319337B1A";
        String dklx = "0";
        String xzqdm = "0";
        int fjlx = 1;
        Long pssj = mediaFile.getCreateTime();
        String pstz = "Y";
        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;
        File file = downloadFile(url);
//        JSONObject jsonObjects = JSONObject.parseObject(ImgUtil.getInfo(file));
        JSONObject jsons = (JSONObject) (ImgUtil.getInfo(file));
        String str = jsons.toJSONString();
        String newstr = str.replace("+", "");
        JSONObject json = JSON.parseObject(newstr);
        Double pitch = json.getDouble("GimbalPitchDegree");
        String focal = json.getString("FocalLength");
        String numberPart = focal.replaceAll("[^\\d]", "");
        int psjj = Integer.parseInt(numberPart);
        json.getString("GimbalYawDegree");
        BufferedImage image = ImageIO.read(file);
        int width = image.getWidth();
        int height = image.getHeight();
        String fjhxz = null;
        //对应图片和视频文件进行不同处理
        boolean endsWith = key.endsWith(".mp4");
        if (!endsWith) {
            file1 = WaterMark.addWatermark(file, patchesConfigPojo.getUnzip(), pssj, lat, lng);
            file1 = WaterMarkUtil.addWatermark(file, sj, lat, lng, gimbalYawDegree);
            fjhxz = String.valueOf(SM3HashExample.HaXi(new File(file1.toURI())));
            FJ = fileToByteArray(file1);
        } else {
            file1 = VideoZipUtil.compressVideo(file, 800000, 128000, 1280, 720);
            FJ = fileToByteArray(file1);
        }
        String hxz = HashUtil.calculateHash((fjhxz + "," + pssj + "," + lng + "," + lat + "," + gimbalYawDegree + "," + psjd + "," + pshgj + "," + psry + "," + zsdm).getBytes());
        String jym=SM2SignExamples.jym(hxz);
        TbFjEntity.TbFjEntityBuilder builder = TbFjEntity.builder();
        if (lotInfo != null) {
            builder.bsm(bsm)
                    .dkbsm(lotInfo.getDkbh())
                    .xzqdm(xzqdm)
                    .dkbsm(lotInfo.getBsm())
                    .xzqdm(lotInfo.getXzqdm())
                    .dklx(dklx)
                    .xdgd(relativeAltitude)
                    .jdgd(absoluteAltitude)
                    .Latitude(lat)
                    .longitude(lng)
                    .xdgd(truncateToTwoDecimalPlaces(relativeAltitude))
                    .jdgd(truncateToTwoDecimalPlaces(absoluteAltitude))
                    .Latitude(truncateToSevenDecimalPlaces(lat))
                    .longitude(truncateToSevenDecimalPlaces(lng))
                    .fj(FJ)
                    .psjj(psjj)
                    .fjmc(fjmc)
                    .fjlx(fjlx)
                    .psfyj(gimbalYawDegree)
                    .pssj(String.valueOf(pssj))
                    .psjd(psjd)
                    .psfyj(pitch)
                    .pssj(pssj)
                    .psjd((int) psjd)
                    .fjhxz(fjhxz)
                    .pshgj(pshgj)
                    .zsdm(zsdm)
                    .psry(psry)
                    .jym(jym)
                    .url(url)
                    .pstz(pstz)
                    .fjyskd(width)
                    .fjysgd(height)
                    .build();
        } else {
            throw new IllegalArgumentException("未匹配到相关地块");
        }
//        else {
////            throw new IllegalArgumentException("未匹配到相关地块");
//        }
        return builder.build();
    }
@@ -134,14 +185,13 @@
     * @param fileUrl
     * @return
     */
    public File downloadFile(String fileUrl) {
    public static 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");
            downloadedFile = new File(localFilePath);
            downloadedFile = new File(File.createTempFile("temp", ".jpeg").toURI());
            InputStream inputStream = connection.getInputStream();
            OutputStream outputStream = new FileOutputStream(downloadedFile);
            byte[] buffer = new byte[1024];
@@ -171,4 +221,38 @@
        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);
    }
    public static Double truncateToTwoDecimalPlaces(Double value) {
        if (value == null) {
            return null;  // 处理null值情况
        }
        BigDecimal bd = new BigDecimal(value).setScale(2, RoundingMode.DOWN);
        return bd.doubleValue();
    }
    public static Double truncateToSevenDecimalPlaces(Double value) {
        if (value == null) {
            return null;  // 处理null值情况
        }
        BigDecimal bd = new BigDecimal(value).setScale(7, RoundingMode.DOWN);
        return bd.doubleValue();
    }
    public static void main(String[] args) {
        String s= String.valueOf(SM3HashExample.HaXi(new File("src/main/resources/FJ_10.jpeg")));
        System.out.println(s);
    }
}