新增 车位相机图片上传到OSS服务器

This commit is contained in:
Lemon
2023-12-13 10:43:50 +08:00
parent d81324fa77
commit f3c0874e7d
4 changed files with 177 additions and 47 deletions

View File

@@ -1,18 +1,32 @@
package com.jsowell.thirdparty.camera.service;
import cn.hutool.core.codec.Base64;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.jsowell.common.constant.CacheConstants;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.ObjectMetadata;
import com.jsowell.common.config.AliyunOssConfig;
import com.jsowell.common.core.redis.RedisCache;
import com.jsowell.common.util.DateUtils;
import com.jsowell.common.util.StringUtils;
import com.jsowell.common.util.file.AliyunOssUploadUtils;
import com.jsowell.common.util.file.ImageUtils;
import com.jsowell.pile.domain.PileCameraInfo;
import com.jsowell.pile.dto.camera.CameraIdentifyResultsDTO;
import com.jsowell.pile.service.IPileCameraInfoService;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Base64;
import javax.imageio.ImageIO;
import java.util.List;
@@ -24,6 +38,7 @@ import java.util.List;
*/
@Service
public class CameraService {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
private RedisCache redisCache;
@@ -89,10 +104,17 @@ public class CameraService {
return false;
}
// Base64解密
String plateNumber = Base64.decodeStr(plate.getPlate());
String zoneName = Base64.decodeStr(parking.getZoneName());
String plateNumber = cn.hutool.core.codec.Base64.decodeStr(plate.getPlate());
String zoneName = cn.hutool.core.codec.Base64.decodeStr(parking.getZoneName());
for (CameraIdentifyResultsDTO.BgImg bgImg : bgImgList) {
// 上传到阿里云OSS获取图片上传成功后的地址
String fileName = zoneName + "-" + System.currentTimeMillis() / 1000 + ".jpg";
String url = saveImage(bgImg.getImage(), fileName);
if (StringUtils.isBlank(url)) {
logger.error("车位号:{} 图片上传失败", zoneName);
continue;
}
PileCameraInfo pileCameraInfo = PileCameraInfo.builder()
.deviceName(deviceInfo.getDevName())
.deviceIp(deviceInfo.getIp())
@@ -103,7 +125,7 @@ public class CameraService {
.zoneName(zoneName)
.color(plate.getColor())
.plateType(plate.getType())
.image(bgImg.getImage())
.image(url)
.build();
// 插入数据库
@@ -114,34 +136,68 @@ public class CameraService {
}
/**
* 保存图像
* @param base64Image 图像的Base64编码
* @param fileName 文件名
* @return
*/
private String saveImage(String base64Image, String fileName) {
try {
// 将Base64编码的字符串解码为字节数组
byte[] imageBytes = Base64.getDecoder().decode(base64Image);
// 将字节数组转换为 BufferedImage
ByteArrayInputStream inputStream = new ByteArrayInputStream(imageBytes);
BufferedImage originalImage = ImageIO.read(inputStream);
// 对图像进行压缩
double quality = 0.5; // 压缩质量
BufferedImage compressedImage = ImageUtils.compressImage(originalImage, quality);
// 将压缩后的图片转换为字节数组
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ImageIO.write(compressedImage, "jpg", outputStream);
byte[] compressedImageBytes = outputStream.toByteArray();
// 上传图片到OSS
String url = AliyunOssUploadUtils.upload2OSS(compressedImageBytes, fileName);
if (StringUtils.isNotBlank(url)) {
return url;
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 将车辆图片信息存入缓存
* @param jsonObject
*/
private boolean saveCarPicture2Redis(JSONObject jsonObject, String parkingState) {
// 获取车牌号
String plateNumber = jsonObject.getJSONObject("plate").getString("plate");
if (StringUtils.isBlank(plateNumber)) {
return false;
}
// 获取背景图片
JSONArray bgImgs = jsonObject.getJSONArray("bg_img");
List<CameraIdentifyResultsDTO.BgImg> bgImgList = bgImgs.toList(CameraIdentifyResultsDTO.BgImg.class);
if (CollectionUtils.isEmpty(bgImgList)) {
return false;
}
for (CameraIdentifyResultsDTO.BgImg bgImg : bgImgList) {
String image = bgImg.getImage(); // 图片的 base64 编码
// String key = bgImg.getKey(); // 索引id
// key: 前缀 + 车牌号 + 日期 + 入场/出场状态
String redisKey = CacheConstants.CAMERA_IMAGE_BY_PLATE_NUMBER + plateNumber + "_" + DateUtils.getDate() + "_" + parkingState;
// 存入缓存
// TODO 暂时永久保存
redisCache.setCacheObject(redisKey, image);
}
return true;
}
// private boolean saveCarPicture2Redis(JSONObject jsonObject, String parkingState) {
// // 获取车牌号
// String plateNumber = jsonObject.getJSONObject("plate").getString("plate");
// if (StringUtils.isBlank(plateNumber)) {
// return false;
// }
// // 获取背景图片
// JSONArray bgImgs = jsonObject.getJSONArray("bg_img");
// List<CameraIdentifyResultsDTO.BgImg> bgImgList = bgImgs.toList(CameraIdentifyResultsDTO.BgImg.class);
// if (CollectionUtils.isEmpty(bgImgList)) {
// return false;
// }
// for (CameraIdentifyResultsDTO.BgImg bgImg : bgImgList) {
// String image = bgImg.getImage(); // 图片的 base64 编码
// // String key = bgImg.getKey(); // 索引id
// // key: 前缀 + 车牌号 + 日期 + 入场/出场状态
// String redisKey = CacheConstants.CAMERA_IMAGE_BY_PLATE_NUMBER + plateNumber + "_" + DateUtils.getDate() + "_" + parkingState;
// // 存入缓存
// // 暂时永久保存
// redisCache.setCacheObject(redisKey, image);
// }
// return true;
// }
}