这是 Picsart 可编程图像 API 和 Picsart GenAI API 的 Java SDK。只需向 Java 或 Kotlin 项目添加几行代码,您就可以轻松地对图像执行许多操作。
要使用 SDK,请将以下依赖项添加到 Maven pom.xml
文件中:
< dependency >
< groupId >com.picsart groupId >
< artifactId >picsart-creative-apis-java-sdk artifactId >
< version >1.0.0 version >
dependency >
如果您使用 Gradle,请将以下内容添加到您的build.gradle
文件中:
implementation ' com.picsart:picsart-creative-apis-java-sdk:1.0.0 '
警告:该 SDK 使用 Reactor java 并且对io.projectreactor:reactor-core
有传递依赖。如果您使用不同版本的 Reactor,您可能会遇到兼容性问题。在这种情况下,您可以排除传递依赖项并将您自己的 Reactor 版本添加到您的项目中。
import com . picsart . creativeapis . PicsartEnterprise ;
import com . picsart . creativeapis . image . ImageApi ;
import com . picsart . creativeapis . genai . GenAIApi ;
// Get an instance of Image API with default baseUrl and timeout
ImageApi imageApi = PicsartEnterprise . createImageApi ( "YOUR_API_KEY" );
// Get an instance of Image API with custom baseUrl and timeout
ImageApi customBaseUrlImageApi = PicsartEnterprise . createImageApi ( "YOUR_API_KEY" )
. withBaseUrl ( "https://custom-base-url.com" )
. withTimeout ( Duration . ofSeconds ( 10 ));
// Get an instance of GenAI API
GenAIApi genaiApi = PicsartEnterprise . createGenAIApi ( "YOUR_API_KEY" );
// Get an instance of GenAI API with custom baseUrl and timeout
GenAIApi customBaseUrlGenaiApi = PicsartEnterprise . createGenAIApi ( "YOUR_API_KEY" )
. withBaseUrl ( "https://custom-base-url.com" )
. withTimeout ( Duration . ofSeconds ( 10 ));
import com . picsart . busobj . image . ImageSource ;
import com . picsart . creativeapis . image . ImageApi ;
import com . picsart . creativeapis . image . models . ImageSource ;
import java . io . File ;
var imageUrl = ImageSource . fromUrl ( "https://url-to-your-image.jpg" );
// or
var imageFile = ImageSource . fromFile ( new File ( "path/to/your-image.jpg" ));
// or
var imageId = ImageSource . fromImageId ( "your-image-id" );
RemoveBackgroundParameters parameters = RemoveBackgroundParameters . builder ( mainImageSource )
. bgImage ( backgroundImageSource )
. build ();
Mono < RemoveBackgroundResult > removeBackgroundResultMono = imageApi . removeBackground ( parameters );
removeBackgroundResultMono
. subscribe ( result -> { // non-blocking subscribe
System . out . println ( "Result Image: " + result . image ());
System . out . println ( "Result metadata: " + result . metadata ());
});
有关更多详细信息,请查看RemoveBackgroundExample.java。
AdjustParameters parameters = AdjustParameters . builder ( imageSource )
. brightness ( 20 )
. contrast ( 30 )
. saturation ( 40 )
. build ();
Mono < AdjustResult > adjustResultMono = imageApi . adjust ( parameters );
adjustResultMono
. subscribe ( result -> { // non-blocking subscribe
System . out . println ( "Result Image: " + result . image ());
System . out . println ( "Result metadata: " + result . metadata ());
});
欲了解更多详细信息,请查看AdjustExample.java。
EffectParameters parameters = EffectParameters . builder ( imageSource , effectName )
. build ();
Mono < EffectResult > resultMono = imageApi . effect ( parameters );
resultMono
. subscribe ( result -> { // non-blocking subscribe
System . out . println ( "Result Image: " + result . image ());
System . out . println ( "Result metadata: " + result . metadata ());
});
有关更多详细信息,请查看 EffectExample.java。
Mono < ListEffectsResult > resultMono = imageApi . listEffects ();
resultMono
. subscribe ( result -> { // non-blocking subscribe
System . out . println ( "Effects: " + result . effects ());
System . out . println ( "Result metadata: " + result . metadata ());
});
有关更多详细信息,请查看 ListEffectsExample.java。
//效果预览
EffectsPreviewsParameters parameters = EffectsPreviewsParameters . builder ( imageSource )
. addEffectName ( effectName1 )
. addEffectName ( effectName2 )
. addEffectName ( effectName3 )
. format ( ImageFormat . PNG )
. build ();
Mono < EffectsPreviewsResult > resultMono = imageApi . effectsPreviews ( parameters );
resultMono
. subscribe ( result -> { // non-blocking subscribe
System . out . println ( "Result images with effects" + result . effectsPreviews ());
System . out . println ( "Result metadata: " + result . metadata ());
});
有关更多详细信息,请查看 EffectsPreviewsExample.java。
Mono < UploadResult > resultMono = imageApi . upload ( imageFile ); // either imageFile or imageUrl
resultMono
. subscribe ( result -> { // non-blocking subscribe
System . out . println ( "Result Image: " + result . image ());
System . out . println ( "Result metadata: " + result . metadata ());
});
有关更多详细信息,请查看 UploadExample.java。
UpscaleParameters parameters = UpscaleParameters . builder ( imageSource )
. upscaleFactor ( 4 )
. format ( ImageFormat . PNG )
. build ();
Mono < UpscaleResult > resultMono = imageApi . upscale ( parameters );
resultMono
. subscribe ( result -> { // non-blocking subscribe
System . out . println ( "Result Image: " + result . image ());
System . out . println ( "Result metadata: " + result . metadata ());
});
有关更多详细信息,请查看 UpscaleExample.java。
UltraUpscaleParameters parameters = UltraUpscaleParameters . builder ( imageSource )
. mode ( UpscaleMode . ASYNC )
. upscaleFactor ( 4 )
. format ( ImageFormat . PNG )
. build ();
Mono < UltraUpscaleResult > resultMono = imageApi . ultraUpscale ( parameters );
resultMono
. subscribe ( result -> { // non-blocking subscribe
System . out . println ( "Result Image: " + result . image ());
System . out . println ( "Result metadata: " + result . metadata ());
});
有关更多详细信息,请查看 UltraUpscaleExample.java。
UltraEnhanceParameters parameters = UltraEnhanceParameters . builder ( imageSource )
. upscaleFactor ( 4 )
. format ( ImageFormat . PNG )
. build ();
Mono < UltraEnhanceResult > resultMono = imageApi . ultraEnhance ( parameters );
resultMono
. subscribe ( result -> { // non-blocking subscribe
System . out . println ( "Result Image: " + result . image ());
System . out . println ( "Result metadata: " + result . metadata ());
});
有关更多详细信息,请查看 UltraEnhanceExample.java。
EnhanceFaceParameters parameters = EnhanceFaceParameters . builder ( imageSource )
. format ( ImageFormat . PNG )
. build ();
Mono < EnhanceFaceResult > resultMono = imageApi . enhanceFace ( parameters );
resultMono
. subscribe ( result -> { // non-blocking subscribe
System . out . println ( "Result Image: " + result . image ());
System . out . println ( "Result metadata: " + result . metadata ());
});
有关更多详细信息,请查看EnhanceFaceExample.java。
SurfaceMapParameters parameters = SurfaceMapParameters . builder ( mainImageSource , maskSource , stickerSource )
. format ( ImageFormat . PNG )
. build ();
Mono < SurfaceMapResult > resultMono = imageApi . surfaceMap ( parameters );
resultMono
. subscribe ( result -> { // non-blocking subscribe
System . out . println ( "Result Image: " + result . image ());
System . out . println ( "Result metadata: " + result . metadata ());
});
有关更多详细信息,请查看 SurfaceMapExample.java。
BackgroundTextureParameters parameters = BackgroundTextureParameters . builder ( imageSource )
. format ( ImageFormat . PNG )
. width ( 800 )
. height ( 800 )
. pattern ( BackgroundTexturePattern . DIAMOND )
. build ();
Mono < BackgroundTextureResult > resultMono = imageApi . backgroundTexture ( parameters );
resultMono
. subscribe ( result -> { // non-blocking subscribe
System . out . println ( "Result Image: " + result . image ());
System . out . println ( "Result metadata: " + result . metadata ());
});
有关更多详细信息,请查看BackgroundTextureExample.java。
Mono < BalanceResult > resultMono = imageApi . balance ();
resultMono
. subscribe ( result -> { // non-blocking subscribe
System . out . println ( "Result credits: " + result . credits ());
});
有关更多详细信息,请查看 BalanceExample.java。
Text2ImageParameters parameters = Text2ImageParameters . builder ( "your-prompt" , "your-negative-prompt" )
. count ( 1 )
. width ( 512 )
. height ( 512 )
. build ();
Mono < Text2ImageResult > resultMono = genAIApi . text2Image ( parameters );
resultMono
. subscribe ( result -> { // non-blocking subscribe
System . out . println ( "Result Images: " + result . images ());
System . out . println ( "Result metadata: " + result . metadata ());
});
有关更多详细信息,请查看 Text2ImageExample.java。
在下面的示例中,我们从文本生成图像,然后从生成的图像中删除背景,最后放大结果。
Text2ImageParameters parameters = Text2ImageParameters . builder ( "your-prompt" , "your-negative-prompt" )
. count ( 10 )
. width ( 512 )
. height ( 512 )
. build ();
Mono < Text2ImageResult > resultMono = genAIApi . text2Image ( parameters );
resultMono
. flatMapIterable ( Text2ImageResult :: images ) // flatMapIterable is used to iterate over the result images and process each of them individually
. flatMap ( image -> { // this will be called for each image in the result (10 times in this case)
RemoveBackgroundParameters removeBackgroundParameters = RemoveBackgroundParameters
. builder ( image . toImageSource ())
. build ();
return imageApi . removeBackground ( removeBackgroundParameters ); // remove background from each image
})
. flatMap ( removeBackgroundResult -> { // this will be called for each image in the result (10 times in this case)
var imageSource = removeBackgroundResult . image (). toImageSource ();
UpscaleParameters upscaleParameters = UpscaleParameters . builder ( imageSource )
. upscaleFactor ( 2 )
. build ();
return imageApi . upscale ( upscaleParameters ); // upscale each image
})
. subscribe ( result -> { // non-blocking subscribe
// each image will be printed separately
System . out . println ( "Result Image: " + result . image ());
System . out . println ( "Result metadata: " + result . metadata ());
});
有关更多详细信息,请查看 Text2ImageRemoveBackgroundUpscaleExample.java。
Api 异常由 SDK 处理,并作为 ApiException 或其子异常抛出。如果响应状态不是 2xx,则根据响应状态代码抛出FailureResponseException
或其子异常。可以通过重试请求来解决的异常(例如ServiceUnavailableException
被标记为 Recoverable 接口。您可以捕获这些异常并在需要时重试请求。
发生可恢复异常时重试请求的示例:
Mono < BalanceResult > resultMono = imageApi . balance ();
resultMono
// can be used to perform a side-effect in case of an error
. doOnError ( throwable -> {
System . out . println ( "exception: " + throwable . getMessage ());
})
// Retry 3 times with an initial backoff of 2 seconds.
// The delay between retries will increase exponentially up to max delay of 5 seconds.
. retryWhen ( Retry . backoff ( 3 , Duration . ofSeconds ( 2 ))
. maxBackoff ( Duration . ofSeconds ( 5 ))
. filter ( throwable -> throwable instanceof Recoverable )
. onRetryExhaustedThrow (( retryBackoffSpec , retrySignal ) -> new RuntimeException ( "Retry exhausted" , retrySignal . failure ()))
)
. subscribe ( result -> { // non-blocking subscribe
System . out . println ( "Result credits: " + result . credits ());
});
Picsart Creative API SDK 是根据 MIT 许可证提供的,可以在许可证文件中找到该许可证。通过使用、分发或为本项目做出贡献,您同意本许可的条款和条件。
该项目有一些第三方依赖项,每个依赖项都可能具有独立的许可:
如果您喜欢 Picsart Creative APIs SDK 并希望为这个开源项目做出贡献,请查看贡献指南。