這是 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 並希望為這個開源專案做出貢獻,請查看貢獻指南。