Newer
Older
newfiber-termite / newfiber-termites / newfiber-termites-package / src / test / java / CommonTest.java
@xiongkai xiongkai on 9 Jul 3 KB 图片添加水印
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.img.Img;
import cn.hutool.core.img.ImgUtil;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.newfiber.common.core.utils.file.FileUtils;
import com.newfiber.termite.domain.jingchu.JingChuUpload;
import com.newfiber.termite.util.CosUtil;
import java.awt.Color;
import java.awt.Font;
import java.awt.Image;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.web.multipart.MultipartFile;

@Slf4j
public class CommonTest {

    @Test
    public void fileUploadTest(){
        MultipartFile file = FileUtils.getMultipartFile(new File("C:\\Users\\Administrator\\Pictures\\201210133348076.jpg"));
        System.out.println(CosUtil.putFile(file));
    }

    @Test
    public void jingchuTest(){
        String url = "https://sk.hubeishuiyi.cn/bylr";
        String token = "eyJraWQiOiJrMSIsImFsZyI6IlJTMjU2In0.eyJpc3MiOiJhZG1pbkBnaXN1bmkuY29tIiwiYXVkIjoidXVtcyIsImp0aSI6IjYwMGZiYmMyLWE1NDgtNGZmNC04ZGNhLTZkNTdkZjY1OTYxNyIsImlhdCI6MTcxNzcyNDA1MywibmJmIjoxNzE3NzIzNDUzLCJzdWIiOiJzdXBlciIsImV4cCI6MTcxNzc4NTQ5Mywia2V5IjoiMmQ5ZWY2MTNlYTkxNDc4MDhlOGZiYTMxYWNlOTE0NmEiLCJpZCI6IjEiLCJuYW1lIjoic3VwZXIiLCJuaWNrTmFtZSI6Iui2hee6p-euoeeQhuWRmCIsImFjY291bnQiOiJzdXBlciIsInR5cGUiOiIyIiwiY2xpZW50IjpudWxsLCJjbGllbnRfaWQiOiIiLCJ0b2tlblR5cGUiOiJVU0VSIn0.VzETse76m1rkRsMjst9kDr1oCnHrPq81pRs-34bjVXPDRiPT6ekVCm-63hLfoH4GAp25oTL9dWA1ax-t1KWlSzHUn82myEBokwCVWlkZZPVMzFMCgP_SNdFeYlvguVhige1nQ6B0wo5crGYKKsfllt0_bp98TFg3I3zF03WsORk7HlsZfsY7ROwYkXXkIgbdw5gIRYlZJ6W1MiMET9TOyAzWGJvEyTPewaW4XMoq-okZoAe_7gV2u_tylngqRCohxTSuA7xf59gK8OrstwTeId_qQp4remoEDdLJfYyzLXCX5xBKns8gw2i1MZfpq9rizDT-Qdz23VQDQdmeHWK7zQ";

        JingChuUpload jingChuUpload = new JingChuUpload();
        jingChuUpload.setMnNo("4202813006");
        jingChuUpload.setObDate(DateUtil.formatDateTime(new Date()).replace(" ", "T"));
        jingChuUpload.setObTime(DateUtil.format(new Date(), "yyMMddHHmm"));
        jingChuUpload.setOrder("123456");
        jingChuUpload.setReceiveType(0);
        jingChuUpload.setStationId(1625);
        jingChuUpload.setStationName("大晚垅水库");
        jingChuUpload.setDeviceId(5858);
        jingChuUpload.setDeviceName("大晚垅水库白蚁监测站");

        List<JingChuUpload> jingChuUploadList = new ArrayList<>();
        jingChuUploadList.add(jingChuUpload);
        HttpResponse httpResponse = HttpUtil.createPost(url.concat("/waterDataTermites/insertDataBatch")).
                body(JSONObject.toJSONString(jingChuUploadList)).timeout(1000).
                header("Authorization", token).execute();
        log.info("同步荆楚监测数据.请求:{},响应:{}", JSONObject.toJSONString(jingChuUploadList), httpResponse.body());

    }

    @Test
    public void imgUtil(){
        Font font = new Font("微软雅黑", Font.BOLD, 50);

//        ImgUtil.pressText(new File("C:\\Users\\Administrator\\Downloads\\capture_202407.jpg"),
//                new File("C:\\Users\\Administrator\\Downloads\\capture_202407_ex.jpg"),
//                "Hello\r\n" + "World", Color.WHITE, font, -1, -1, 1f);

        Image srcImage = ImgUtil.read(new File("C:\\Users\\Administrator\\Downloads\\capture_202407.jpg"));
        Img img = Img.from(srcImage);
        img.setPositionBaseCentre(false);
        img = img.pressText("项目:皇田水库", Color.WHITE, font, 0, 900, 1f);
        img = img.pressText("位置:3-5", Color.WHITE, font, 0, 1000, 1f);
        img = img.pressText("时间:2024/05/01 13:00", Color.WHITE, font, 0, 1100, 1f);

        ImgUtil.write(img.getImg(), new File("C:\\Users\\Administrator\\Downloads\\capture_202407_ex.jpg"));
    }
}