阿里云人脸识别Java调用示例参考

本文涉及的产品
密钥管理服务KMS,1000个密钥,100个凭据,1个月
简介: 目前阿里云提供了关于人脸识别的调用接口,提供的接口包括人脸检测定位、人脸属性识别及人脸对比三个API,用户可以基于Rest API的调用说明完成调用操作。下面给出使用Java语言分别使用网络图片和本地图片调用人脸属性API的示例。

概述

目前阿里云提供了关于人脸识别的调用接口,提供的接口包括人脸检测定位、人脸属性识别及人脸对比三个API,用户可以基于[Rest API]的调用说明完成调用操作。下面给出使用Java语言分别使用网络图片和本地图片调用人脸属性API的示例。

准备工作

1、拥有阿里云账号并开通了人脸识别服务,具体参考官方[链接];
2、获取阿里云账户的AccessKey ID和Access Key Secret;
3、获取服务调用的地址,如果不清楚,可以直接到人脸识别API调试部分获取,也可以使用其完成快速测试。
_API

Java Code Sample

使用网络图片Code
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Encoder;
import javax.crypto.Mac;

//@SuppressWarnings("restriction")
public class FaceDemo1 {
   
    /*
     * 计算MD5+BASE64
     */
    public static String MD5Base64(String s) {
   
        if (s == null)
            return null;
        String encodeStr = "";
        byte[] utfBytes = s.getBytes();
        MessageDigest mdTemp;
        try {
   
            mdTemp = MessageDigest.getInstance("MD5");
            mdTemp.update(utfBytes);
            byte[] md5Bytes = mdTemp.digest();
            BASE64Encoder b64Encoder = new BASE64Encoder();
            encodeStr = b64Encoder.encode(md5Bytes);
        } catch (Exception e) {
   
            throw new Error("Failed to generate MD5 : " + e.getMessage());
        }
        return encodeStr;
    }
    /*
     * 计算 HMAC-SHA1
     */
    public static String HMACSha1(String data, String key) {
   
        String result;
        try {
   
            SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), "HmacSHA1");
            Mac mac = Mac.getInstance("HmacSHA1");
            mac.init(signingKey);
            byte[] rawHmac = mac.doFinal(data.getBytes());
            result = (new BASE64Encoder()).encode(rawHmac);
        } catch (Exception e) {
   
            throw new Error("Failed to generate HMAC : " + e.getMessage());
        }
        return result;
    }
    /*
     * 等同于javaScript中的 new Date().toUTCString();
     */
    public static String toGMTString(Date date) {
   
        SimpleDateFormat df = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z", Locale.UK);
        df.setTimeZone(new java.util.SimpleTimeZone(0, "GMT"));
        return df.format(date);
    }
    /*
     * 发送POST请求
     */
    public static String sendPost(String url, String body, String ak_id, String ak_secret) throws Exception {
   
        PrintWriter out = null;
        BufferedReader in = null;
        String result = "";
        int statusCode = 200;
        try {
   
            URL realUrl = new URL(url);
            /*
             * http header 参数
             */
            String method = "POST";
            String accept = "application/json";
            String content_type = "application/json";
            String path = realUrl.getFile();
            String date = toGMTString(new Date());
            // 1.对body做MD5+BASE64加密
            String bodyMd5 = MD5Base64(body);
            String stringToSign = method + "\n" + accept + "\n" + bodyMd5 + "\n" + content_type + "\n" + date + "\n"
                    + path;
            // 2.计算 HMAC-SHA1
            String signature = HMACSha1(stringToSign, ak_secret);
            // 3.得到 authorization header
            String authHeader = "Dataplus " + ak_id + ":" + signature;
            // 打开和URL之间的连接
            URLConnection conn = realUrl.openConnection();
            // 设置通用的请求属性
            conn.setRequestProperty("accept", accept);
            conn.setRequestProperty("content-type", content_type);
            conn.setRequestProperty("date", date);
            conn.setRequestProperty("Authorization", authHeader);
            // 发送POST请求必须设置如下两行
            conn.setDoOutput(true);
            conn.setDoInput(true);
            // 获取URLConnection对象对应的输出流
            out = new PrintWriter(conn.getOutputStream());
            // 发送请求参数
            out.print(body);
            // flush输出流的缓冲
            out.flush();
            // 定义BufferedReader输入流来读取URL的响应
            statusCode = ((HttpURLConnection)conn).getResponseCode();
            if(statusCode != 200) {
   
                in = new BufferedReader(new InputStreamReader(((HttpURLConnection)conn).getErrorStream()));
            } else {
   
                in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            }
            String line;
            while ((line = in.readLine()) != null) {
   
                result += line;
            }
        } catch (Exception e) {
   
            e.printStackTrace();
        } finally {
   
            try {
   
                if (out != null) {
   
                    out.close();
                }
                if (in != null) {
   
                    in.close();
                }
            } catch (IOException ex) {
   
                ex.printStackTrace();
            }
        }
        if (statusCode != 200) {
   
            throw new IOException("\nHttp StatusCode: "+ statusCode + "\nErrorMessage: " + result);
        }
        return result;
    }
    /*
     * GET请求
     */
    public static String sendGet(String url, String ak_id, String ak_secret) throws Exception {
   
        String result = "";
        BufferedReader in = null;
        int statusCode = 200;
        try {
   
            URL realUrl = new URL(url);
            /*
             * http header 参数
             */
            String method = "GET";
            String accept = "application/json";
            String content_type = "application/json";
            String path = realUrl.getFile();
            String date = toGMTString(new Date());
            // 1.对body做MD5+BASE64加密
            // String bodyMd5 = MD5Base64(body);
            String stringToSign = method + "\n" + accept + "\n" + "" + "\n" + content_type + "\n" + date + "\n" + path;
            // 2.计算 HMAC-SHA1
            String signature = HMACSha1(stringToSign, ak_secret);
            // 3.得到 authorization header
            String authHeader = "Dataplus " + ak_id + ":" + signature;
            // 打开和URL之间的连接
            URLConnection connection = realUrl.openConnection();
            // 设置通用的请求属性
            connection.setRequestProperty("accept", accept);
            connection.setRequestProperty("content-type", content_type);
            connection.setRequestProperty("date", date);
            connection.setRequestProperty("Authorization", authHeader);
            connection.setRequestProperty("Connection", "keep-alive");
            // 建立实际的连接
            connection.connect();
            // 定义 BufferedReader输入流来读取URL的响应
            statusCode = ((HttpURLConnection)connection).getResponseCode();
            if(statusCode != 200) {
   
                in = new BufferedReader(new InputStreamReader(((HttpURLConnection)connection).getErrorStream()));
            } else {
   
                in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            }
            String line;
            while ((line = in.readLine()) != null) {
   
                result += line;
            }
        } catch (Exception e) {
   
            e.printStackTrace();
        } finally {
   
            try {
   
                if (in != null) {
   
                    in.close();
                }
            } catch (Exception e) {
   
                e.printStackTrace();
            }
        }
        if (statusCode != 200) {
   
            throw new IOException("\nHttp StatusCode: "+ statusCode + "\nErrorMessage: " + result);
        }
        return result;
    }
    public static void main(String[] args) throws Exception {
   
        // 发送POST请求示例
            System.out.println(i);
            String ak_id = "******"; //用户ak
            String ak_secret = "******"; // 用户ak_secret
            String url = "https://dtplus-cn-shanghai.data.aliyuncs.com/face/attribute";
            String body = "{\"type\": \"0\", \"image_url\":\"https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1544608444&di=54513735567736d0eaeba7a13aa0421c&src=http://img5q.duitang.com/uploads/item/201411/23/20141123205812_Bircn.jpeg\"}";
            System.out.println("response body:" + sendPost(url, body, ak_id, ak_secret));
    }
}
使用本地图片Code
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Encoder;
import javax.crypto.Mac;

