目录


  • 一官方文档链接
  • 二官方java sdk
  • 三本地调用
  • 前端ajax跨域请求
  • 后端java请求腾讯接口
  • 注意事项
  • 官方sdk包截图


一、官方文档链接

http://open.youtu.qq.com/#/develop/api-makeup-merge

二、官方java sdk

https://github.com/TencentYouTu/java_sdk

另外官方还提供了php,, nodejs, python, c++

入口:http://open.youtu.qq.com/#/develop/tool-authentication

人脸融合代码python 人脸融合接口_java

三、本地调用

前端ajax跨域请求

前端需要给后端传入模版id,本地图片base64转码后的图片,应用编号

$.ajax({
      type:"get",    //请求方式
      async:true,    //是否异步
      url:"http://5f5bf1d1.ngrok.io/b-console/wechat/queryAppUserInfo",
      dataType:"jsonp",    //跨域json请求一定是jsonp
      jsonp: "callback",    //跨域请求的参数名,默认是callback
      data:{"modelId":'xxx',"imgBase64":"xxx","resImgType":'url',"appCd":"dlq"},    //请求参数
     beforeSend: function() {
         //请求前的处理
     },

     success: function(data) {
         //请求成功处理
         console.log(data);
        }
     },
     complete: function() {
         //请求完成的处理
         alert("complete");
     },
     error: function() {
         //请求出错处理
         alert("error");
     }

后端java请求腾讯接口

package com.wuxing.ballot.controller.youtu;

import java.io.IOException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.core.code.web.utils.RequestUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wuxing.ballot.dao.youtu.YoutuAppMapper;
import com.wuxing.ballot.entity.youtu.YoutuApp;
import com.wuxing.ballot.entity.youtu.YoutuAppExample;
import com.wuxing.ballot.img.ImgBase64Utils;
import com.wuxing.ballot.service.common.SuperService;
import com.wuxing.ballot.youtu.Youtu;
import com.wuxing.ballot.youtu.sign.YoutuSign;

@Controller
@RequestMapping("faceMerger")
public class FaceMergerController {

    @Resource 
    private SuperService superService;
    @RequestMapping(value="/merger",produces="text/html;charset=UTF-8;")
    @ResponseBody
    public String faceMerger(HttpServletRequest request,HttpServletResponse response) throws JSONException, KeyManagementException, NoSuchAlgorithmException, IOException{
        //定义返回值
        JSONObject jObject=new JSONObject();  //返回对象
        JSONObject code=new JSONObject();     //请求状态对象
        JSONObject data=new JSONObject();     //请求数据对象

        //获取请求参数
        String imgBase64=RequestUtils.getStringParameter(request, "imgBase64", "-1");//前台传入的base64转码图片
        String modelId=RequestUtils.getStringParameter(request, "modelId", "-1");    //模版id
        String appCd=RequestUtils.getStringParameter(request, "appCd", "-1");        //腾讯AI应用编号,自己在数据库中设定的,目的是通过这个编号访问对应的应用号
        String resImgType=RequestUtils.getStringParameter(request, "resImgType", "-1"); //返回图片方式,默认为url
        String callback=RequestUtils.getStringParameter(request, "callback", "-1");     //ajax跨域请求自带参数

        //校验参数
        if("-1".equals(imgBase64) || "-1".equals(modelId) || "-1".equals(appCd) || "-1".equals(resImgType)){
            code.put("code", "1001");
            code.put("msg", "未获取到足够参数");
            code.put("params", "{imgBase64Length:"+imgBase64.length()+",modelId:"+modelId+",appCd:"+appCd+",resImgType:"+resImgType+",callback:"+callback+"}");
            jObject.put("code",code);
            jObject.put("data", data);
            return callback+"("+jObject.toString()+")";  
        }

        //根据appCd查询数据库,获取appId,appSecret,appKey
        YoutuAppExample youtuExample=new YoutuAppExample();
        youtuExample.createCriteria().andAppCdEqualTo(appCd);
        List<YoutuApp> youtuList = superService.getMapper(YoutuAppMapper.class).selectByExample(youtuExample);
        //校验是否查询到数据
        if(youtuList == null || youtuList.size() ==0){
            code.put("code", "1002");
            code.put("msg", "未获取到应用配置信息,请到数据库中配置!");
            code.put("params", "{imgBase64Length:"+imgBase64.length()+",modelId:"+modelId+",appCd:"+appCd+",resImgType:"+resImgType+"}");
            jObject.put("code",code);
            jObject.put("data", data);
            return callback+"("+jObject.toString()+")";  
        }

        //通过app配置信息创建官方提供的sdk中的Youtu对象
        YoutuApp youtuApp=youtuList.get(0);
        Youtu youtu = new Youtu(youtuApp.getAppId(), youtuApp.getSecretId(), youtuApp.getSecretKey(),Youtu.API_YOUTU_IMG_MERGER_END_POINT);

           //获取签名
           StringBuffer mySign=new StringBuffer();
           YoutuSign.appSign(youtuApp.getAppId(), youtuApp.getSecretId(), youtuApp.getSecretKey(), (System.currentTimeMillis() / 1000)+100, youtuApp.getAppRegisterUser(), mySign);

           /*封装请求参数,格式:
            * {
                "img_data": "xxxx...", // base64 编码后的输入图像
                "rsp_img_type": "url",
                "opdata": [{           // 注意opdata是一个数组
                    "cmd": "doFaceMerge",
                    "params": {
                        "model_id": "snow" // 通用模板id
                    }
                }]
           }
            */
           //定义请求参数对象:requestData
           JSONObject requestData=new JSONObject();
           //定义请求参数内部数组对象:opdata
           JSONArray opdata=new JSONArray();
           //定义opdata内部对象opdataElement
           JSONObject opdataElement=new JSONObject();
           //定义opdataElement内部对象params
           JSONObject params=new JSONObject();

           //按照从内到外的方式给请求参数赋值
           //给params对象赋值
           params.put("model_id", modelId);
           //给opdataElement赋值
           opdataElement.put("cmd", "doFaceMerge");
           opdataElement.put("params", params);
           //给opdata赋值
           opdata.put(opdataElement);
           //封装请求参数
           requestData.put("img_data", rewrite(imgBase64));
           requestData.put("rsp_img_type", resImgType);
           requestData.put("opdata", opdata);

           //调用sdk接口请求优图
           JSONObject res=youtu.faceMerger(requestData);
           System.out.println(res.toString());      

           return callback+"("+res.toString()+")";  
      }
    }

注意事项

官方sdk中的Youtu对象并没有人脸融合方法,需要自己添加进去,找到Youtu类

//添加全局变量人脸融合请求接口地址
public final static  String API_YOUTU_IMG_MERGER_END_POINT = "http://api.youtu.qq.com/cgi-bin/";

//添加人脸融合请求方法
public JSONObject faceMerger(JSONObject data) throws KeyManagementException, NoSuchAlgorithmException, IOException, JSONException{
    JSONObject respose =m_not_use_https?SendHttpRequest(data, "pitu_open_access_for_youtu.fcg"):SendHttpsRequest(data, "pitu_open_access_for_youtu.fcg");
    return respose;
}

官方sdk包截图

人脸融合代码python 人脸融合接口_人脸融合_02