Ini adalah Java SDK dari API Gambar yang Dapat Diprogram Picsart dan API Picsart GenAI. Anda dapat dengan mudah melakukan banyak tindakan dengan gambar Anda hanya dengan menambahkan beberapa baris kode ke proyek Java atau Kotlin Anda.
Untuk menggunakan SDK, tambahkan dependensi berikut ke file Maven pom.xml
Anda:
< dependency >
< groupId >com.picsart groupId >
< artifactId >picsart-creative-apis-java-sdk artifactId >
< version >1.0.0 version >
dependency >
Jika Anda menggunakan Gradle, tambahkan yang berikut ini ke file build.gradle
Anda:
implementation ' com.picsart:picsart-creative-apis-java-sdk:1.0.0 '
Peringatan : SDK menggunakan Reactor java dan memiliki ketergantungan transitif pada io.projectreactor:reactor-core
. Jika Anda menggunakan versi Reactor yang berbeda, Anda mungkin menghadapi masalah kompatibilitas. Dalam hal ini, Anda dapat mengecualikan ketergantungan transitif dan menambahkan versi Reactor Anda sendiri ke proyek Anda.
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 ());
});
Untuk lebih jelasnya silakan periksa HapusBackgroundExample.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 ());
});
Untuk lebih jelasnya silakan periksa 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 ());
});
Untuk lebih jelasnya silakan periksa 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 ());
});
Untuk lebih jelasnya silakan periksa ListEffectsExample.java.
//Pratinjau Efek
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 ());
});
Untuk lebih jelasnya silakan periksa 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 ());
});
Untuk lebih jelasnya silakan periksa 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 ());
});
Untuk lebih jelasnya silakan periksa 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 ());
});
Untuk lebih jelasnya silakan periksa 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 ());
});
Untuk lebih jelasnya silakan periksa 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 ());
});
Untuk lebih jelasnya silakan periksa 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 ());
});
Untuk lebih jelasnya silakan periksa 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 ());
});
Untuk lebih jelasnya silakan periksa BackgroundTextureExample.java.
Mono < BalanceResult > resultMono = imageApi . balance ();
resultMono
. subscribe ( result -> { // non-blocking subscribe
System . out . println ( "Result credits: " + result . credits ());
});
Untuk lebih jelasnya silakan periksa 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 ());
});
Untuk lebih jelasnya silakan periksa Text2ImageExample.java.
Pada contoh di bawah ini kita membuat gambar dari teks, lalu menghapus latar belakang dari gambar yang dihasilkan, dan terakhir meningkatkan hasilnya.
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 ());
});
Untuk lebih jelasnya silakan periksa Text2ImageRemoveBackgroundUpscaleExample.java.
Pengecualian Api ditangani oleh SDK dan ditampilkan sebagai ApiException atau turunannya. Jika status respons selain 2xx, FailureResponseException
atau turunannya akan ditampilkan sesuai dengan kode status respons. Pengecualian yang berpotensi diselesaikan dengan mencoba kembali permintaan seperti ServiceUnavailableException
ditandai dengan antarmuka yang Dapat Dipulihkan. Anda dapat menangkap pengecualian tersebut dan mencoba kembali permintaan tersebut jika diperlukan.
Contoh mencoba ulang permintaan jika ada pengecualian yang dapat dipulihkan:
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 ());
});
SDK API Kreatif Picsart disediakan di bawah lisensi MIT yang dapat ditemukan di file LISENSI. Dengan menggunakan, mendistribusikan, atau berkontribusi pada proyek ini, Anda menyetujui syarat dan ketentuan lisensi ini.
Proyek ini memiliki beberapa ketergantungan pihak ketiga, yang masing-masing mungkin memiliki lisensi independen:
Jika Anda menyukai Picsart Creative APIs SDK dan ingin berkontribusi pada proyek sumber terbuka ini, silakan periksa panduan Kontribusi.