//@SuppressWarnings("restriction")
public class FaceDemo2 {
   
    /*
     * 计算MD5+BASE64
     */
    public static String MD5Base64(String s) {
   
        if (s == null)
            return null;
        String encodeStr = "";
        byte[] utfBytes = s.getBytes();
        MessageDigest mdTemp;
        try {
   
            mdTemp = MessageDigest.getInstance("MD5");
            mdTemp.update(utfBytes);
            byte[] md5Bytes = mdTemp.digest();
            BASE64Encoder b64Encoder = new BASE64Encoder();
            encodeStr = b64Encoder.encode(md5Bytes);
        } catch (Exception e) {
   
            throw new Error("Failed to generate MD5 : " + e.getMessage());
        }
        return encodeStr;
    }
    /*
     * 计算 HMAC-SHA1
     */
    public static String HMACSha1(String data, String key) {
   
        String result;
        try {
   
            SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), "HmacSHA1");
            Mac mac = Mac.getInstance("HmacSHA1");
            mac.init(signingKey);
            byte[] rawHmac = mac.doFinal(data.getBytes());
            result = (new BASE64Encoder()).encode(rawHmac);
        } catch (Exception e) {
   
            throw new Error("Failed to generate HMAC : " + e.getMessage());
        }
        return result;
    }
    /*
     * 等同于javaScript中的 new Date().toUTCString();
     */
    public static String toGMTString(Date date) {
   
        SimpleDateFormat df = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z", Locale.UK);
        df.setTimeZone(new java.util.SimpleTimeZone(0, "GMT"));
        return df.format(date);
    }
    /*
     * 发送POST请求
     */
    public static String sendPost(String url, String body, String ak_id, String ak_secret) throws Exception {
   
        PrintWriter out = null;
        BufferedReader in = null;
        String result = "";
        int statusCode = 200;
        try {
   
            URL realUrl = new URL(url);
            /*
             * http header 参数
             */
            String method = "POST";
            String accept = "application/json";
            String content_type = "application/json";

//            String content_type = "application/octet-stream";

            String path = realUrl.getFile();
            String date = toGMTString(new Date());
            // 1.对body做MD5+BASE64加密
            String bodyMd5 = MD5Base64(body);
            String stringToSign = method + "\n" + accept + "\n" + bodyMd5 + "\n" + content_type + "\n" + date + "\n"
                    + path;
            // 2.计算 HMAC-SHA1
            String signature = HMACSha1(stringToSign, ak_secret);
            // 3.得到 authorization header
            String authHeader = "Dataplus " + ak_id + ":" + signature;
            // 打开和URL之间的连接
            URLConnection conn = realUrl.openConnection();
            // 设置通用的请求属性
            conn.setRequestProperty("accept", accept);
            conn.setRequestProperty("content-type", content_type);
            conn.setRequestProperty("date", date);
            conn.setRequestProperty("Authorization", authHeader);
            // 发送POST请求必须设置如下两行
            conn.setDoOutput(true);
            conn.setDoInput(true);
            // 获取URLConnection对象对应的输出流
            out = new PrintWriter(conn.getOutputStream());
            // 发送请求参数
            out.print(body);
            // flush输出流的缓冲
            out.flush();
            // 定义BufferedReader输入流来读取URL的响应
            statusCode = ((HttpURLConnection)conn).getResponseCode();
            if(statusCode != 200) {
   
                in = new BufferedReader(new InputStreamReader(((HttpURLConnection)conn).getErrorStream()));
            } else {
   
                in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            }
            String line;
            while ((line = in.readLine()) != null) {
   
                result += line;
            }
        } catch (Exception e) {
   
            e.printStackTrace();
        } finally {
   
            try {
   
                if (out != null) {
   
                    out.close();
                }
                if (in != null) {
   
                    in.close();
                }
            } catch (IOException ex) {
   
                ex.printStackTrace();
            }
        }
        if (statusCode != 200) {
   
            throw new IOException("\nHttp StatusCode: "+ statusCode + "\nErrorMessage: " + result);
        }
        return result;
    }
    /*
     * GET请求
     */
    public static String sendGet(String url, String ak_id, String ak_secret) throws Exception {
   
        String result = "";
        BufferedReader in = null;
        int statusCode = 200;
        try {
   
            URL realUrl = new URL(url);
            /*
             * http header 参数
             */
            String method = "GET";
            String accept = "application/json";
            String content_type = "application/json";

//            String content_type = "application/octet-stream";
            String path = realUrl.getFile();
            String date = toGMTString(new Date());
            // 1.对body做MD5+BASE64加密
            // String bodyMd5 = MD5Base64(body);
            String stringToSign = method + "\n" + accept + "\n" + "" + "\n" + content_type + "\n" + date + "\n" + path;
            // 2.计算 HMAC-SHA1
            String signature = HMACSha1(stringToSign, ak_secret);
            // 3.得到 authorization header
            String authHeader = "Dataplus " + ak_id + ":" + signature;
            // 打开和URL之间的连接
            URLConnection connection = realUrl.openConnection();
            // 设置通用的请求属性
            connection.setRequestProperty("accept", accept);
            connection.setRequestProperty("content-type", content_type);
            connection.setRequestProperty("date", date);
            connection.setRequestProperty("Authorization", authHeader);
            connection.setRequestProperty("Connection", "keep-alive");
            // 建立实际的连接
            connection.connect();
            // 定义 BufferedReader输入流来读取URL的响应
            statusCode = ((HttpURLConnection)connection).getResponseCode();
            if(statusCode != 200) {
   
                in = new BufferedReader(new InputStreamReader(((HttpURLConnection)connection).getErrorStream()));
            } else {
   
                in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            }
            String line;
            while ((line = in.readLine()) != null) {
   
                result += line;
            }
        } catch (Exception e) {
   
            e.printStackTrace();
        } finally {
   
            try {
   
                if (in != null) {
   
                    in.close();
                }
            } catch (Exception e) {
   
                e.printStackTrace();
            }
        }
        if (statusCode != 200) {
   
            throw new IOException("\nHttp StatusCode: "+ statusCode + "\nErrorMessage: " + result);
        }
        return result;
    }
    public static void main(String[] args) throws Exception {
   
        // 发送POST请求示例
        String ak_id = "******"; //用户ak
        String ak_secret = "******"; // 用户ak_secret
        String url = "https://dtplus-cn-shanghai.data.aliyuncs.com/face/attribute";

        //上传本地图片
        // Request body
        String pic_path = "C:\\Users\\****\\Desktop\\timg.jpg";//本地图片的路径

        File picBase64 = new File(pic_path);
        String pic = encodeImageToBase64(picBase64);
        //提出base64编码的换行符问题
        String data = pic.replaceAll("[\\s*\t\n\r]", "");
        data = "'" + data + "'";
        String body = "{\"type\": "+1+", \"content\": "+data+"}";
        System.out.println("response body:" + sendPost(url, body, ak_id, ak_secret));
    }

    /**
     * 将本地图片编码为base64
     *
     * @param file
     * @return
     * @throws Exception
     */
    public static String encodeImageToBase64(File file) throws Exception {
   
        //将图片文件转化为字节数组字符串,并对其进行Base64编码处理
//        loggerger.info("图片的路径为:" + file.getAbsolutePath());
        InputStream in = null;
        byte[] data = null;
        //读取图片字节数组
        try {
   
            in = new FileInputStream(file);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException e) {
   
            e.printStackTrace();
            throw new Exception("图片上传失败,请联系客服!");
        }
        //对字节数组Base64编码
        BASE64Encoder encoder = new BASE64Encoder();
        String base64 = encoder.encode(data);
        return base64;//返回Base64编码过的字节数组字符串
    }
}
人脸比对示例使用本地图片Code
import sun.misc.BASE64Encoder;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

