Skip to content

Commit

Permalink
集成接口
Browse files Browse the repository at this point in the history
  • Loading branch information
wmlcjj committed Sep 28, 2024
1 parent f8ae976 commit 424a651
Show file tree
Hide file tree
Showing 20 changed files with 492 additions and 295 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package org.photo.modular.HivisionIDPhotos.contorller;

import org.photo.modular.HivisionIDPhotos.request.HIDAddBackgroundRequest;
import org.photo.modular.HivisionIDPhotos.request.HIDIdPhotoRequest;
import org.photo.modular.HivisionIDPhotos.response.HIDHivisionResponse;
import org.photo.modular.HivisionIDPhotos.service.HivisionIDPhotosService;
import org.photo.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/idPhotoApi")
public class HivisionIDPhotosController {

@Autowired
private HivisionIDPhotosService hivisionIDPhotosService;

@PostMapping("/idphoto")
public HIDHivisionResponse idphoto(MultipartFile file, HIDIdPhotoRequest request) throws IOException {
request.setInput_image(file);
return hivisionIDPhotosService.idphoto(request);
}

@PostMapping("/add_background")
public R<HIDHivisionResponse> add_background(MultipartFile file, HIDAddBackgroundRequest request) throws IOException {
request.setInput_image(file);
return R.ok(hivisionIDPhotosService.add_background(request));
}

@PostMapping("/generate_layout_photos")
public R<HIDHivisionResponse> generate_layout_photos(MultipartFile file, HIDAddBackgroundRequest request) throws IOException {
request.setInput_image(file);
return R.ok(hivisionIDPhotosService.generate_layout_photos(request));
}

@PostMapping("/human_matting")
public R<HIDHivisionResponse> human_matting(MultipartFile file, HIDAddBackgroundRequest request) throws IOException {
request.setInput_image(file);
return R.ok(hivisionIDPhotosService.human_matting(request));
}

@PostMapping("/watermark")
public R<HIDHivisionResponse> watermark(MultipartFile file, HIDAddBackgroundRequest request) throws IOException {
request.setInput_image(file);
return R.ok(hivisionIDPhotosService.watermark(request));
}

@PostMapping("/set_kb")
public R<HIDHivisionResponse> set_kb(MultipartFile file, HIDAddBackgroundRequest request) throws IOException {
request.setInput_image(file);
return R.ok(hivisionIDPhotosService.set_kb(request));
}

@PostMapping("/idphoto_crop")
public R<HIDHivisionResponse> idphoto_crop(MultipartFile file, HIDAddBackgroundRequest request) throws IOException {
request.setInput_image(file);
return R.ok(hivisionIDPhotosService.idphoto_crop(request));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package org.photo.modular.HivisionIDPhotos.enums;

import cn.hutool.core.util.ObjectUtil;
import lombok.AllArgsConstructor;
import lombok.Getter;

/**
* 积分类型枚举
*
* @author Chopper
* @since 2021/3/20 10:44
*/

@Getter
@AllArgsConstructor
public enum HIDOptTypeEnum {
/**
* 生成证件照(底透明)
*/
IDPHOTO(1, "idphoto"),
/**
* 添加背景色
*/
ADD_BACKGROUND(2, "add_background"),
/**
* 生成六寸排版照
*/
GENERATE_LAYOUT_PHOTOS(3, "generate_layout_photos"),
/**
* 人像抠图
*/
HUMAN_MATTING(4, "human_matting"),
/**
* 图像加水印
*/
WATERMARK(5, "watermark"),
/**
* 设置图像KB大小
*/
SET_KB(6, "set_kb"),
/**
* 证件照裁切
*/
IDPHOTO_CROP(7, "idphoto_crop");


private Integer value;
private String name;

public static String getNameByValue(Integer code){
if(ObjectUtil.isNotEmpty(code)) {
for (HIDOptTypeEnum pointOptEnum : values()) {
if (pointOptEnum.getValue().toString().equals(code.toString())) {
return pointOptEnum.getName();
}
}
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.photo.modular.HivisionIDPhotos.request;

import lombok.Data;
import org.springframework.web.multipart.MultipartFile;

@Data
public class HIDAddBackgroundRequest {
private MultipartFile input_image; // 传入的图像文件路径
private String input_image_base64; // 传入的图像文件的base64编码
private String color; // 背景色HEX值,默认为000000
private Integer kb; // 输出照片的 KB 值,默认为None
private Integer render; // 渲染模式,默认为0
private Integer dpi; // 图像分辨率,默认为300
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.photo.modular.HivisionIDPhotos.request;

import lombok.Data;
import org.springframework.web.multipart.MultipartFile;

@Data
public class HIDGenerateLayoutPhotosRequest {
private MultipartFile input_image; // 传入的图像文件路径
private String input_image_base64; // 传入的图像文件的base64编码
private Integer height; // 输入图像的高度,默认为413
private Integer width; // 输入图像的宽度,默认为295
private Integer kb; // 输出照片的 KB 值,默认为None
private Integer dpi; // 图像分辨率,默认为300
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.photo.modular.HivisionIDPhotos.request;

import lombok.Data;
import org.springframework.web.multipart.MultipartFile;

@Data
public class HIDHumanMattingRequest {
private MultipartFile input_image; // 传入的图像文件路径
private String human_matting_model; // 人像分割模型,默认为modnet_photographic_portrait_matting
private Integer dpi; // 图像分辨率,默认为300
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.photo.modular.HivisionIDPhotos.request;

import lombok.Data;
import org.springframework.web.multipart.MultipartFile;

@Data
public class HIDIdPhotoCropRequest {
private MultipartFile input_image; // 传入的图像文件路径
private String input_image_base64; // 传入的图像文件的base64编码
private Integer height; // 标准证件照高度,默认为413
private Integer width; // 标准证件照宽度,默认为295
private String face_detect_model; // 人脸检测模型,默认为mtcnn
private Boolean hd; // 是否生成高清证件照,默认为true
private Integer dpi; // 图像分辨率,默认为300
private Float head_measure_ratio; // 面部面积与照片面积的比例,默认为0.2
private Float head_height_ratio; // 面部中心与照片顶部的高度比例,默认为0.45
private Float top_distance_max; // 头部与照片顶部距离的比例最大值,默认为0.12
private Float top_distance_min; // 头部与照片顶部距离的比例最小值,默认为0.1
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package org.photo.modular.idPhotoApi.request;
package org.photo.modular.HivisionIDPhotos.request;

import lombok.Data;
import org.springframework.web.multipart.MultipartFile;

@Data
public class IdPhotoRequest {
public class HIDIdPhotoRequest {
private MultipartFile input_image; // 传入的图像文件路径
private String input_image_base64; // 传入的图像文件的base64编码
private Integer height; // 标准证件照高度,默认为413
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.photo.modular.HivisionIDPhotos.request;

import lombok.Data;
import org.springframework.web.multipart.MultipartFile;

@Data
public class HIDSetKbRequest {
private MultipartFile input_image; // 传入的图像文件路径
private String input_image_base64; // 传入的图像文件的base64编码
private Integer kb; // 输出照片的 KB 值,默认为None
private Integer dpi; // 图像分辨率,默认为300
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.photo.modular.HivisionIDPhotos.request;

import lombok.Data;
import org.springframework.web.multipart.MultipartFile;

@Data
public class HIDWatermarkRequest {
private MultipartFile input_image; // 传入的图像文件路径
private String input_image_base64; // 传入的图像文件的base64编码
private String text; // 水印文本,默认为Hello
private Integer size; // 水印字体大小,默认为20
private Float opacity; // 水印透明度,默认为0.5
private Integer angle; // 水印旋转角度,默认为30
private String color; // 水印颜色,默认为#000000
private Integer space; // 水印间距,默认为25
private Integer dpi; // 图像分辨率,默认为300
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package org.photo.modular.idPhotoApi.response;
package org.photo.modular.HivisionIDPhotos.response;

import cn.hutool.core.util.ObjectUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class HivisionResponse {
public class HIDHivisionResponse {
// 状态码,true表示成功
private boolean status;
//标准证件照的base64编码
Expand All @@ -18,13 +19,19 @@ public class HivisionResponse {
private String imageBase64;

public String getImageBase64Standard() {
if (!imageBase64Standard.startsWith("data:image")){
if (ObjectUtil.isNotEmpty(imageBase64Standard) && !imageBase64Standard.startsWith("data:image")){
imageBase64Standard = "data:image/png;base64,"+imageBase64Standard;
}
return imageBase64Standard;
}
public String getImageBase64Hd() {
if (ObjectUtil.isNotEmpty(imageBase64Hd) && !imageBase64Hd.startsWith("data:image")){
imageBase64Hd = "data:image/png;base64,"+imageBase64Hd;
}
return imageBase64Hd;
}
public String getImageBase64() {
if (!imageBase64.startsWith("data:image")){
if (ObjectUtil.isNotEmpty(imageBase64) && !imageBase64.startsWith("data:image")){
imageBase64 = "data:image/png;base64,"+imageBase64;
}
return imageBase64;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package org.photo.modular.HivisionIDPhotos.service;

import org.photo.modular.HivisionIDPhotos.enums.HIDOptTypeEnum;
import org.photo.modular.HivisionIDPhotos.request.HIDAddBackgroundRequest;
import org.photo.modular.HivisionIDPhotos.request.HIDIdPhotoRequest;
import org.photo.modular.HivisionIDPhotos.response.HIDHivisionResponse;

import java.io.IOException;

public interface HivisionIDPhotosService {

/**
* 生成证件照
* 接口的逻辑是发送一张 RGB 图像,输出一张标准证件照和一张高清证件照:
* 高清证件照:根据size的宽高比例制作的证件照,文件名为output_image_dir增加_hd后缀
* 标准证件照:尺寸等于size,由高清证件照缩放而来,文件名为output_image_dir
* 需要注意的是,生成的两张照片都是透明的(RGBA 四通道图像),要生成完整的证件照,还需要下面的添加背景色接口。
* @param request
* @return
* @throws IOException
*/
HIDHivisionResponse idphoto(HIDIdPhotoRequest request) throws IOException;

/**
* 添加背景色
* 接口的逻辑是接收一张 RGBA 图像(透明图),根据color添加背景色,合成一张 JPG 图像。
* @param request
* @return
* @throws IOException
*/
HIDHivisionResponse add_background(HIDAddBackgroundRequest request) throws IOException;

/**
* 生成六寸排版照
* 接口的逻辑是接收一张 RGB 图像(一般为添加背景色之后的证件照),根据size进行照片排布,然后生成一张六寸排版照。
* @param request
* @return
* @throws IOException
*/
HIDHivisionResponse generate_layout_photos(HIDAddBackgroundRequest request) throws IOException;

/**
* 人像抠图
* 接口的逻辑是接收一张 RGB 图像,输出一张标准抠图人像照和高清抠图人像照(无任何背景填充)。
* @param request
* @return
* @throws IOException
*/
HIDHivisionResponse human_matting(HIDAddBackgroundRequest request) throws IOException;

/**
* 图像加水印
* 接口的功能是接收一个水印文本,然后在原图上添加指定的水印。用户可以指定水印的位置、透明度和大小等属性,以便将水印无缝地融合到原图中。
* @param request
* @return
* @throws IOException
*/
HIDHivisionResponse watermark(HIDAddBackgroundRequest request) throws IOException;
/**
* 设置图像KB大小
* 接口的功能是接收一张图像和目标文件大小(以KB为单位),如果设置的KB值小于原文件,则调整压缩率;如果设置的KB值大于源文件,则通过给文件头添加信息的方式调大KB值,目标是让图像的最终大小与设置的KB值一致。
* @param request
* @return
* @throws IOException
*/
HIDHivisionResponse set_kb(HIDAddBackgroundRequest request) throws IOException;

/**
* 证件照裁切
* 接口的功能是接收一张 RBGA 图像(透明图),输出一张标准证件照和一张高清证件照。
* @param request
* @return
* @throws IOException
*/
HIDHivisionResponse idphoto_crop(HIDAddBackgroundRequest request) throws IOException;


}
Loading

0 comments on commit 424a651

Please sign in to comment.