七牛获取Token+百度人脸融合

这篇博客详细介绍了如何使用七牛云存储服务进行图片上传,并实现回调处理。首先,通过Java代码展示了获取七牛token的过程,然后阐述了回调函数的实现,包括从请求中获取参数、创建图书馆条目和Cosplay记录,以及调用FaceMergeUtils进行图像处理。当图像处理成功,将新图片URL保存并发送消息到所有客户端。整个流程涉及到了前后端交互、七牛云API使用和人脸识别技术。
摘要由CSDN通过智能技术生成

获取七牛token返回给前端

 public RestResult minGetCosplayToken(RequestBean model) {
       
        CosplayPerson cosplayPerson = cosplayPersonService.queryByOId(model.getPersonId(), model.getClientId());
        if (cosplayPerson == null) {
            return RestResult.error("cosplay area is not found");
        }
        String bucket = "wilssir";
        long expireSeconds = 600;   //过期时间
        String body = "clientId=" + cosplayPerson.getClientId() +
                "&nickName=" + "nickNamw" +
                "&userId=" + "userId" +
                "&personId=" + "personId" +
                "&key=$(key)" +
                "&width=$(imageInfo.width)" +
                "&height=$(imageInfo.height)";
        StringMap putPolicy = new StringMap();
        putPolicy.put("scope", Constant.getQiNiuBucket());
        putPolicy.put("callbackUrl", Constant.getQiNiuCallbackUrl());
        putPolicy.put("callbackBody", body);
        String token = QiNiuUtils.getQiNiuToken(bucket, expireSeconds, putPolicy);
        return RestResult.ok(token);
    }

回调

public RestResult callBack(HttpServletRequest request) {
        try {
            String clientId = request.getParameter("clientId");

            String userId = request.getParameter("userId");
            String personId = request.getParameter("personId");
            String key = request.getParameter("key");
            String width = request.getParameter("width");
            String height = request.getParameter("height");
            String nickName = request.getParameter("nickName");
            CosplayPerson person = cosplayPersonService.queryByOId(personId, null);
            if (person == null) {
                return RestResult.error("person not found");
            }
            // add library
            Library library = new Library();
            library.setOId(UUIDGenerator.getUUID());
            library.setUserId(userId);
            library.setName("图中换人");
            library.setTags("图中换人,用户上传");
            library.setModule("Cosplay");
            library.setSubject("User");
            library.setOldUrl(key);
            library.setWidth(Long.valueOf(width));
            library.setHeight(Long.valueOf(height));
            library.setDateline(TimeUtils.getCurrentUnixTime());
            library.setStatus(200L);

            // add cosplay
            Cosplay cosplay = new Cosplay();
            cosplay.setOId(UUIDGenerator.getUUID());
            cosplay.setName("baidu:v2.0");
            cosplay.setUserId(userId);
            cosplay.setDateline(TimeUtils.getCurrentUnixTime());
            cosplay.setSubject("picture");
            cosplay.setTargetId(person.getTargetId());
            cosplay.setTargetNumber(person.getTargetNumber());
            cosplay.setPersonId(person.getOId());
            cosplay.setPersonName(person.getName());
            cosplay.setOldImageUrl(person.getPartImageUrl());
            cosplay.setStartShowTime(TimeUtils.getCurrentUnixTime());
            cosplay.setEndShowTime(cosplay.getStartShowTime() + 60);
            cosplay.setIsStar(0L);// 不参与点赞活动
            cosplay.setStar(0L);
//            cosplay.setStarTime(0L);
//            cosplay.setStarEndTime(0L);
//            cosplay.setStarLimit(1L);
            // request baidu faceMerge
            RestResult restResult = FaceMergeUtils.faceMerge(person.getPartImageUrl(), key);
            if (restResult.getCode() != 200) {  //fail add errInfo
                cosplay.setStatus(400L);
                cosplay.setErrInfo(restResult.getData());
                cosplayService.add(cosplay);
                return restResult;
            }
            String data = (String) restResult.getData();
            cosplay.setStatus(200L);
            cosplay.setNewImageUrl(data);
            cosplayService.add(cosplay);
            library.setNewUrl(data);
            libraryService.add(library);
            Map<String, Object> map = new HashMap<>();
            map.put("cosplay", cosplay);
            //send socket
            MinUserSocket.SendMessageAll(JSON.toJSONString(map));
            String start = TimeUtils.unixToTime(cosplay.getStartShowTime(), "HH:mm:ss");
            String end = TimeUtils.unixToTime(cosplay.getEndShowTime(), "HH:mm:ss");
            map.put("msg", start + "-" + end);
            return RestResult.ok(map);
        } catch (Exception e) {
            e.printStackTrace();
            return RestResult.exception();
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值