//@SuppressWarnings("restriction")
public class FaceDemoVerify {
   
    /*
     * 计算MD5+BASE64
     */
    public static String MD5Base64(String s) {
   
        if (s == null)
            return null;
        String encodeStr = "";
        byte[] utfBytes = s.getBytes();
        MessageDigest mdTemp;
        try {
   
            mdTemp = MessageDigest.getInstance("MD5");
            mdTemp.update(utfBytes);
            byte[] md5Bytes = mdTemp.digest();
            BASE64Encoder b64Encoder = new BASE64Encoder();
            encodeStr = b64Encoder.encode(md5Bytes);
        } catch (Exception e) {
   
            throw new Error("Failed to generate MD5 : " + e.getMessage());
        }
        return encodeStr;
    }
    /*
     * 计算 HMAC-SHA1
     */
    public static String HMACSha1(String data, String key) {
   
        String result;
        try {
   
            SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), "HmacSHA1");
            Mac mac = Mac.getInstance("HmacSHA1");
            mac.init(signingKey);
            byte[] rawHmac = mac.doFinal(data.getBytes());
            result = (new BASE64Encoder()).encode(rawHmac);
        } catch (Exception e) {
   
            throw new Error("Failed to generate HMAC : " + e.getMessage());
        }
        return result;
    }
    /*
     * 等同于javaScript中的 new Date().toUTCString();
     */
    public static String toGMTString(Date date) {
   
        SimpleDateFormat df = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z", Locale.UK);
        df.setTimeZone(new java.util.SimpleTimeZone(0, "GMT"));
        return df.format(date);
    }
    /*
     * 发送POST请求
     */
    public static String sendPost(String url, String body, String ak_id, String ak_secret) throws Exception {
   
        PrintWriter out = null;
        BufferedReader in = null;
        String result = "";
        int statusCode = 200;
        try {
   
            URL realUrl = new URL(url);
            /*
             * http header 参数
             */
            String method = "POST";
            String accept = "application/json";
            String content_type = "application/json";

//            String content_type = "application/octet-stream";

            String path = realUrl.getFile();
            String date = toGMTString(new Date());
            // 1.对body做MD5+BASE64加密
            String bodyMd5 = MD5Base64(body);
            String stringToSign = method + "\n" + accept + "\n" + bodyMd5 + "\n" + content_type + "\n" + date + "\n"
                    + path;
            // 2.计算 HMAC-SHA1
            String signature = HMACSha1(stringToSign, ak_secret);
            // 3.得到 authorization header
            String authHeader = "Dataplus " + ak_id + ":" + signature;
            // 打开和URL之间的连接
            URLConnection conn = realUrl.openConnection();
            // 设置通用的请求属性
            conn.setRequestProperty("accept", accept);
            conn.setRequestProperty("content-type", content_type);
            conn.setRequestProperty("date", date);
            conn.setRequestProperty("Authorization", authHeader);
            // 发送POST请求必须设置如下两行
            conn.setDoOutput(true);
            conn.setDoInput(true);
            // 获取URLConnection对象对应的输出流
            out = new PrintWriter(conn.getOutputStream());
            // 发送请求参数
            out.print(body);
            // flush输出流的缓冲
            out.flush();
            // 定义BufferedReader输入流来读取URL的响应
            statusCode = ((HttpURLConnection)conn).getResponseCode();
            if(statusCode != 200) {
   
                in = new BufferedReader(new InputStreamReader(((HttpURLConnection)conn).getErrorStream()));
            } else {
   
                in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            }
            String line;
            while ((line = in.readLine()) != null) {
   
                result += line;
            }
        } catch (Exception e) {
   
            e.printStackTrace();
        } finally {
   
            try {
   
                if (out != null) {
   
                    out.close();
                }
                if (in != null) {
   
                    in.close();
                }
            } catch (IOException ex) {
   
                ex.printStackTrace();
            }
        }
        if (statusCode != 200) {
   
            throw new IOException("\nHttp StatusCode: "+ statusCode + "\nErrorMessage: " + result);
        }
        return result;
    }
    /*
     * GET请求
     */
    public static String sendGet(String url, String ak_id, String ak_secret) throws Exception {
   
        String result = "";
        BufferedReader in = null;
        int statusCode = 200;
        try {
   
            URL realUrl = new URL(url);
            /*
             * http header 参数
             */
            String method = "GET";
            String accept = "application/json";
            String content_type = "application/json";

//            String content_type = "application/octet-stream";
            String path = realUrl.getFile();
            String date = toGMTString(new Date());
            // 1.对body做MD5+BASE64加密
            // String bodyMd5 = MD5Base64(body);
            String stringToSign = method + "\n" + accept + "\n" + "" + "\n" + content_type + "\n" + date + "\n" + path;
            // 2.计算 HMAC-SHA1
            String signature = HMACSha1(stringToSign, ak_secret);
            // 3.得到 authorization header
            String authHeader = "Dataplus " + ak_id + ":" + signature;
            // 打开和URL之间的连接
            URLConnection connection = realUrl.openConnection();
            // 设置通用的请求属性
            connection.setRequestProperty("accept", accept);
            connection.setRequestProperty("content-type", content_type);
            connection.setRequestProperty("date", date);
            connection.setRequestProperty("Authorization", authHeader);
            connection.setRequestProperty("Connection", "keep-alive");
            // 建立实际的连接
            connection.connect();
            // 定义 BufferedReader输入流来读取URL的响应
            statusCode = ((HttpURLConnection)connection).getResponseCode();
            if(statusCode != 200) {
   
                in = new BufferedReader(new InputStreamReader(((HttpURLConnection)connection).getErrorStream()));
            } else {
   
                in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            }
            String line;
            while ((line = in.readLine()) != null) {
   
                result += line;
            }
        } catch (Exception e) {
   
            e.printStackTrace();
        } finally {
   
            try {
   
                if (in != null) {
   
                    in.close();
                }
            } catch (Exception e) {
   
                e.printStackTrace();
            }
        }
        if (statusCode != 200) {
   
            throw new IOException("\nHttp StatusCode: "+ statusCode + "\nErrorMessage: " + result);
        }
        return result;
    }
    public static void main(String[] args) throws Exception {
   
        // 发送POST请求示例
        String ak_id = "********"; //用户ak
        String ak_secret = "********"; // 用户ak_secret
        String url = "https://dtplus-cn-shanghai.data.aliyuncs.com/face/verify";

        //上传本地图片
        // Request body
        String pic_path = "D:\\picturetest\\timg.jpg";//本地图片的路径

        File picBase64 = new File(pic_path);
        String pic = encodeImageToBase64(picBase64);
        //提出base64编码的换行符问题
        String data = pic.replaceAll("[\\s*\t\n\r]", "");
        data = "'" + data + "'";
        String body = "{\"type\":1,\n" +
                "\"content_1\":"+data+",\n" +
                "\"content_2\":"+data+"\n" +
                "}";
        System.out.println("response body:" + sendPost(url, body, ak_id, ak_secret));
    }

    /**
     * 将本地图片编码为base64
     *
     * @param file
     * @return
     * @throws Exception
     */
    public static String encodeImageToBase64(File file) throws Exception {
   
        //将图片文件转化为字节数组字符串,并对其进行Base64编码处理
//        loggerger.info("图片的路径为:" + file.getAbsolutePath());
        InputStream in = null;
        byte[] data = null;
        //读取图片字节数组
        try {
   
            in = new FileInputStream(file);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException e) {
   
            e.printStackTrace();
            throw new Exception("图片上传失败,请联系客服!");
        }
        //对字节数组Base64编码
        BASE64Encoder encoder = new BASE64Encoder();
        String base64 = encoder.encode(data);
        return base64;//返回Base64编码过的字节数组字符串
    }
}

