在华文云提供的文档基础上进行了简单的整理,需要引入华为相关的jar包
import com.oss.oss_demo.util.OcrClientToken;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.net.URISyntaxException;
@RestController
@RequestMapping("/huaweicloud/ocr")
@Api(tags = "华为云 文字识别")
public class HWOcrController {
private String Username="";
private String Password="";
private String Domainname=""; //if it's not IAM user the Domain_name is the same with username
private String Regionname="cn-north-1";
private String HttpEndpoint="ocr.cn-north-1.myhuaweicloud.com";
@PostMapping(value ="getIdCard")
@ApiOperation("身份证识别")
@ApiImplicitParam(name ="imagePath",value ="图片路径",required = true)
public String getIdCard(String imagePath) throws InterruptedException, IOException, URISyntaxException {
OcrClientToken ocrClient= new OcrClientToken(Domainname,Username,Password,Regionname,HttpEndpoint);
HttpResponse response=ocrClient.requestOcrServiceBase64("/v1.0/ocr/id-card", imagePath);
System.out.println(response);
String content = IOUtils.toString(response.getEntity().getContent(), "utf-8");
System.out.println(content);
return content;
}
@PostMapping(value ="getDriverLicense")
@ApiOperation("驾驶证识别")
@ApiImplicitParam(name ="imagePath",value ="图片路径",required = true)
public String getDriverLicense(String imagePath) throws InterruptedException, IOException, URISyntaxException {
OcrClientToken ocrClient= new OcrClientToken(Domainname,Username,Password,Regionname,HttpEndpoint);
HttpResponse response=ocrClient.requestOcrServiceBase64("/v1.0/ocr/driver-license", imagePath);
System.out.println(response);
String content = IOUtils.toString(response.getEntity().getContent(), "utf-8");
System.out.println(content);
return content;
}
@PostMapping(value ="getVehicleLicense")
@ApiOperation("行驶证识别")
@ApiImplicitParam(name ="imagePath",value ="图片路径",required = true)
public String getVehicleLicense(String imagePath) throws InterruptedException, IOException, URISyntaxException {
OcrClientToken ocrClient= new OcrClientToken(Domainname,Username,Password,Regionname,HttpEndpoint);
HttpResponse response=ocrClient.requestOcrServiceBase64("/v1.0/ocr/vehicle-license", imagePath);
System.out.println(response);
String content = IOUtils.toString(response.getEntity().getContent(), "u