Skip to content

Commit

Permalink
docs: update java api
Browse files Browse the repository at this point in the history
  • Loading branch information
Zeyi-Lin committed Sep 5, 2024
1 parent a1a2d85 commit 131f8e3
Show file tree
Hide file tree
Showing 2 changed files with 266 additions and 14 deletions.
12 changes: 10 additions & 2 deletions docs/api_CN.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# API Docs

## 目录

- [开始之前:开启后端服务](#开始之前开启后端服务)
- [接口功能说明](#接口功能说明)
- [Python 请求示例](#python-请求示例)
- [Python Requests 请求方法](#1️⃣-python-requests-请求方法)
- [Python 脚本请求方法](#2️⃣-python-脚本请求方法)
- [Java 请求示例](#java-请求示例)

## 开始之前:开启后端服务

在请求 API 之前,请先运行后端服务
Expand Down Expand Up @@ -134,7 +143,6 @@ python requests_api.py -u <URL> -t <TYPE> -i <INPUT_IMAGE_DIR> -o <OUTPUT_IMAGE_
- `--height`,
- **描述**: 标准证件照的输出尺寸的高度。
- **默认值**: 413

- `--width`,

- **描述**: 标准证件照的输出尺寸的宽度。
Expand Down Expand Up @@ -193,7 +201,7 @@ python requests_api.py \

## Java 请求示例

### 添加maven依赖
### 添加 maven 依赖

```java
<dependency>
Expand Down
268 changes: 256 additions & 12 deletions docs/api_EN.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# API Documentation

## TOC

- [Before You Start: Launch the Backend Service](#before-you-start-launch-the-backend-service)
- [Interface Function Descriptions](#interface-function-descriptions)
- [Python Request Example](#python-request-example)
- [Python Requests Method](#1️⃣-python-requests-method)
- [Python Script Method](#2️⃣-python-script-request-method)
- [Java Request Example](#java-request-example)

## Before You Start: Launch the Backend Service

Before making API requests, please run the backend service:
Expand Down Expand Up @@ -40,9 +49,11 @@ The `Generate 6-inch Layout Photo` interface logic involves sending an RGB image

<br>

## 1️⃣ Python Requests Method
## Python Request Example

### 1. Generate ID Photo (Transparent Background)
### 1️⃣ Python Requests Method

#### 1. Generate ID Photo (Transparent Background)

```python
import requests
Expand All @@ -59,7 +70,7 @@ response = requests.post(url, files=files, data=data).json()
print(response)
```

### 2. Add Background Color
#### 2. Add Background Color

```python
import requests
Expand All @@ -76,7 +87,7 @@ response = requests.post(url, files=files, data=data).json()
print(response)
```

### 3. Generate 6-inch Layout Photo
#### 3. Generate 6-inch Layout Photo

```python
import requests
Expand All @@ -95,15 +106,15 @@ print(response)

<br>

## 2️⃣ Python Script Request Method
### 2️⃣ Python Script Request Method

```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>]
```

### Parameter Descriptions
#### Parameter Descriptions

#### Basic Parameters
##### Basic Parameters

- `-u`, `--url`

Expand All @@ -126,7 +137,7 @@ python requests_api.py -u <URL> -t <TYPE> -i <INPUT_IMAGE_DIR> -o <OUTPUT_IMAGE_
- **Required**: Yes
- **Example**: `./output_images/processed_photo.jpg`

#### Optional Parameters
##### Optional Parameters

- `--height`

Expand All @@ -148,7 +159,7 @@ python requests_api.py -u <URL> -t <TYPE> -i <INPUT_IMAGE_DIR> -o <OUTPUT_IMAGE_
- **Default Value**: `None`
- **Example**: `50`

### 1. Generate ID Photo (Transparent Background)
#### 1. Generate ID Photo (Transparent Background)

```bash
python requests_api.py \
Expand All @@ -160,7 +171,7 @@ python requests_api.py \
--width 295
```

### 2. Add Background Color
#### 2. Add Background Color

```bash
python requests_api.py \
Expand All @@ -172,7 +183,7 @@ python requests_api.py \
-k 50
```

### 3. Generate 6-inch Layout Photo
#### 3. Generate 6-inch Layout Photo

```bash
python requests_api.py \
Expand All @@ -185,6 +196,239 @@ python requests_api.py \
-k 200
```

### Request Failure Scenarios
#### Request Failure Scenarios

- The request fails if more than one face is detected in the photo.

## Java Request Example

### Add Maven Dependency

```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>
```

### Running the Code

#### 1. Generate ID Photo (Transparent Background)

```java
/**
* Generate ID Photo (Transparent Background) /idphoto interface
* @param inputImageDir File address
* @return
* @throws IOException
*/
public static String requestIdPhoto(String inputImageDir) throws IOException {
String url = BASE_URL+"/idphoto";
// Create file object
File inputFile = new File(inputImageDir);
Map<String, Object> paramMap=new HashMap<>();
paramMap.put("input_image",inputFile);
paramMap.put("height","413");
paramMap.put("width","295");
// Contains status, image_base64_standard, and image_base64_hd
return HttpUtil.post(url, paramMap);
}
```

#### 2. Add Background Color

```java
/**
* Add Background Color /add_background interface
* @param inputImageDir File address
* @return
* @throws IOException
*/
public static String requestAddBackground(String inputImageDir) throws IOException {
String url = BASE_URL+"/add_background";
// Create file object
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 is a JSON dictionary containing status and image_base64
return HttpUtil.post(url, paramMap);
}
```

#### 3. Generate 6-inch Layout Photo

```java
/**
* Generate 6-inch Layout Photo /generate_layout_photos interface
* @param inputImageDir File address
* @return
* @throws IOException
*/
public static String requestGenerateLayoutPhotos(String inputImageDir) throws IOException {
String url = BASE_URL+"/generate_layout_photos";
// Create file object
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 is a JSON dictionary containing status and image_base64
return HttpUtil.post(url, paramMap);
}
```

#### 4. Summary

```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 generate ID photo, test case
*/
public class Test {
/**
* Interface address
*/
private final static String BASE_URL = "http://127.0.0.1:8080";

/**
* Generate ID Photo (Transparent Background) /idphoto interface
* @param inputImageDir File address
* @return
* @throws IOException
*/
public static String requestIdPhoto(String inputImageDir) throws IOException {
String url = BASE_URL+"/idphoto";
// Create file object
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 Color /add_background interface
* @param inputImageDir File address
* @return
* @throws IOException
*/
public static String requestAddBackground(String inputImageDir) throws IOException {
String url = BASE_URL+"/add_background";
// Create file object
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 6-inch Layout Photo /generate_layout_photos interface
* @param inputImageDir File address
* @return
* @throws IOException
*/
public static String requestGenerateLayoutPhotos(String inputImageDir) throws IOException {
String url = BASE_URL+"/generate_layout_photos";
// Create file object
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);
}
/**
* Generate ID Photo (Transparent Background)
* @param inputImageDir Source file address
* @param outputImageDir Output file address
* @throws IOException
*/
private static void requestIdPhotoToImage(String inputImageDir, String outputImageDir) throws IOException {
String res =requestIdPhoto(inputImageDir);
// Convert to JSON
JSONObject response= JSONUtil.parseObj(res);
if(response.getBool("status")){// Request interface success
String image_base64_standard= response.getStr("image_base64_standard");
String image_base64_hd =response.getStr("image_base64_hd");
String[] outputImageDirArr= StringUtils.split(outputImageDir,".");
// Base64 save as image
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));
}
}
/**
* Add Background Color
* @param inputImageDir Source file address
* @param outputImageDir Output file address
* @throws IOException
*/
private static void requestAddBackgroundToImage(String inputImageDir, String outputImageDir) throws IOException {
String res =requestAddBackground(inputImageDir);
// Convert to JSON
JSONObject response= JSONUtil.parseObj(res);
if(response.getBool("status")){// Request interface success
String image_base64= response.getStr("image_base64");
String[] outputImageDirArr= StringUtils.split(outputImageDir,".");
// Base64 save as image
FileUtils.writeByteArrayToFile(new File(outputImageDirArr[0]+"_background."+outputImageDirArr[1]), Base64.getDecoder().decode(image_base64));
}
}
/**
* Generate 6-inch Layout Photo
* @param inputImageDir Source file address
* @param outputImageDir Output file address
* @throws IOException
*/
private static void requestGenerateLayoutPhotosToImage(String inputImageDir, String outputImageDir) throws IOException {
String res =requestGenerateLayoutPhotos(inputImageDir);
// Convert to JSON
JSONObject response= JSONUtil.parseObj(res);
if(response.getBool("status")){// Request interface success
String image_base64= response.getStr("image_base64");
String[] outputImageDirArr= StringUtils.split(outputImageDir,".");
// Base64 save as image
FileUtils.writeByteArrayToFile(new File(outputImageDirArr[0]+"_layout."+outputImageDirArr[1]), Base64.getDecoder().decode(image_base64));
}
}

public static void main(String[] args) {
try {
// Generate ID Photo (Transparent Background)
requestIdPhotoToImage("C:\\Users\\Administrator\\Desktop\\1111.jpg","C:\\Users\\Administrator\\Desktop\\2222.png");
// Add Background Color
requestAddBackgroundToImage("C:\\Users\\Administrator\\Desktop\\2222_hd.png","C:\\Users\\Administrator\\Desktop\\idphoto_with_background.jpg");
// Generate 6-inch Layout Photo
requestGenerateLayoutPhotosToImage("C:\\Users\\Administrator\\Desktop\\1111.jpg","C:\\Users\\Administrator\\Desktop\\2222.png");

} catch (IOException e) {
e.printStackTrace();
}
}
}

```

0 comments on commit 131f8e3

Please sign in to comment.