注意事项

关于调用本地图片的问题,特别需要注意关于base64编码默认添加了换行符的问题,需要剔除,请求body的具体格式也需要注意,具体参考示例代码。

参考链接

[API校验规范]

[人脸识别错误码]

相关文章
|
Java API 计算机视觉
阿里云新版人脸识别Java使用示例教程
之前阿里云人脸识别只提供人脸检测,人脸属性及人脸对比三个API接口,关于这方面的介绍及使用细节,可以参考阿里云人脸识别使用流程简介,之前使用的服务地址为:dtplus-cn-shanghai.data.aliyuncs.com。目前新版本加入了1:N人脸查找的功能,新版本还处于公测阶段,服务地址:face.cn-shanghai.aliyuncs.com。下面主要介绍如何使用新版本的地址调用之前的三个API的功能。
2572 0
|
测试技术 API C#
阿里云新版人脸识别 C# 使用示例教程
前面分别介绍了新版人脸识别服务接口的开通、JAVA SDK对通用人脸识别通用方法的调用及1:N人脸识别接口的调用,可以参考阿里云人脸识别公测使用说明与阿里云人脸识别 1:N 使用简明示例,本文主要介绍如何使用C# 程序调用阿里云新版人脸识别接口。
4030 1
阿里云新版人脸识别 C# 使用示例教程
|
Java API 计算机视觉
阿里云人脸识别Java调用示例参考
目前阿里云提供了关于人脸识别的调用接口,提供的接口包括人脸检测定位、人脸属性识别及人脸对比三个API,用户可以基于Rest API的调用说明完成调用操作。下面给出使用Java语言分别使用网络图片和本地图片调用人脸属性API的示例。
阿里云人脸识别Java调用示例参考
|
Java 网络安全 PHP
阿里云人脸识别PHP调用示例参考
本文主要介绍PHP调用阿里云人脸识别服务,参数等的获取参考阿里云人脸识别使用流程简介。
阿里云人脸识别PHP调用示例参考
|
Java 计算机视觉 API
阿里云人脸识别 1:N 使用简明示例
目前人脸识别1:N功能已经正式商业化。下面主要演示1:N 服务的开通及Java程序的调用测试。
阿里云人脸识别 1:N 使用简明示例
|
JavaScript 网络协议 物联网
阿里云物联网平台IoT Studio调用人脸识别服务示例
目前IoT Studio服务开发模块提供了云市场人脸识别API的调用模块,但是还没有直接调用阿里云人脸识别API的方法,本文从设备端开始上传图片到oss服务,然后通过上行消息传递图片URL,使用NodeJS节点进行阿里云人脸识别服务的调用,将调用的结果通过云数据库MySQL进行存储。
956 0
阿里云物联网平台IoT Studio调用人脸识别服务示例
|
开发工具 计算机视觉 Python
阿里云新版人脸识别 Python 使用示例教程
前面分别介绍了新版人脸识别服务接口的开通、JAVA SDK对通用人脸识别通用方法的调用、C# SDK使用方法及1:N人脸识别接口的调用,可以参考阿里云人脸识别公测使用说明、阿里云人脸识别 1:N 使用简明示例及阿里云新版人脸识别 C# 使用示例教程,本文主要介绍如何使用Python程序调用阿里云新版人脸识别接口。
465 0
阿里云新版人脸识别 Python 使用示例教程
|
C# 开发工具 计算机视觉
阿里云新版人脸识别 C# 使用示例教程
前面分别介绍了新版人脸识别服务接口的开通、JAVA SDK对通用人脸识别通用方法的调用及1:N人脸识别接口的调用,可以参考阿里云人脸识别公测使用说明与阿里云人脸识别 1:N 使用简明示例,本文主要介绍如何使用C# 程序调用阿里云新版人脸识别接口。
702 0
阿里云新版人脸识别 C# 使用示例教程
|
JSON JavaScript 数据安全/隐私保护
阿里云老版人脸识别NodeJS调用示例参考
前面分别给出了关于阿里云人脸识别Java调用示例参考、阿里云人脸识别C#调用示例参考、阿里云人脸识别Python3调用示例参考 、阿里云人脸识别PHP调用示例参考。本文主要介绍DodeJS调用阿里云人脸识别服务,参数等的获取参考阿里云人脸识别使用流程简介。
486 0
阿里云老版人脸识别NodeJS调用示例参考
|
JSON API PHP
阿里云新版人脸识别 PHP 使用示例教程
本篇博客是对前面多语言开发阿里云新版人脸识别的续,主要介绍使用PHP SDK调用人脸识别的接口。
3148 0

热门文章

最新文章

下一篇
无影云桌面