Skip to content

Commit

Permalink
增加黑白图片上色功能
Browse files Browse the repository at this point in the history
  • Loading branch information
no1xuan committed Nov 8, 2024
1 parent 271debf commit 3a8fed6
Show file tree
Hide file tree
Showing 26 changed files with 247 additions and 74 deletions.
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
- 小程序前端请前往:https://github.com/no1xuan/photo
- 小程序前端另外一套请前往:https://github.com/no1xuan/ai-photo
- 小程序管理员网页后台请前往:https://github.com/no1xuan/zjzAdmin
- HivisionIDPhotos:https://github.com/Zeyi-Lin/HivisionIDPhotos
- HivisionIDPhotos(证件照API)https://github.com/Zeyi-Lin/HivisionIDPhotos

<hr>
<br>
Expand All @@ -32,7 +32,8 @@ uniapp多端兼容版:https://github.com/wmlcjj/AI-IDPhoto
<br>

# ⭐最近更新
- 2024.11.06:让1.sql文件同时支持Mysql5.7和8.0的版本,免去手动修改的麻烦,从此版本开始为长期稳定版本,基本不会频繁更新
- 2024.11.08:增加黑白图片上色功能,从此版本开始为长期稳定版本,基本不会频繁更新
- 2024.11.06:让1.sql文件同时支持Mysql5.7和8.0的版本,免去手动修改的麻烦
- 2024.11.05:增加登录失败时返回错误原因
- 2024.10.18:修复高清照一直没生效的问题
- 2024.10.10:修复指定最快模型传参单词拼写错误
Expand All @@ -51,13 +52,14 @@ uniapp多端兼容版:https://github.com/wmlcjj/AI-IDPhoto

# 📦前提准备

本项目基于HivisionIDPhotos的2024.09.25更新的版本进行对接开发
本项目证件照功能的API是基于HivisionIDPhotos的2024.09.25更新的版本进行对接开发

理论HivisionIDPhotos不改变入参和返回,即可直接使用最新版

1. 2024.09.25的HivisionIDPhotos(以内置MTCNN+hivision_modnet模型)下载:https://wwba.lanzouq.com/ihjM02bfoz0d

2. 鉴黄APi端下载:https://github.com/no1xuan/zjzNsfw
2. 鉴黄APi下载:https://github.com/no1xuan/zjzNsfw
3. 黑白图片上色API:https://github.com/no1xuan/colourize

注意:

Expand Down Expand Up @@ -175,7 +177,8 @@ uniapp多端兼容版:https://github.com/wmlcjj/AI-IDPhoto

鉴黄APi:http://121.62.63.137:3006/

证件照APi: http://121.62.63.137:8299/
证件照APi:http://121.62.63.137:8299/
黑白图片:http://colourize.0po.cn/

<hr>
<br>
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/zjzWx/controller/AdminController.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public R okLogin(String code1,String code2) {
}


