初始界面
项目结构
SpringBootApplication——SpringBoot启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class IdCardorcApplication {
public static void main(String[] args) {
SpringApplication.run(IdCardorcApplication.class, args);
}
}
MainController——控制进程
异常情况下clear
if (faceImages.size() != backImages.size()) {
faceImages.clear();
backImages.clear();
faceResults.clear();
backResults.clear();
}
刷新页面重新加载已识别结果
if (!CollectionUtils.isEmpty(faceImages) && faceImages.size() == backImages.size()) {
model.addAttribute("faceImage", faceImages.get(faceImages.size() - 1));
model.addAttribute("faceResult", faceResults.get(faceResults.size() - 1));
model.addAttribute("backImage", backImages.get(backImages.size() - 1));
model.addAttribute("backResult", backResults.get(backResults.size() - 1));
}
上传文件判定
public String upload(@RequestParam("face") MultipartFile face, @RequestParam("back") MultipartFile back,
RedirectAttributes redirectAttributes) {
if (face.isEmpty() || back.isEmpty()) {
redirectAttributes.addFlashAttribute("messages", "请选择一个文件进行上传。");
return "redirect:/index";
}
String errorMessages = null;
Path dir = Paths.get(uploadDir);
if (!Files.exists(dir)) {
try {
Files.createDirectories(dir);
} catch (IOException e) {
e.printStackTrace();
errorMessages += e.getMessage() + "\n";
}
}
try {
if (!face.isEmpty()) {
String filename = saveFile(face);
Map<String, String> faceResult = ocrService.RecognizeIdCard(uploadDir + filename, "face");
faceImages.add("/images/" + filename);
faceResults.add(faceResult);
}
if (!back.isEmpty()) {
String filename = saveFile(back);
Map<String, String> faceResult = ocrService.RecognizeIdCard(uploadDir + filename, "back");
backImages.add("/images/" + filename);
backResults.add(faceResult);
}
} catch (Exception e) {
e.printStackTrace();
errorMessages += e.getMessage() + "\n";
}
if (StringUtils.isNoneBlank(errorMessages)) {
redirectAttributes.addFlashAttribute("messages", errorMessages);
}
return "redirect:/index";
}
存储逻辑
public String saveFile(MultipartFile file) {
String filename = UUID.randomUUID().toString() + "."
+ StringUtils.substringAfterLast(file.getOriginalFilename(), ".");
Path path = Paths.get(uploadDir + filename);
System.out.println(faceResults);
try {
Files.copy(file.getInputStream(), path, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
return null;
}
return filename;
}
OcrService——通过SDK调用视觉开放平台的OCR
初始化config
private void init() throws Exception {
Config config = new Config();
config.endpointType = "access_key";
config.regionId = "cn-shanghai";
config.accessKeyId = accessKeyId;
config.accessKeySecret = accessKeySecret;
config.endpoint = "ocr.cn-shanghai.aliyuncs.com";
orcClient = new Client(config);
runtimeOptions = new RuntimeOptions();
}
调用逻辑
public Map<String, String> RecognizeIdCard(String fielpath, String side) throws Exception {
RecognizeIdentityCardAdvanceRequest request = new RecognizeIdentityCardAdvanceRequest();
request.imageURLObject = Files.newInputStream(Paths.get(fielpath));
request.side = side;
RecognizeIdentityCardResponse response = orcClient.recognizeIdentityCardAdvance(request, runtimeOptions);
return "face".equals(side) ?
JSON.parseObject(JSON.toJSONString(response.data.frontResult), new TypeReference<Map<String, String>>(){})
: JSON.parseObject(JSON.toJSONString(response.data.backResult), new TypeReference<Map<String, String>>(){});
}
index.html——前端模板
上传界面
<form method="post" th:action="@{/upload}" enctype="multipart/form-data">
<div class="row">
<div class="col-sm-4">
<div class="input-group">
<input id="location" class="form-control" onclick="$('#i-face').click();">
<label class="input-group-btn">
<input type="button" id="i-check" value="上传人像面" class="btn btn-primary" onclick="$('#i-face').click();">
</label>
</div>
</div>
<input type="file" name="face" id="i-face" accept=".jpg, .png, .jpeg" onchange="$('#location').val($('#i-face').val());" style="display: none">
<div class="col-sm-4">
<div class="input-group">
<input id="locationf" class="form-control" onclick="$('#i-back').click();">
<label class="input-group-btn">
<input type="button" id="i-checkb" value="上传国徽面" class="btn btn-primary" onclick="$('#i-back').click();">
</label>
</div>
</div>
<input type="file" name="back" id="i-back" accept=".jpg, .png, .jpeg" onchange="$('#locationf').val($('#i-back').val());" style="display: none">
<div class="col-sm-4">
<button type="submit" class="btn btn-primary form-control">开始识别</button>
</div>
</div>
</form>
识别后展示
<div class="row" style="margin-top: 38px;">
<div class="col-md-12 mx-auto">
<div class="row">
<div class="col-sm-4">
<img th:src="${faceImage}" th:if="faceImage != null" class="img-fluid">
</div>
<div class="col-sm-4">
<img th:src="${backImage}" th:if="backImage != null" class="img-fluid">
</div>
</div>
</div>
</div>
<div class="row" style="margin-top: 38px;">
<div class="col-md-12 mx-auto" th:if="${faceResult != null}">
<div class="row">
<div class="col-sm-4">
<p><span>姓名: </span><span th:text="${faceResult.name}"></span></p>
<p><span>性别: </span><span th:text="${faceResult.gender}"></span></p>
<p><span>民族: </span><span th:text="${faceResult.nationality}"></span></p>
<p><span>出生日期: </span><span th:text="${faceResult.birthDate}"></span></p>
<p><span>住址: </span><span th:text="${faceResult.address}"></span></p>
<p><span>身份证号: </span><span th:text="${faceResult.IDNumber}"></span></p>
</div>
<div class="col-sm-4">
<p><span>签发机关: </span><span th:text="${backResult.issue}"></span></p>
<p><span>有效日期: </span><span th:text="${backResult.startDate}">-<span th:text="${faceResult.endDate}"></span></span></p>
</div>
</div>
</div>
</div>
application.properties——若干配置文件
server.port=
file.uplaod.path=
aliapi.accessKeyId=
aliapi.accessKeySecret=
pom.xml——若干相关依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.10</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.4.8</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.52</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>ocr</artifactId>
<version>1.0.3</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>
</dependencies>