feat(java请求示例): 增加Java请求示例,生成证件照

This commit is contained in:
Ronaldoshuang
2024-09-05 16:04:47 +08:00
committed by GitHub
parent a18ce6957a
commit 9153357c97
+244 -10
View File
@@ -40,9 +40,11 @@ python delopy_api.py
<br>
## 1️⃣ Python Requests 请求方法
## Python 请求示例
### 1.生成证件照(底透明)
### 1️⃣ Python Requests 请求方法
#### 1.生成证件照(底透明)
```python
import requests
@@ -60,7 +62,7 @@ print(response)
```
### 2.添加背景色
#### 2.添加背景色
```python
import requests
@@ -77,7 +79,7 @@ response = requests.post(url, files=files, data=data).json()
print(response)
```
### 3.生成六寸排版照
#### 3.生成六寸排版照
```python
import requests
@@ -96,15 +98,15 @@ print(response)
<br>
## 2️⃣ Python 脚本请求方法
### 2️⃣ Python 脚本请求方法
```bash
python requests_api.py -u <URL> -t <TYPE> -i <INPUT_IMAGE_DIR> -o <OUTPUT_IMAGE_DIR> [--height <HEIGHT>] [--width <WIDTH>] [-c <COLOR>] [-k <KB>]
```
### 参数说明
#### 参数说明
#### 基本参数
##### 基本参数
- `-u`, `--url`
@@ -127,13 +129,12 @@ python requests_api.py -u <URL> -t <TYPE> -i <INPUT_IMAGE_DIR> -o <OUTPUT_IMAGE_
- **必需**: 是
- **示例**: `./output_images/processed_photo.jpg`
#### 可选参数
##### 可选参数
- `--height`,
- **描述**: 标准证件照的输出尺寸的高度。
- **默认值**: 413
- `--width`,
- **描述**: 标准证件照的输出尺寸的宽度。
@@ -189,3 +190,236 @@ python requests_api.py \
### 请求失败的情况
- 照片中检测到的人脸大于 1,则失败
## Java 请求示例
### 添加maven依赖
```java
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.16</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
```
### 运行代码
#### 1.生成证件照(底透明)
```java
/**
* 生成证件照(底透明) /idphoto 接口
* @param inputImageDir 文件地址
* @return
* @throws IOException
*/
public static String requestIdPhoto(String inputImageDir) throws IOException {
String url = BASE_URL+"/idphoto";
// 创建文件对象
File inputFile = new File(inputImageDir);
Map<String, Object> paramMap=new HashMap<>();
paramMap.put("input_image",inputFile);
paramMap.put("height","413");
paramMap.put("width","295");
//包含status、image_base64_standard和image_base64_hd三项
return HttpUtil.post(url, paramMap);
}
```
#### 2.添加背景色
```java
/**
* 添加背景色 /add_background 接口
* @param inputImageDir 文件地址
* @return
* @throws IOException
*/
public static String requestAddBackground(String inputImageDir) throws IOException {
String url = BASE_URL+"/add_background";
// 创建文件对象
File inputFile = new File(inputImageDir);
Map<String, Object> paramMap=new HashMap<>();
paramMap.put("input_image",inputFile);
paramMap.put("color","638cce");
paramMap.put("kb","200");
// response为一个json格式字典,包含status和image_base64
return HttpUtil.post(url, paramMap);
}
```
#### 3.生成六寸排版照
```java
/**
* 生成六寸排版照 /generate_layout_photos 接口
* @param inputImageDir 文件地址
* @return
* @throws IOException
*/
public static String requestGenerateLayoutPhotos(String inputImageDir) throws IOException {
String url = BASE_URL+"/generate_layout_photos";
// 创建文件对象
File inputFile = new File(inputImageDir);
Map<String, Object> paramMap=new HashMap<>();
paramMap.put("input_image",inputFile);
paramMap.put("height","413");
paramMap.put("width","295");
paramMap.put("kb","200");
//response为一个json格式字典,包含status和image_base64
return HttpUtil.post(url, paramMap);
}
```
#### 4.汇总
```java
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.apache.commons.io.FileUtils;
import org.springframework.util.StringUtils;
import java.io.File;
import java.io.IOException;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
/**
* @author: qingshuang
* @createDate: 2024/09/05
* @description: java生成证件照,测试用例
*/
public class Test {
/**
* 接口地址
*/
private final static String BASE_URL = "http://127.0.0.1:8080";
/**
* 生成证件照(底透明) /idphoto 接口
* @param inputImageDir 文件地址
* @return
* @throws IOException
*/
public static String requestIdPhoto(String inputImageDir) throws IOException {
String url = BASE_URL+"/idphoto";
// 创建文件对象
File inputFile = new File(inputImageDir);
Map<String, Object> paramMap=new HashMap<>();
paramMap.put("input_image",inputFile);
paramMap.put("height","413");
paramMap.put("width","295");
return HttpUtil.post(url, paramMap);
}
/**
* 添加背景色 /add_background 接口
* @param inputImageDir 文件地址
* @return
* @throws IOException
*/
public static String requestAddBackground(String inputImageDir) throws IOException {
String url = BASE_URL+"/add_background";
// 创建文件对象
File inputFile = new File(inputImageDir);
Map<String, Object> paramMap=new HashMap<>();
paramMap.put("input_image",inputFile);
paramMap.put("color","638cce");
paramMap.put("kb","200");
return HttpUtil.post(url, paramMap);
}
/**
* 生成六寸排版照 /generate_layout_photos 接口
* @param inputImageDir 文件地址
* @return
* @throws IOException
*/
public static String requestGenerateLayoutPhotos(String inputImageDir) throws IOException {
String url = BASE_URL+"/generate_layout_photos";
// 创建文件对象
File inputFile = new File(inputImageDir);
Map<String, Object> paramMap=new HashMap<>();
paramMap.put("input_image",inputFile);
paramMap.put("height","413");
paramMap.put("width","295");
paramMap.put("kb","200");
return HttpUtil.post(url, paramMap);
}
/**
* 生成证件照(底透明)
* @param inputImageDir 源文件地址
* @param outputImageDir 输出文件地址
* @throws IOException
*/
private static void requestIdPhotoToImage(String inputImageDir, String outputImageDir) throws IOException {
String res =requestIdPhoto(inputImageDir);
//转成json
JSONObject response= JSONUtil.parseObj(res);
if(response.getBool("status")){//请求接口成功
String image_base64_standard= response.getStr("image_base64_standard");
String image_base64_hd =response.getStr("image_base64_hd");
String[] outputImageDirArr= StringUtils.split(outputImageDir,".");
// Base64 保存为图片
FileUtils.writeByteArrayToFile(new File(outputImageDirArr[0]+"_standard."+outputImageDirArr[1]), Base64.getDecoder().decode(image_base64_standard));
FileUtils.writeByteArrayToFile(new File(outputImageDirArr[0]+"_hd."+outputImageDirArr[1]), Base64.getDecoder().decode(image_base64_hd));
}
}
/**
* 添加背景色
* @param inputImageDir 源文件地址
* @param outputImageDir 输出文件地址
* @throws IOException
*/
private static void requestAddBackgroundToImage(String inputImageDir, String outputImageDir) throws IOException {
String res =requestAddBackground(inputImageDir);
//转成json
JSONObject response= JSONUtil.parseObj(res);
if(response.getBool("status")){//请求接口成功
String image_base64= response.getStr("image_base64");
String[] outputImageDirArr= StringUtils.split(outputImageDir,".");
// Base64 保存为图片
FileUtils.writeByteArrayToFile(new File(outputImageDirArr[0]+"_background."+outputImageDirArr[1]), Base64.getDecoder().decode(image_base64));
}
}
/**
* 生成六寸排版照
* @param inputImageDir 源文件地址
* @param outputImageDir 输出文件地址
* @throws IOException
*/
private static void requestGenerateLayoutPhotosToImage(String inputImageDir, String outputImageDir) throws IOException {
String res =requestGenerateLayoutPhotos(inputImageDir);
//转成json
JSONObject response= JSONUtil.parseObj(res);
if(response.getBool("status")){//请求接口成功
String image_base64= response.getStr("image_base64");
String[] outputImageDirArr= StringUtils.split(outputImageDir,".");
// Base64 保存为图片
FileUtils.writeByteArrayToFile(new File(outputImageDirArr[0]+"_layout."+outputImageDirArr[1]), Base64.getDecoder().decode(image_base64));
}
}
public static void main(String[] args) {
try {
//生成证件照(底透明)
requestIdPhotoToImage("C:\\Users\\Administrator\\Desktop\\1111.jpg","C:\\Users\\Administrator\\Desktop\\2222.png");
//添加背景色
requestAddBackgroundToImage("C:\\Users\\Administrator\\Desktop\\2222_hd.png","C:\\Users\\Administrator\\Desktop\\idphoto_with_background.jpg");
//生成六寸排版照
requestGenerateLayoutPhotosToImage("C:\\Users\\Administrator\\Desktop\\1111.jpg","C:\\Users\\Administrator\\Desktop\\2222.png");
} catch (IOException e) {
e.printStackTrace();
}
}
}
```