Browse Source

fix: 修改图片为空的空指针异常

master
温小超 6 months ago
parent
commit
17de6ec92a
  1. 5
      open-anpr-server/src/main/java/com/visual/open/anpr/server/controller/server/impl/PlateControllerImpl.java
  2. 8
      open-anpr-server/src/main/java/com/visual/open/anpr/server/controller/server/restful/PlateController.java
  3. 8
      open-anpr-server/src/main/java/com/visual/open/anpr/server/domain/common/ResponseInfo.java
  4. 14
      open-anpr-server/src/main/java/com/visual/open/anpr/server/utils/FfmpegUtil.java

5
open-anpr-server/src/main/java/com/visual/open/anpr/server/controller/server/impl/PlateControllerImpl.java

@ -18,6 +18,11 @@ public class PlateControllerImpl extends BaseController implements PlateControll
@Override @Override
public ResponseInfo<List<PlateInfoRepVo>> recognition(PlateInfoReqVo plateInfoReq) { public ResponseInfo<List<PlateInfoRepVo>> recognition(PlateInfoReqVo plateInfoReq) {
String image = plateInfoReq.getImage();
if (image == null || image.isEmpty()) {
return ResponseBuilder.exception("image cannot be empty", null);
}
try { try {
return ResponseBuilder.success(plateService.recognition(plateInfoReq)); return ResponseBuilder.success(plateService.recognition(plateInfoReq));
}catch (Exception e){ }catch (Exception e){

8
open-anpr-server/src/main/java/com/visual/open/anpr/server/controller/server/restful/PlateController.java

@ -34,11 +34,12 @@ public class PlateController extends PlateControllerImpl {
@ApiOperation(value = "根据图片名称车牌识别") @ApiOperation(value = "根据图片名称车牌识别")
@RequestMapping(value = "getByImageName", method = RequestMethod.GET) @RequestMapping(value = "getByImageName", method = RequestMethod.GET)
public ResponseInfo<List<PlateInfoRepVo>> imagesToBase64(@RequestParam String imageName){ public ResponseInfo<List<PlateInfoRepVo>> imagesToBase64(@RequestParam String imageName){
String property = System.getProperty("user.dir") + "\\images\\"; String property = System.getProperty("user.dir") ;
String filePath = property + targetPath + imageName; String filePath = property + targetPath + imageName;
String imageBaseCode = ImageUtil.convertImageToBase64Str(filePath); String imageBaseCode = ImageUtil.convertImageToBase64Str(filePath);
PlateInfoReqVo plateInfoReq = new PlateInfoReqVo(); PlateInfoReqVo plateInfoReq = new PlateInfoReqVo();
plateInfoReq.setImage(imageBaseCode); plateInfoReq.setImage(imageBaseCode);
return recognition(plateInfoReq); return recognition(plateInfoReq);
} }
@ -46,7 +47,7 @@ public class PlateController extends PlateControllerImpl {
/** /**
* 抓拍实况中的车牌并识别 可同时识别默认最多5张车牌 * 抓拍实况中的车牌并识别 可同时识别默认最多5张车牌
* @param CameraIP ffmpeg所需的摄像头IP地址 * @param CameraIP ffmpeg所需要的输入流
* @return 返回识别结果 * @return 返回识别结果
*/ */
@ApiOperation(value = "抓拍实况中的车牌并识别") @ApiOperation(value = "抓拍实况中的车牌并识别")
@ -55,6 +56,9 @@ public class PlateController extends PlateControllerImpl {
// 获取当前项目路径 // 获取当前项目路径
String property = System.getProperty("user.dir"); String property = System.getProperty("user.dir");
String imageName = FfmpegUtil.getLiveCapture(CameraIP, property + targetPath); String imageName = FfmpegUtil.getLiveCapture(CameraIP, property + targetPath);
if ("error".equals(imageName)){
return ResponseInfo.error("抓拍失败");
}
return imagesToBase64(imageName); return imagesToBase64(imageName);
} }

8
open-anpr-server/src/main/java/com/visual/open/anpr/server/domain/common/ResponseInfo.java

@ -28,6 +28,14 @@ public class ResponseInfo<T> implements Serializable{
this.data = data; this.data = data;
} }
public static <T> ResponseInfo<T> success(T data){
return new ResponseInfo<T>(200, "success", data);
}
public static <T> ResponseInfo<T> error(String message){
return new ResponseInfo<T>(500, message, null);
}
public Integer getCode() { public Integer getCode() {
return code; return code;
} }

14
open-anpr-server/src/main/java/com/visual/open/anpr/server/utils/FfmpegUtil.java

@ -197,11 +197,17 @@ public class FfmpegUtil {
blockFfmpeg(br); blockFfmpeg(br);
} }
} catch (IOException e) { } catch (IOException e) {
formattedDate = "获取实况抓拍图片失败"; System.out.println("获取实况抓拍图片失败");
System.out.println(formattedDate); return "error";
e.printStackTrace(); }
File file = new File(targetPath + formattedDate);
boolean exists = file.exists();
if (!exists) {
return "error";
}
return formattedDate; return formattedDate;
} return formattedDate;
} }
/** /**

Loading…
Cancel
Save