From e00528e446c3e2091789c6fc086f9abab455b19c Mon Sep 17 00:00:00 2001 From: Miku <2634544095@qq.com> Date: Wed, 23 Aug 2023 19:52:47 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BD=A6=E7=89=8C=E8=AF=86=E5=88=ABweb?= =?UTF-8?q?=E7=AB=AF=EF=BC=8C=E5=88=A9=E7=94=A8Tess4j=E8=BF=9B=E8=A1=8C?= =?UTF-8?q?=E8=BD=A6=E7=89=8C=E8=AF=86=E5=88=AB=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 5 + .../module/car/web/DistinguishController.java | 167 +++++++++++++++++ .../templates/car/distinguish/list.html | 177 ++++++++++++++++++ 3 files changed, 349 insertions(+) create mode 100644 src/main/java/com/smart/module/car/web/DistinguishController.java create mode 100644 src/main/resources/templates/car/distinguish/list.html diff --git a/pom.xml b/pom.xml index 7167043..39cfea2 100644 --- a/pom.xml +++ b/pom.xml @@ -127,6 +127,11 @@ poi-ooxml-schemas 4.0.1 + + net.sourceforge.tess4j + tess4j + 4.5.4 + org.springframework.boot diff --git a/src/main/java/com/smart/module/car/web/DistinguishController.java b/src/main/java/com/smart/module/car/web/DistinguishController.java new file mode 100644 index 0000000..0861724 --- /dev/null +++ b/src/main/java/com/smart/module/car/web/DistinguishController.java @@ -0,0 +1,167 @@ +package com.smart.module.car.web; + +import cn.hutool.core.date.DateUtil; +import cn.hutool.core.io.FileUtil; +import cn.hutool.core.util.IdUtil; +import com.smart.common.constant.SystemConstant; +import com.smart.common.model.Result; +import com.smart.common.util.CommonUtils; +import com.smart.common.util.DateUtils; +import com.smart.module.car.entity.CarManage; +import com.smart.module.car.entity.CarParkManage; +import com.smart.module.car.entity.CarParkingRecord; +import com.smart.module.car.repository.CarParkingRecordRepository; +import com.smart.module.car.repository.ParkManageRepository; +import com.smart.module.car.service.CarManageService; +import com.smart.module.car.service.CarParkingRecordService; +import com.smart.module.car.util.BaiDuUtils; +import net.sourceforge.tess4j.ITesseract; +import net.sourceforge.tess4j.Tesseract; +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.multipart.MultipartFile; + +import java.io.File; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +@RestController +@RequestMapping("/car/distinguish") +public class DistinguishController { + + @Value("${file.path}") + private String filePath; + + @Autowired + private BaiDuUtils baiDuUtils; + // 注入百度工具类,用于车牌识别 + + @Autowired + private ParkManageRepository parkManageRepository; + // 注入停车场管理仓库,用于查询停车场信息 + + @Autowired + private CarManageService carManageService; + // 注入车辆管理服务,用于查询车辆信息 + + @Autowired + private CarParkingRecordRepository carParkingRecordRepository; + // 注入车辆进出记录仓库,用于查询车辆进出记录 + + @Autowired + private CarParkingRecordService carParkingRecordService; + // 注入车辆进出记录服务,用于保存和查询车辆进出记录 + + /** + * 接收上传的文件和停车场id + */ + @RequestMapping("upload") + public Result upload(MultipartFile file,Long id) { + try { + if(file!=null){ + CarParkManage carParkManage = + parkManageRepository.findById(id).orElse(new CarParkManage()); + // 根据传入的停车场id查询停车场信息 + + if(id==null||carParkManage==null){ + return Result.error("请选择停车场"); + } + // 如果停车场id为空或找不到对应的停车场信息,则返回错误提示 + + File parentFile = CommonUtils.createParentFile(filePath); + // 创建保存文件的父目录 + + String fileName = file.getOriginalFilename(); + String suffix = fileName.substring(fileName.lastIndexOf(".")); + String uuid = IdUtil.simpleUUID(); + fileName = uuid + suffix; + // 生成唯一的文件名,保证文件名不重复 + + File imageFile = new File(parentFile,fileName); + FileUtil.writeFromStream(file.getInputStream(), imageFile); + // 将上传的文件保存到指定路径下 + + String fileDay = DateUtil.thisYear()+"/"+(DateUtil.thisMonth()+1)+"/"+DateUtil.thisDayOfMonth(); + String imagePath = SystemConstant.FILE + "/" + fileDay+"/"+fileName; + // 构建图片路径,用于存储到数据库中 + +// String plateNumber = baiDuUtils.plateLicense(imageFile.getAbsolutePath()); +// // 调用百度工具类进行车牌识别 +// +// if(StringUtils.isBlank(plateNumber)){ +// return Result.error("识别失败"); +// } +// // 如果识别结果为空,则返回识别失败的错误提示 + + ITesseract instance = new Tesseract(); + + String tessData = "C:\\Users\\Re11a\\Downloads\\smart-parking\\Tess4J";//tess4j 解压地址 + File directory = new File(tessData); + String courseFile = null; + try { + courseFile = directory.getCanonicalPath(); + } catch (IOException e) { + e.printStackTrace(); + } + //设置训练库的位置 + instance.setDatapath(courseFile + "//tessdata"); + instance.setLanguage("chi_sim+eng");//chi_sim :简体中文, eng 根据需求选择语言库 + String plateNumber = instance.doOCR(imageFile);//.replaceAll("\\s", ""); // 进行OCR识别,并去除空格 + + if (plateNumber.isEmpty()) { + System.out.println("识别失败"); + } else { + System.out.println("车牌号:" + plateNumber); + } + + Map map = new HashMap<>(); + map.put("plateNumber",plateNumber); + map.put("imagePath",imagePath); + // 构建返回结果,包含车牌号码和图片路径 + + CarParkingRecord record = + carParkingRecordService.getByPlateNumber(plateNumber,id); + // 根据车牌号码和停车场id查询车辆进出记录 + + /** + * 出厂 + */ + if(record!=null){ + record.setGmtOut(DateUtils.getTimestamp()); + map.put("msg","出厂成功"); + }else{ + record = new CarParkingRecord(); + record.setOrgId(carParkManage.getOrgId()); + record.setOrgName(carParkManage.getOrgName()); + record.setParkManageId(carParkManage.getId()); + record.setParkManageName(carParkManage.getName()); + record.setGmtInto(DateUtils.getTimestamp()); + record.setPlateNumber(plateNumber); + CarManage carManage = + carManageService.getByPlateNumber(plateNumber,id); + if(carManage!=null){ + record.setType(carManage.getType()); + }else{ + record.setType(SystemConstant.CAR_TYPE_TEMP); + } + map.put("msg","进厂成功"); + } + // 判断车辆进出记录是否存在,如果存在则表示车辆已经入场,执行出场逻辑;否则表示车辆未入场,执行入场逻辑。 + + carParkingRecordService.save(record); + // 保存车辆进出记录 + + return Result.ok(map); + }else{ + return Result.error(); + } + } catch (Exception e) { + e.printStackTrace(); + return Result.error(); + } + } +} diff --git a/src/main/resources/templates/car/distinguish/list.html b/src/main/resources/templates/car/distinguish/list.html new file mode 100644 index 0000000..9796cfd --- /dev/null +++ b/src/main/resources/templates/car/distinguish/list.html @@ -0,0 +1,177 @@ + + + + + +
+ + +
+
+ + + +