package com.zd.face.controller;

import com.arcsoft.face.FaceFeature;
import com.arcsoft.face.FaceInfo;
import com.arcsoft.face.toolkit.ImageInfo;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.zd.common.core.domain.R;
import com.zd.common.core.web.domain.ResultData;
import com.zd.common.swagger.config.Knife4jConfiguration;
import com.zd.face.utils.FileUtil;
import com.zd.system.api.face.FaceCompare;
import com.zd.face.service.FaceService;
import com.zd.system.api.domain.LabStudentsInfo;
import com.zd.system.api.students.RemoteStudentsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.util.List;

/**
 * @Author: zhoupan
 * @Date: 2021/10/12/13:36
 * @Description:
 */

@Api(tags = "【人脸识别】")
@ApiSupport(author = Knife4jConfiguration.Author.ZP)
@RestController
@RequestMapping("/faceApi")
public class FaceApi {

    @Autowired
    FaceService faceService;

    @Autowired
    RemoteStudentsService remoteStudentsService;


    @ApiOperation("根据照片文件获取特征码")
    @PostMapping("faceFeature")
    public ResultData faceFeature(@RequestParam("file") MultipartFile file) throws IOException {
        ImageInfo imageInfo;
        File file1=null;
        try {
            file1= FileUtil.multipartFileToFile(file);
            imageInfo = faceService.getImageInfo(file1);
        } finally {
            if(file1!=null)
            {
                file1.delete();
            }
        }
        List<FaceInfo> faceInfos = faceService.faceDetect(imageInfo);
        FaceFeature faceFeature = faceService.faceFeature(imageInfo, faceInfos);
        return ResultData.success(faceFeature.getFeatureData());
    }
    /**
     * 人脸比较
     * @return
     */
    @ApiOperation("人脸比较")
    @PostMapping("/compare")
    public ResultData faceDetection(@RequestBody  FaceCompare faceCompare)
    {

        R<LabStudentsInfo> info = remoteStudentsService.getInfo(faceCompare.getUserId());
        if(info.getCode()==200&&info.getData()!=null)
        {
            FaceFeature target = new FaceFeature(info.getData().getFaceFeature());
            FaceFeature source = new FaceFeature(faceCompare.getData());
            Boolean compore = faceService.compore(target, source);

            return ResultData.result(compore,()->"人脸对比不符！");
        }else
        {
            return ResultData.fail("未获取到对比数据！:"+info.getMsg());
        }
    }



    //获取流文件
    private static void inputStreamToFile(InputStream ins, File file) {
        try {
            OutputStream os = new FileOutputStream(file);
            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            os.close();
            ins.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }





}
