Java对接海康威视获取人员信息下的面部信息下载转成Base64

注意
如果,不清楚之前的获取合作方的数据可以看这个,缺少jar包也看这个

https://blog.csdn.net/qq_41840735/article/details/132604747?csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22132604747%22%2C%22source%22%3A%22qq_41840735%22%7D

这儿只介绍对海康威视下获取的人员面部信息的获取和面部信息的处理

1.看到这儿,你一定是要先是获取到了人员的这两条数据,这儿只是获取到了图片的数据,然后如果你要是和人员绑定的话,那就接下来看,

{
    "picUri": "/pic?8dd685i46-e*9b649962225a--0a15b67eb3a78i7b5*=ids1*=idp2*=0d8t0pe*m5i11=4-3634fc91z20ds=4i32=",
    "serverIndexCode": "71d96061-9f20-4c46-b7c6-f75b7e5ca5e7"
}

2.如果你没有 就去调用这个接口

Java对接海康威视获取人员信息下的面部信息下载转成Base64_第1张图片

Java对接海康威视获取人员信息下的面部信息下载转成Base64_第2张图片

/api/resource/v2/person/personList

3.请求入参

{
    "pageNo": 1,
    "pageSize": 100
}

4.数据出参

{
    "code": "0",
    "msg": "SUCCESS",
    "data": {
        "total": 59,
        "pageNo": 1,
        "pageSize": 1,
        "list": [
            {
                "personId": "03fc803834c34004add606cc991bdd27",
                "personName": "qe",
                "gender": 1,
                "phone": "13333333333",
                "jobNo": "1555",
                "orgIndexCode": "c01ab7b0-46db-415f-aa0c-540b9d191bdd",
                "certificateType": 111,
                "certificateNo": "12313",
                "createTime": "2019-06-20T14:19:42.984+08:00",
                "updateTime": "2019-07-23T18:41:49.708+08:00",
                "orgPath": "@root000000@c01ab7b0-46db-415f-aa0c-540b9d191bdd@",
                "orgPathName": "@默认组织@sz@",
                "personPhoto": [
                    {
                        "personPhotoIndexCode": "1dd742ea-f220-4d63-bffa-fea82441bbba",
                        "picUri": "/pic?4dd569i90-e*615455166115m6ep=t=i1p*i=d1s*i=d3b*iedd0*9373cb46d-97105f--1544370za33s=1i15=",
                        "serverIndexCode": "a4b8e993-6d6a-43b0-b3ac-3b2dddf17a45",
                        "personId": "03fc803834c34004add606cc991bdd27"
                    }
                ]
            }
        ]
  }
}

5.获取到了数据之后,进行数据的解析,拿到personId、picUri和serverIndexCode,这三个字段 ,通过接口存入数据库

6.发送获取照片的请求

import com.alibaba.fastjson.JSONObject;
import com.hikvision.artemis.sdk.ArtemisHttpUtil;
import com.hikvision.artemis.sdk.config.ArtemisConfig;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import static com.hikvision.artemis.sdk.util.HttpUtil.wrapClient;
public class DownloadImageUtils {
public static String callPostImgs2(String picUri) throws Exception {
		ArtemisConfig config = new ArtemisConfig();
		config.setHost("IP"); // 代理API网关nginx服务器ip端口
		config.setAppKey("AK");  // 秘钥appkey
		config.setAppSecret("SK");// 秘钥appSecret
		final String getSecurityApi = "/artemis" + "/api/resource/v1/person/picture"; // 接口路径
		Map<String, String> path = new HashMap<String, String>(2) {
			{
				put("https://", getSecurityApi);
			}
		};

		JSONObject jsonBody = new JSONObject();
		jsonBody.put("serverIndexCode", "serverIndexCode的值");
		jsonBody.put("picUri", "pirUri的值");
		String body = jsonBody.toJSONString();
		//参数根据接口实际情况设置
//		HttpResponse result = ArtemisHttpUtil.doPostStringImgArtemis(config, path, body, query, null,"application/json",head);
		HttpResponse result = ArtemisHttpUtil.doPostStringImgArtemis(config, path, body, null, null,"application/json",null);
		try {
			HttpResponse resp = result;
			if (302==resp.getStatusLine().getStatusCode()) {
                /*
                获取图片数据保存到本地
                注:1.对于有时效的图片,必须尽快保存到本地
                   2.若无时效,则可以直接保存location,后续自行访问获取
                 */
				Header header=  resp.getFirstHeader("location");
				//如果在config中的信息不能用,就在这儿写
				//String pre = "https://";
				//String ipaddr="IP:PORT";
				//picUri的值 在入参
				String  newUrl =pre+ipaddr+picUri;
				HttpGet httpget = new HttpGet(newUrl);
				HttpClient httpClient = wrapClient(httpget.getURI().getScheme()+"://"+httpget.getURI().getHost());
				HttpResponse execute = httpClient.execute(httpget);
				HttpEntity entity = execute.getEntity();
				InputStream in = entity.getContent();
				System.out.println("in:"+in.toString());
				ImageTools.savePicToDisk(in);
			}else{
				System.out.println("下载出错");
			}

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

		return getSecurityApi;
	}
}

7调用转换成Base64的方法

ImageTools.savePicToDisk(in);

import sun.misc.BASE64Encoder;

import java.io.*;
import java.net.URL;

public class ImageTools {
 /**
     * 下载网络图片时,转成Base64
     * @param in
     */
    public static String savePicToDisk(InputStream in)  {
        ByteArrayOutputStream data = new ByteArrayOutputStream();
        try {
            byte[] buf = new byte[1024];
            int len = -1;
            while ((len = in.read(buf)) != -1) {
                data.write(buf, 0, len);
            }
            //关闭流
            data.close();
            BASE64Encoder encoder = new BASE64Encoder();
            String encode = encoder.encode(data.toByteArray());
            String result = encode.replaceAll("\\s", "");
//            System.out.println("faceData"+":"+result);
            return result;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return "0";
    }
}

8 调用上述方法

从数据库中获取的personId和picUri
调用方法

    String baseStr = DownloadImageUtils.callPostImgs2(picUri);

转成Base64后,存数据库时,我用的是MySQL,这个字段我设置的是mediumText。也可以用BLOB你就可以自己随意操作了。

你可能感兴趣的:(海康威视,后端,java)