MyTask.java 10.7 KB
package com.example.test;

import com.sun.org.apache.xpath.internal.operations.Bool;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.InetAddress;
import java.net.URI;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

@Component
public class MyTask implements ApplicationRunner {

    private String url = "http://114.55.30.100:9020/";
    private String ip;

    {
        try {
            ip = InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {


        File file = new File("E:\\wwwhtdocs\\SmartCampus\\face17e50\\School562\\Student");
        File[] files = file.listFiles();
        for (int i = 0; i < files.length; i++) {
            File img = files[i];
            File outFile = new File("E:\\wwwhtdocs\\SmartCampus\\face17e50\\School562\\Student",img.getName().split("\\.")[0]+".png");

            FileInputStream fileInputStream = new FileInputStream(img);
            FileOutputStream fileOutputStream =new FileOutputStream(outFile);
            byte[] bytes = new byte[1024];
            int length = 0;
            while ((length=fileInputStream.read(bytes))!=-1){
                fileOutputStream.write(bytes,0,length);
            }

            fileInputStream.close();
            fileOutputStream.close();
            img.delete();
        }

//        sendHaiKangLocal();

//        removeImgs("C:\\TaoHandong\\copy\\school110\\Student");

//        sendImgs();

        /**
         * 下发人脸到海康
         */

        /*File file = new File("C:\\Users\\taohandong\\Desktop\\下发学校资料\\高级中学卡号和姓名.txt");

        BufferedReader bufferedReader = new BufferedReader(new FileReader(file));

        String content = null;
        String deviceId = "18262213";
        String SchoolId = "479";

        RestTemplate restTemplate = new RestTemplate();
        List<String> studentS = new ArrayList<>();
        while ((content=bufferedReader.readLine())!=null){

            if (content.split(",").length>2){
                String card = content.split(",")[0];
                String name = content.split(",")[1];
                String studentcode = content.split(",")[2];

                String contUrl = url+"facereco/SendCard?card="+card+"&name="+name+"&validTimeEnabled="+1+"&startTime=2019-09-28 00:00:00&endTime=2023-09-28 00:00:00"+
                        "&deviceId="+deviceId;
                //下发卡号
                String cardResult = restTemplate.getForObject(contUrl,String.class);
                System.out.println("卡号下发:"+cardResult);

                String faceUrl = url+"facereco/SendStudentSingleImage?schoolId="+SchoolId+"&deviceId=" + deviceId + "&studentCode=" + studentcode;

                String result = restTemplate.getForObject(faceUrl, String.class);
                if (result.equals("0")){
                    studentS.add(studentcode);
                }
                System.out.println("人脸下发:"+result+" "+studentcode);
            }

        }*/


        /**
         * 替换学籍号
         */
        /*File file = new File("E:\\wwwhtdocs\\SmartCampus\\face17e5\\School110\\Student");

        File fileStudentcodes = new File("E:\\wwwhtdocs\\SmartCampus\\face17e5\\School110\\studentcode.txt");

        List<String> studentList = new ArrayList<>();
        String content = null;
        BufferedReader bufferedReader = new BufferedReader(new FileReader(fileStudentcodes));
        while ((content=bufferedReader.readLine())!=null){
                studentList.add(content);
        }


        List<String> studentListCode = new ArrayList<>();

        File[] files = file.listFiles();
        System.out.println("files:"+files.length);

        for (int i = 0; i < files.length; i++) {
            File img = files[i];

            String studentCode = img.getName().split("\\.")[0];
            if (!studentList.contains(studentCode))studentListCode.add(studentCode);

        }

        System.out.println("studentList:"+studentList.toString()+"\r\n");

        System.out.println("studentListCode:"+studentListCode.toString());*/


    }
    private String haikangUrl = "http://192.168.1.100:8000/";
    private void sendHaiKangLocal() throws IOException {

        File file = new File("C:\\TaoHandong\\copy\\school110\\盖北.txt");

        BufferedReader bufferedReader = new BufferedReader(new FileReader(file));

        String content = null;
        String deviceId = "218963574";
        String SchoolId = "110";

        RestTemplate restTemplate = new RestTemplate();
        List<String> strings = new ArrayList<>();//记录下发失败图片原因
        List<String> strings1 = new ArrayList<>();

        while ((content=bufferedReader.readLine())!=null){

            if (content.split(",").length>2){
                String card = content.split(",")[0];
                String name = content.split(",")[1];
                String studentcode = content.split(",")[2];

                String contUrl = haikangUrl+"facereco/SendCard?card="+card+"&name="+name+"&validTimeEnabled="+1+"&startTime=2019-09-28 00:00:00&endTime=2023-09-28 00:00:00"+
                        "&deviceId="+deviceId;
                //下发卡号
                String cardResult = restTemplate.getForObject(contUrl,String.class);
                System.out.println("卡号下发:"+cardResult+" card:"+card);

                String faceUrl = haikangUrl+"facereco/SendStudentSingleImage?schoolId="+SchoolId+"&deviceId=" + deviceId + "&studentCode=" + studentcode;

                String result = restTemplate.getForObject(faceUrl, String.class);
                if (result.equals("0")){
                    String url = "C:\\TaoHandong\\copy\\school110\\Student\\"+studentcode+".png";
                    File fileExit = new File(url);
                    if (!fileExit.exists()){
                        strings.add(studentcode+",图片不存在");
                    }else {
                        strings1.add(studentcode+",图片不合格");
                    }
                }
                System.out.println("本地人脸下发:"+result+" "+studentcode);
            }else {
                System.out.println("信息不全");
            }

            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
        System.out.println("图片不合格人数:"+strings1.toString()+" 图片不存在人数:"+strings.toString());
        System.out.println("图片不合格人数:"+strings1.size()+" 图片不存在人数:"+strings.size());
    }

    private void sendImgs() throws IOException {
        File file = new File("./config.txt");
        if (!file.exists()){
            System.out.println("配置文件不存在");
            file.createNewFile();
            return;
        }

        BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
        String content = null;
        String schoolId = "";
        String filePath = "";//图片目录
        while ((content = bufferedReader.readLine()) != null) {
            if (content.contains("schoolId"))
                schoolId = content.replace("schoolId:", "").trim();

            if (content.contains("filePath:"))
                filePath = content.replace("filePath:","").trim();

        }

        RestTemplate  restTemplate = new RestTemplate();

        File filePaths = new File(filePath);
        if (filePaths.exists()){
            File[] files = filePaths.listFiles();
            for (int i = 0; i < files.length; i++) {
                File imgFile = files[i];
                String studentCode = imgFile.getName().split("\\.")[0];
                if (!StringUtils.isEmpty(studentCode)){
                    String imgPath = imgFile.getAbsolutePath().replace("\\","/");
//                    imgPath = imgPath.replace("\\","%5C");
                    String apiBaseUrl = String.format("http://" + ip + ":8991/user/uploadImgAndUserInfo?file=%s&schoolId=" + schoolId + "&studentCode=" + studentCode+"&clint_type=22",imgPath);
                    System.out.println("apiBaseUrl:"+apiBaseUrl);
                    boolean result = restTemplate.getForObject(apiBaseUrl, boolean.class);
                    System.out.println("result:"+result);
                }else {
                    System.out.println("学籍号为空");
                }
            }
            System.out.println("结束调用");
        }else {
            System.out.println("图片目录不存在");
        }
    }


    /**
     * 移除相同的图片
     */
    private void removeImgs(String path) {
//        String path = "C:\\TaoHandong\\copy\\school479\\StudentCompressed1";
        File file = new File(path);
        File[] imgFiles = file.listFiles();

        List<String> list = new ArrayList<>();
        List<String> listExit = new ArrayList<>();

        for (int i = 0; i < imgFiles.length; i++) {
            File img = imgFiles[i];

            String name = img.getName().split("\\.")[0];

            if (list.contains(name)) {
                listExit.add(name);
            } else {
                list.add(name);
            }

        }
        System.out.println("listExit:" + list.size());
        System.out.println("listExit:" + listExit.toString());

        for (int i = 0; i < listExit.size(); i++) {
            String name = listExit.get(i);
            File file1 = new File(path + File.separator + name + ".jpg");
            if (file1.exists())
                System.out.println("file1.delete():" + file1.delete());
        }
    }

    private void moveImg(BufferedImage bufferedImage, File img, File outFile, String newStudentCode, String studentCode) {
        try {


            //创建一个空白大小相同的RGB背景
            BufferedImage newBufferedReader = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(), BufferedImage.TYPE_INT_RGB);
            newBufferedReader.createGraphics().drawImage(bufferedImage, 0, 0, Color.WHITE, null);

            ImageIO.write(newBufferedReader, "png", new File(outFile.getAbsolutePath(), studentCode + ".png"));

            ImageIO.write(newBufferedReader, "png", new File(img.getParentFile().getAbsolutePath(), newStudentCode + ".png"));

            img.delete();

            System.out.println("操作成功");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}