//管理员首页数据
//首页数据
@PostMapping("/adminIndex")
public R adminIndex(){
int id = Integer.parseInt(StpUtil.getTokenInfo().getLoginId().toString());
Expand All @@ -74,7 +74,7 @@ public R adminIndex(){



//管理员规格列表
//规格列表
@PostMapping("/getItemPage")
public R getItemPage(int pageNum, int pageSize, String name){
int id = Integer.parseInt(StpUtil.getTokenInfo().getLoginId().toString());
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/zjzWx/controller/ItemController.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public R photoList(int pageNum, int pageSize){
return R.ok(photos);
}

//删除作品
//删除作品(下个版本更新物理删除)
@GetMapping("/deletePhotoId")
public R deletePhotoId(int id){
QueryWrapper<Photo> qw = new QueryWrapper<>();
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/org/zjzWx/controller/OtherApiController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.zjzWx.controller;

import cn.dev33.satoken.stp.StpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.zjzWx.service.OtherApiService;
import org.zjzWx.util.R;

@RestController
@RequestMapping("/otherApi")
public class OtherApiController {

@Autowired
private OtherApiService otherApiService;



@PostMapping("/colourize")
public R colourize(@RequestBody String image) {
String colourize = otherApiService.colourize(Integer.parseInt(StpUtil.getTokenInfo().getLoginId().toString()), image);
if(null==colourize){
return R.no("图片上色失败,请重试");
}
return R.ok(colourize);
}



}
1 change: 1 addition & 0 deletions src/main/java/org/zjzWx/entity/Admin.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
@AllArgsConstructor
@NoArgsConstructor
public class Admin {

@TableId(type = IdType.AUTO)
private Integer id;

Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/zjzWx/entity/Custom.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
@NoArgsConstructor
@TableName("custom")
public class Custom {

@TableId(type = IdType.AUTO)
private Integer id;
/**
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/zjzWx/entity/Item.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
@NoArgsConstructor
@TableName("item")
public class Item {

@TableId(type = IdType.AUTO)
private Integer id;
/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/zjzWx/entity/Photo.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class Photo {
/**
* 图片表
*/
@TableId(type = IdType.AUTO)
@TableId(type = IdType.AUTO)
private Integer id;
/**
* 用户id
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/zjzWx/entity/PhotoRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class PhotoRecord {
/**
* 用户行为记录
*/
@TableId(type = IdType.AUTO)
@TableId(type = IdType.AUTO)
private Integer id;
/**
* 名字
Expand All @@ -30,7 +30,7 @@ public class PhotoRecord {
/**
* 创建时间
*/
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date createTime;


Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/zjzWx/entity/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class User {
/**
* 用户表id
*/
@TableId(type = IdType.AUTO)
@TableId(type = IdType.AUTO)
private Integer id;
/**
* openid
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/zjzWx/file/ImageUpload.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class ImageUpload {
@Autowired
private PhotoRecordService photoRecordService;

//图片检查
//图片鉴黄,通过返回base64
@PostMapping("/upload")
public R uploadImage(@RequestParam("file") MultipartFile file) {
if (file.isEmpty()) {
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/org/zjzWx/model/dto/ColourizeDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.zjzWx.model.dto;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class ColourizeDto {
private Integer status;
private String processedImage;
}
2 changes: 1 addition & 1 deletion src/main/java/org/zjzWx/model/dto/CreatePhotoDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class CreatePhotoDto {
private Integer itemId;
//记录id
private Integer photoId;
//换色方式 0纯色 1上下渐变 2中心渐变
//换色方式 0纯色 1上下渐变 2中心渐变
private Integer render;
//图片kb
private Integer kb;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/zjzWx/model/dto/HivisionDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
public class HivisionDto {
//是否制作成功
private boolean status;
//初始化白底证件照图片
//不高清抠图透明图片
private String imageBase64Standard;
//高清抠图透明图片
private String imageBase64Hd;
//切换颜色上色后图片
//切换颜色后的上色图片
private String imageBase64;
}
2 changes: 1 addition & 1 deletion src/main/java/org/zjzWx/model/vo/AdminIndexVo.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ public class AdminIndexVo {
//支持的规格(包含自定义)
private long itemTotal;

//图标
//图表
private ChartDataVo chartDataVo;
}
1 change: 0 additions & 1 deletion src/main/java/org/zjzWx/model/vo/ChartDataVo.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ public class ChartDataVo {
private List<String> time;
private List<Integer> data;

// Getter 和 Setter 方法
public List<String> getTime() {
return time;
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/zjzWx/service/ApiService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.zjzWx.model.vo.PicVo;
import org.zjzWx.util.R;

//证件照相关API
public interface ApiService {


Expand Down
10 changes: 10 additions & 0 deletions src/main/java/org/zjzWx/service/OtherApiService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.zjzWx.service;

//其它乱七八糟的小功能的APi
public interface OtherApiService {


//黑白图片上色
String colourize(Integer userId,String img);

}
3 changes: 1 addition & 2 deletions src/main/java/org/zjzWx/service/impl/AdminServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,8 @@ public AdminLoginVo login() {
mp.put("scene", code);
mp.put("page", "pages/admin/index");
mp.put("check_path", false);
// 要打开的小程序版本。正式版为 "release",体验版为 "release",开发版为 "develop"。默认是正式版.
// 要打开的小程序版本。正式版为 "release",体验版为 "release",开发版为 "develop"。默认是正式版
mp.put("env_version", envVersion);
// 获取access_token 2小时过期一次
String url1 = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + webSet.getAppId() + "&secret=" + webSet.getAppSecret();
HttpClient http1 = new HttpClient(url1);
http1.setHttps(true);
Expand Down
59 changes: 26 additions & 33 deletions src/main/java/org/zjzWx/service/impl/ApiServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ public class ApiServiceImpl implements ApiService {
@Value("${webset.zjzDomain}")
private String zjzDomain;

@Value("${webset.directory}")
private String directory;

@Value("${webset.picDomain}")
private String picDomain;



@Autowired
private CustomService customService;
@Autowired
Expand All @@ -40,11 +48,7 @@ public class ApiServiceImpl implements ApiService {
@Autowired
private PhotoRecordService photoRecordService;

@Value("${webset.directory}")
private String directory;

@Value("${webset.picDomain}")
private String picDomain;



Expand Down Expand Up @@ -300,7 +304,7 @@ public PicVo updateUserPhonto(Integer userid,String img,Integer photoId) {


//因为图片没刚开始存库,是为了防止性能浪费,所有由前端传入
// 将图片转成MultipartFile,再次检查,防止数据伪造,如:被劫持数据包上传黄色,木马什么的
//将图片转成MultipartFile,再次检查,防止数据伪造,如:被劫持数据包上传黄色,木马什么的
MultipartFile file = PicUtil.base64ToMultipartFile(img);


Expand Down Expand Up @@ -330,44 +334,33 @@ public PicVo updateUserPhonto(Integer userid,String img,Integer photoId) {
// }


//检查通过,上传服务器,数据库保存url
//之前试过保存base64,发现数据库加载很慢,性能很低
//检查通过,上传服务器,数据库保存url
//之前试过保存base64,发现数据库加载很慢,性能很低
try {
// 创建文件存储目录
//按照日期保存文件
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
String folderName = dateFormat.format(new Date());
File uploadFolder = new File(directory, folderName);
if (!uploadFolder.exists()) {
uploadFolder.mkdirs();
}
String filename = PicUtil.filesCopy(folderName, directory, originalFilename, file);

// 生成新的文件名
String filename = PicUtil.generateUniqueFilename(originalFilename, file);
Path filePath = uploadFolder.toPath().resolve(filename);


// 保存文件
Files.copy(file.getInputStream(), filePath, StandardCopyOption.REPLACE_EXISTING);
String imagePath = picDomain + folderName + "/" + filename;
photo.setId(photoId);
photo.setNImg(imagePath);
photoService.updateById(photo);

//nginx帮助
String imagePath = picDomain + folderName + "/" + filename;
photo.setId(photoId);
photo.setNImg(imagePath);
photoService.updateById(photo);

//保存用户行为记录
PhotoRecord record = new PhotoRecord();
record.setName("下载证件照");
record.setUserId(userid);
record.setCreateTime(new Date());
photoRecordService.save(record);
picVo.setPicUrl(imagePath);
return picVo;

//保存用户行为记录
PhotoRecord record = new PhotoRecord();
record.setName("下载证件照");
record.setUserId(userid);
record.setCreateTime(new Date());
photoRecordService.save(record);
picVo.setPicUrl(imagePath);
return picVo;

} catch (IOException e) {
} catch (Exception e) {
e.printStackTrace();
// 处理文件保存失败的情况
picVo.setMsg("图片存入失败错误,请重试");
return picVo;
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/zjzWx/service/impl/ItemServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class ItemServiceImpl extends ServiceImpl<ItemDao,Item> implements ItemSe
@Override
public <T> List<T> itemList(int pageNum, int pageSize, int type, String userId,String name) {

//定制列表
if(type==4){
Page<Custom> page = new Page<>(pageNum, pageSize);
QueryWrapper<Custom> qw = new QueryWrapper<>();
Expand All @@ -33,6 +34,7 @@ public <T> List<T> itemList(int pageNum, int pageSize, int type, String userId,S
}


//规格列表
Page<Item> page = new Page<>(pageNum, pageSize);
QueryWrapper<Item> qw = new QueryWrapper<>();
if(name!=null && !name.equals("")){
Expand Down
Loading

0 comments on commit 3a8fed6

Please sign in to comment.