Java SDK
This SDK is aimed at users who code in Java and Android, auto-signing and simplifying the calling process. Download and unzip the following file, put picpik-sdk-java-x.x.x.jar
in your project dependency:
Basic Class
Config
Picpik Configuration
/**
* Constructor
* apiUrl: Chinese mainland=https://api.picpikai.com/ , International=https://api.picpik.ai/
*/
Config(String apiUrl, String publicKey, String privateKey)
// set log level of http request
Config setLogLevel(HttpLoggingInterceptor.Level logLevel)
// set timeout about connect
Config setTimeoutConnect(Timeout timeout)
// set timeout about read
Config setTimeoutRead(Timeout timeout)
// set timeout about write
Config setTimeoutWrite(Timeout timeout)
Config.Timeout
/**
* timeout: timeout value
* timeUnit: timeout timeUnit
*/
new Timeout(long timeout, TimeUnit timeUnit);
Functions
void Picpik.init(Config config)
Init SDK。Return value: void
参数 | 类型 | 默认值 | 说明 |
---|---|---|---|
config | Config | None | SDK Configuration, See Config information. apiUrl: Chinese mainland=https://api.picpikai.com/ (opens in a new tab) , International=https://api.picpik.ai/。 (opens in a new tab) |
<T> T Picpik.call(Map<String, Object> params, Class<T> respClass)
Calls the API. Return value: T
.
Parameter | Type | Default | Description |
---|---|---|---|
params | Map<String, Object> | None | Calling parameters, consult the API documentation for more information. The parameters PublicKey , PrivateKey and Signature are not required. |
respClass | Class<T> | None | The generics of return value you need. Use Gson to parse the bean. |
<T> void Picpik.call(Map<String, Object> body, ResponseCallback<T> callback)
Calls the API Async. Return value: void
.
Parameter | Type | Default | Description |
---|---|---|---|
params | Map<String, Object> | None | Calling parameters, consult the API documentation for more information. The parameters PublicKey , PrivateKey and Signature are not required. |
callback | ResponseCallback<T> | None | The interface to callback the async call. Generics:the dataBean.class you need. Use Gson to parse the bean. |
String Picpik.localImg2Base64(String path)
Get an image or mask from a local disk and encode it in Base64 format. Return value: String
Parameter | Type | Default | Description |
---|---|---|---|
path | String | None | The file path of an image or mask. |
String Picpik.remoteImg2Base64(String url)
Get an image or mask remotely and encode it in Base64 format. Return value: String
Parameter | Type | Default | Description |
---|---|---|---|
url | String | None | The URL address of an image or mask. If the link does not start with "https://", the SDK will try to automatically fix it. |
TaskDetail Picpik.pollingTask(String taskUID, int interval, ResponseCallback<TaskDetail> callback)
Poll a task. Return value: TaskDetail
.
Parameter | Type | Default | Description |
---|---|---|---|
taskUID | String | None | The task ID was obtained after calling the Generate and Modify class APIs. |
interval | Int | None | Polling interval in milliseconds. |
callback | Function | null | Optional. A callback function that returns an TaskDetail containing detailed information about the current task.If the interval isn't set, the callback can be used directly as the second argument. |
Environment dependency
Take Maven pom.xml as an example
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.10.1</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.12.0</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>logging-interceptor</artifactId>
<version>4.12.0</version>
</dependency>
Demo
A demo of generating a bird and remove the background.
import ai.picpik.sdk.Config;
import ai.picpik.sdk.Picpik;
import ai.picpik.sdk.ResponseCallback;
import ai.picpik.sdk.bean.TaskDetail;
import com.google.gson.JsonObject;
import java.util.HashMap;
import java.util.Map;
// Chinese mainland
String urlChinese = "https://api.picpikai.com/";
// International
String urlInternational = "https://api.picpik.ai/";
// Setting Picpik Gateway, and your public & private key which to call the APIs. Keys can be found on the user account page after logging into the official website.
Config config = new Config("https://api.picpik.ai/", "your_public_key", "your_private_key");
// config.setLogLevel to set the log level. And .setTimeout[Connect,Read,Write] to set the timeout in each part of http request.
config.setLogLevel(HttpLoggingInterceptor.Level.BASIC);
// Init SDK with the Config.
Picpik.init(config);
// Get the models' list. A main model and relevant parameters are required when generating images. Different from calling the APIs directly, the SDK will automatically provide common
Map<String, Object> params = new HashMap<>();
params.put("Action", "ListModels");
JsonObject data = Picpik.call(params);
JsonObject mainModels = data.getAsJsonObject("MainModels");
JsonObject model = mainModels.getAsJsonObject("PICPIK General");
// Generate an image by specifying a model from the models' list.
Map<String, Object> params = new HashMap<>();
params.put("Action", "GenerateImages");
params.put("MainModel", "model");
params.put("PositivePrompts", ["One Bird"]);
JsonObject data = Picpik.call(params);
String taskUID = data.get("TaskUID").getAsString();
// Polling the task by it's task UID.
// Each polling results is returned through the ResponseCallback<TaskDetail> callback.
// Until the Task status is `Finished`, the method returns the final result. An exception will be thrown if an error is encountered.
TaskDetail detail = Picpik.pollingTask(taskUID, 1000, new ResponseCallback<>() {
@Override
public void onError(Throwable e) {
e.printStackTrace();
// It's never be called with .pollingTask
}
@Override
public void onResponse(TaskDetail response) {
// Each polling results
}
});
// Get the URL of the generated image and encode it in Base64.
String imageURL = detail.generatedImageList.get(0);
String base64Img = Picpik.remoteImg2Base64(imageURL);
// Remove the background
Map<String, Object> params = new HashMap<>();
params.put("Action", "RemoveBackground");
params.put("RawImage", base64Img);
data = Picpik.call(params, JsonObject.class);
taskUID = data.get("TaskUID").getAsString();
// Polling the task again by it's task UID. If you don't need to poll the status every time, you don't need to set the callback.
detail = Picpik.pollingTask(taskUID, 1000, null);
// Get the URL of the generated image after finishing the task.
imageURL = "https://" + detail.generatedImageList.get(0)
System.out.println(imageURL);