Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to connect the face fusion function in Mini Program

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today, the editor will share with you the relevant knowledge points about how to access face fusion in Mini Program. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article, let's take a look at it.

Create an AI application

Log in to Tencent AI with https://ai.qq.com/cgi-bin/console_overview, create and check the relevant APIs.

Remember to copy APPID APPKEY

Use Java to access this function

Create a SpringMVC project that contains upload-related jar. Or the SpringBoot project. My backend is still improving and not completely open source. Please refer to the https://gitee.com/xshuai/xai project for details.

Java calls Tencent AI API. Handsome, has been packaged into SDK. Is also an open source https://gitee.com/xshuai/taip

If you use maven to build. Just bring in pom directly.

Cn.xsshometaip4.2.1

FaceMergeController (backend processing code)

Import java.util.Iterator;import org.apache.log4j.Logger;import org.springframework.context.annotation.Scope;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.multipart.MultipartFile;import org.springframework.web.multipart.MultipartHttpServletRequest;import cn.xsshome.taip.ptu.TAipPtu / * face fusion API * url: http://www.xxx.com/facemerge/uploadFM*/@Controller@RequestMapping(value="/facemerge")@Scope("prototype")public class FaceMergeController extends BaseController {private static final Logger logger = Logger.getLogger (FaceMergeController.class) / * face fusion * @ throws Exception * / @ RequestMapping (value= "/ uploadFM", method=RequestMethod.POST) public void UploadBDANIMAL () throws Exception {TAipPtu aipPtu = new TAipPtu ("APPID", "APPKEY"); String model = request.getParameter ("model"); logger.info ("model value is = =" + model) String model = request.getParameter ("model"); logger.info ("value of model is = =" + model); String result = ""; MultipartHttpServletRequest mpRequest = (MultipartHttpServletRequest) this.request; Iterator iter = mpRequest.getFileNames (); MultipartFile file = null While (iter.hasNext ()) {file = mpRequest.getFile ((String) iter.next ()); if ((file! = null) & & (file.getSize ()! = 0L)) {byte [] image = file.getBytes () String apiPtuResult = aipPtu.faceMerge (image,Integer.parseInt (model)); PrintUtil.printJson (this.response, apiPtuResult);} else {logger.error ("Please check whether the uploaded file is correct") Result = "{\" result\ ",\" FAIL\ ",\" msg\ ":\" Server deserted\ "}"; PrintUtil.printJson (this.response, result);}}

BaseController (base class)

Import java.util.Map;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.web.bind.annotation.ModelAttribute;/** * base class Controller * some parameters * Title: BaseController * @ author, * @ version 1.0 * / public class BaseController {public Map session; public String openId; public String errMsg; public String jsonParam; public String callback; protected HttpServletRequest request Protected HttpServletResponse response / * * each request will be accompanied by * @ param jsonParam * @ param callback * @ param openId * / @ ModelAttribute public void setReqAndRes (Map session, String openId, String errMsg, String jsonParam, String callback, HttpServletRequest request, HttpServletResponse response) {this.session = session This.openId = openId; this.errMsg = errMsg; this.jsonParam = jsonParam; this.callback = callback; this.request = request; this.response = response;} public Map getSession () {return session } public void setSession (Map session) {this.session = session;} public String getOpenId () {return openId;} public void setOpenId (String openId) {this.openId = openId;} public String getErrMsg () {return errMsg } public void setErrMsg (String errMsg) {this.errMsg = errMsg;} public String getJsonParam () {return jsonParam;} public void setJsonParam (String jsonParam) {this.jsonParam = jsonParam;} public String getCallback () {return callback } public void setCallback (String callback) {this.callback = callback;} public HttpServletRequest getRequest () {return request;} public void setRequest (HttpServletRequest request) {this.request = request;} public HttpServletResponse getResponse () {return response } public void setResponse (HttpServletResponse response) {this.response = response;} public String getRealPath (String path) {return request.getSession () .getServletContext () .getRealPath (path);}}

PrintUtil (response class)

Import java.io.IOException;import java.io.PrintWriter;import java.io.Writer;import javax.servlet.http.HttpServletResponse;/** * output result * @ author handsome, * * / public class PrintUtil {public static void printXml (HttpServletResponse response,String result) {try {response.setContentType ("text/xml; charset=UTF-8"); PrintWriter sos = response.getWriter () Sos.write (result); sos.flush (); sos.close ();} catch (IOException e) {/ / TODO Auto-generated catch block e.printStackTrace () }} / * json or jsonp * @ param callback * @ param response * @ param result * / public static void printJson (String callback,HttpServletResponse response,String result) {boolean jsonP = false If (callback! = null) {jsonP = true; response.setContentType ("text/javascript;charset=utf-8");} else {response.setContentType ("application/x-json;charset=utf-8") } try {Writer out = response.getWriter (); if (jsonP) {out.write (callback + "(")) } out.write (result.toString ()); if (jsonP) {out.write (");") } out.flush (); out.close ();} catch (Exception e) {/ / TODO Auto-generated catch block e.printStackTrace () }} public static void printJson (HttpServletResponse response,String result) {try {response.setCharacterEncoding ("UTF-8"); response.setContentType ("application/json; charset=utf-8") PrintWriter sos = response.getWriter (); sos.write (result); sos.flush (); sos.close ();} catch (IOException e) {/ / TODO Auto-generated catch block e.printStackTrace () These are all the contents of the article "how to access face Fusion in Mini Program". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report