服务端调用微信小程序OCR识别接口实现(下)

简介: 服务端调用微信小程序OCR识别接口实现

   2. controller层编写(分为api-controller)

api

package com.example.demo.api;
import com.example.demo.common.dto.RestControllerResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
/**
 * 小程序请求后端系统Api.
 *
 * @author : 小隐乐乐
 * @since : 2020/6/9 15:37
 */
@RestController
@RequestMapping("/api/vehicle")
public interface AppVehicleMiniApi {
  /**
   * 上传行驶证照片.
   *
   * @param img 图片
   * @return result
   */
  @PostMapping("upload-vehicle")
  RestControllerResult uploadVehicle(@RequestParam("file") MultipartFile img);
}

controller

package com.example.demo.controller;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.api.AppVehicleMiniApi;
import com.example.demo.common.dto.RestControllerResult;
import com.example.demo.service.AppVehicleService;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
/**
 1. 小程序请求后端系统完成功能Controller.
 2.
 3. @author : 小隐乐乐
 4. @since : 2020/6/9
 */
@RestController
public class AppVehicleMiniApiController implements AppVehicleMiniApi {
  private static final Logger logger = LoggerFactory.getLogger(AppVehicleMiniApiController.class);
  private static final String IMG_EMPTY = "上传照片为空";
  @Resource
  private AppVehicleService appVehicleService;
  @Override
  public RestControllerResult uploadVehicle(MultipartFile img) {
    logger.info("=======>行驶证上传<=======");
    RestControllerResult resultsDtoRestControllerResult = new RestControllerResult<>();
    if (img.isEmpty()) {
      logger.error(IMG_EMPTY);
      resultsDtoRestControllerResult.setSuccess(false);
      resultsDtoRestControllerResult.setCode(400);
      resultsDtoRestControllerResult.setErrorMsg(IMG_EMPTY);
      return resultsDtoRestControllerResult;
    }
    System.out.println(JSONObject.toJSONString(appVehicleService.ocrVehilce(img)));
    resultsDtoRestControllerResult.setSuccess(true);
    resultsDtoRestControllerResult.setCode(200);
    resultsDtoRestControllerResult.setData(appVehicleService.ocrVehilce(img));
    return resultsDtoRestControllerResult;
  }
}

3.service实现

package com.example.demo.service;
import org.springframework.web.multipart.MultipartFile;
/**
 * 行驶证图片上传服务接口.
 *
 * @author : 小隐乐乐
 * @since : 2020/6/9 15:50
 */
public interface AppVehicleService {
  /**
   * 识别行驶证.
   *
   * @param img 上传图片
   * @return 识别结果
   */
  Object ocrVehilce(MultipartFile img);
}
package com.example.demo.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.feign.AppVehicleFeign;
import com.example.demo.service.AppVehicleService;
import java.util.Map;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
/**
 * 行驶证照片服务.
 *
 * @author : 小隐乐乐
 * @since : 2020/6/9 1
package com.example.demo.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.feign.AppVehicleFeign;
import com.example.demo.service.AppVehicleService;
import java.util.Map;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
/**
 * 行驶证照片服务.
 *
 * @author : 小隐乐乐
 * @since : 2020/6/9 18:11
 */
@Service
public class AppVehicleServiceImpl implements AppVehicleService {
  private static final Logger logger = LoggerFactory.getLogger(AppVehicleServiceImpl.class);
  @Resource
  private AppVehicleFeign appVehicleFeign;
  @Override
  public Object ocrVehilce(MultipartFile img) {
    Object value = appVehicleFeign.getWeiXinToken();
    String s = JSONObject.toJSONString(value);
    Map<String, Object> map = (Map<String, Object>)JSONObject.parse(s);
    Object token = "";
    if (map.get("access_token") != null) {
      logger.info("最终token为" + map.get("access_token"));
      token = map.get("access_token");
    } else {
      //返回失败结果
      logger.error("微信接口服务获取token发生错误,错误代码 " + map.get("errcode"));
      logger.error("微信接口服务获取token发生错误,错误信息 " + map.get("errmsg"));
    }
    return appVehicleFeign.ocrVehicle(img,token.toString());
  }
}

  4.重点戏:Feign调用实现

package com.example.demo.feign;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;
/**
 * 行驶证处理feign.
 *
 * @author : 小隐乐乐
 * @since : 2020/6/9 17:03
 */
@FeignClient(value = "vehicle", fallbackFactory = AppVehicleFeignFactory.class, url = "${app.vehicle}")
public interface AppVehicleFeign {
  /**
   * 获取微信token.
   *
   * @return token
   */
  @GetMapping("/cgi-bin/token?grant_type=client_credential&appid=[小程序开发id]&secret=[微信密钥]")
  Object getWeiXinToken();
  /**
   * 识别行驶证.
   *
   * @param img 照片
   * @param token token
   * @return 识别结果
   */
  @PostMapping(value = "/cv/ocr/driving?type=photo&access_token={token}",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
  Object ocrVehicle(@RequestPart(value = "file") MultipartFile img,
      @PathVariable(name = "token") String token);
}
package com.example.demo.feign;
import feign.hystrix.FallbackFactory;
/**
 * 行驶证feign工厂.
 *
 * @author : 小隐乐乐
 * @since : 2020/6/9 17:10
 */
public class AppVehicleFeignFactory implements FallbackFactory<AppVehicleFeign> {
  @Override
  public AppVehicleFeign create(Throwable throwable) {
    return null;
  }
}

终于可以愉快地测试了!!!!!

启动应用,访问地址 http://localhost:8890/swagger3/index.html

image.png

干净、简洁、明了

点击上传接口,try it out,选择行驶证照片,执行

image.png

目录
相关文章
|
小程序
微信小程序中识别HTML标签的方法
微信小程序中识别HTML标签的方法
|
JSON 文字识别 小程序
UNIAPP小程序OCR识别
UNIAPP小程序OCR识别
874 0
UNIAPP小程序OCR识别
|
JSON 文字识别 小程序
微信小程序OCR插件,实现身份证、行驶证、银行卡、营业执照和驾驶证等识别
微信小程序OCR插件,实现身份证、行驶证、银行卡、营业执照和驾驶证等识别
3112 0
|
小程序
微信小程序中长按识别二维码
微信小程序中长按识别二维码
2155 0
|
小程序 JavaScript 前端开发
微信小程序:阿里云OSS直传实践-PHP实现服务端签名
微信小程序:阿里云OSS直传实践-PHP实现服务端签名
2519 0
微信小程序:阿里云OSS直传实践-PHP实现服务端签名
|
小程序
uniapp 微信小程序长按识别二维码,跳转小程序、个人微信
uniapp 微信小程序长按识别二维码,跳转小程序、个人微信
uniapp 微信小程序长按识别二维码,跳转小程序、个人微信
|
JSON 文字识别 小程序
UNIAPP小程序OCR识别
UNIAPP小程序OCR识别
1082 0
|
小程序
微信小程序中长按识别二维码
微信小程序中长按识别二维码
951 0
|
API
企业微信授权登录服务端API实战开发(1):企业微信环境部署
企业微信授权登录服务端API实战开发(1):企业微信环境部署
653 0
|
小程序 前端开发 API
微信小程序利用百度api达成植物识别
## 看完本文你可学会: - 如何使用百度智能云 - 如何在微信小程序中去接入百度api,达成植物识别的效果

热门文章

最新文章