A cikin wannan jagorar, za mu nuna muku yadda ake amfani da Java don musanya JPG, PNG, JPEG, da GIF tsarin hoto zuwa WebP. Tsarin yana da sauƙi kuma mai sauƙi, kuma ana iya haɗa shi cikin sauƙi cikin aikace-aikacen Java ɗin da kuke da shi. Ko kuna buƙatar canza hoto guda ɗaya ko tsarin tsari da yawa, jagoranmu yana sauƙaƙa sauya JPG zuwa WebP, PNG zuwa WebP, JPEG zuwa WebP, da GIF zuwa WebP cikin sauƙi.
Mun san cewa Hotunan JPEG suna da ƙarfi sosai, masu ɗaukar nauyi kuma suna dacewa da kusan dukkan na’urori. Hakanan, Hotunan JPEG masu girma suna da ƙarfi da launuka. Hakazalika, matsawa a cikin PNG ba shi da asara kuma yana da kyau ga cikakkun bayanai, manyan hotuna. Koyaya, tsarin WebP ɗin Google ne ya haɓaka kuma sanannen tsarin hoto ne na zamani wanda ke ba da mafi girman asara da matsi ga hotuna akan gidan yanar gizo. Gabaɗaya ya fi ƙanƙanta (25% -34%) fiye da JPG, PNG da sauransu kuma saboda haka gidajen yanar gizo suna ɗaukar sauri. Yanzu bari mu bincika cikakkun bayanai kan Yadda ake canza JPG zuwa WebP a Java.
- JPG zuwa WebP REST API
- Canza JPG zuwa WebP a Java
- PNG zuwa WebP a Java
- GIF zuwa WebP ta amfani da Umarnin CURL
JPG zuwa WebP REST API
Aspose.Imaging Cloud SDK for Java shine ɗayan mafi kyawun mafitarmu waɗanda ke ba da ingantaccen sarrafa hoto da ikon canza hoto. Samar da damar sarrafa hoto (gyara da canzawa) a cikin aikace-aikacen Java ɗinku ta amfani da layukan lamba masu sauƙi. API ɗin yana da ikon sarrafa hotunan raster, fayilolin Photoshop, Metafiles da tsarin Yanar gizo. Yanzu kamar girman wannan labarin, za mu yi amfani da shi don canza jpg zuwa webp a aikace-aikacen Java. Yanzu mataki na farko shine ƙara bayanin API a cikin aikin java ɗin mu ta haɗa da bin bayanai a cikin pom.xml (maven build type project).
<repositories>
<repository>
<id>aspose-cloud</id>
<name>artifact.aspose-cloud-releases</name>
<url>https://artifact.aspose.cloud/repo</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-imaging-cloud</artifactId>
<version>22.4</version>
</dependency>
</dependencies>
Da zarar an ƙara bayanin REST API a cikin aikin Java, da fatan za a sami takaddun shaidar abokin ciniki daga Cloud Dashboard. In ba haka ba, kuna buƙatar fara rajistar asusun kyauta yayin amfani da ingantaccen adireshin imel.
Canza JPG zuwa WebP a Java
A cikin wannan sashe, za mu tattauna duk cikakkun bayanai masu alaƙa da JPG zuwa jujjuyawar Yanar gizo ta amfani da snippet code na Java.
- Da farko, ƙirƙiri wani abu na ImagingApi ta amfani da keɓaɓɓen takaddun shaida
- Abu na biyu, loda hoton JPG ta amfani da hanyar readAllBytes(…) sannan ka wuce ƙimar da aka dawo da shi zuwa tsararrun byte[]
- Na uku, ƙirƙiri misali na UploadFileRequest ta samar da hoton JPEG azaman gardama kuma loda shi zuwa ma’ajiyar girgije ta amfani da hanyar uploadFile(…)
- Yanzu ƙirƙirar wani abu na ConvertImageRequest wanda ke ɗaukar sunan hoton JPEG da tsarin ‘webp’ azaman muhawara
- Kira hanyar canzaImage(…) don yin JPG zuwa Juyin Yanar Gizo. Ana dawo da fitarwa azaman rafin amsawa
- A ƙarshe, ajiye sakamakon WebP zuwa faifan gida ta amfani da abu FileOutputStream
// Samu ClientID da ClientSecret daga https://dashboard.aspose.cloud/
String clientId = "bb959721-5780-4be6-be35-ff5c3a6aa4a2";
String clientSecret = "4d84d5f6584160cbd91dba1fe145db14";
// haifar da Hoto abu
ImagingApi imageApi = new ImagingApi(clientSecret, clientId);
// shigar da hoton JPEG daga faifan gida
File file1 = new File("butterfly-yellow.jpeg");
byte[] imageStream = Files.readAllBytes(file1.toPath());
// ƙirƙiri abin buƙatar loda fayil
UploadFileRequest uploadRequest = new UploadFileRequest("input.jpg",imageStream,null);
// loda hoton jpg zuwa ma'ajiyar gajimare
imageApi.uploadFile(uploadRequest);
// saka tsarin fitarwa azaman WebP
String format = "webp";
// Ƙirƙiri abin buƙatar canza hoto
ConvertImageRequest convertImage = new ConvertImageRequest("input.jpg", format, null, null);
// canza JPG zuwa WebP kuma dawo da fitarwa a cikin rafin amsawa
byte[] resultantImage = imageApi.convertImage(convertImage);
// Ajiye sakamakon WebP zuwa faifan gida
FileOutputStream fos = new FileOutputStream("/Users/nayyer/Documents/" + "output.webp");
fos.write(resultantImage);
fos.close();
PNG zuwa WebP a Java
Bari mu tattauna cikakkun bayanai kan yadda za mu iya yin PNG zuwa WebP jujjuyawar ba tare da amfani da ma’ajiyar girgije ba. Ana loda hoton tushen daga faifan gida kuma bayan juyawa, ana kuma adana fayil ɗin sakamako akan rumbun kwamfutarka na gida.
- Da farko, ƙirƙiri wani abu na ImagingApi ta amfani da keɓaɓɓen takaddun shaida
- Na biyu, loda hoton PNG ta amfani da hanyar readAllBytes(…) kuma wuce zuwa byte[] tsararru.
- Na uku, ƙirƙiri wani abu na CreateConvertedImageRequest wanda ke ɗaukar sunan hoton PNG da tsarin gidan yanar gizo azaman muhawara.
- Yanzu kira hanyar ƙirƙirarConvertedImage(…) don yin PNG zuwa Juyin Yanar Gizo. Ana mayar da fitarwa azaman rafin amsawa
- A ƙarshe, ajiye sakamakon WenP zuwa faifan gida ta amfani da abu FileOutputStream
// Samu ClientID da ClientSecret daga https://dashboard.aspose.cloud/
String clientId = "bb959721-5780-4be6-be35-ff5c3a6aa4a2";
String clientSecret = "4d84d5f6584160cbd91dba1fe145db14";
// haifar da Hoto abu
ImagingApi imageApi = new ImagingApi(clientSecret, clientId);
// ɗora hoton PNG daga faifan gida
File file1 = new File("source.png");
byte[] imageStream = Files.readAllBytes(file1.toPath());
// saka tsarin fitarwa azaman WebP
String format = "webp";
// Ƙirƙiri abin buƙatar canza hoto
CreateConvertedImageRequest convertRequest = new CreateConvertedImageRequest(imageStream,format,null,null);
// canza PNG zuwa WebP kuma dawo da fitarwa a cikin rafin amsawa
byte[] resultantImage = imageApi.createConvertedImage(convertRequest);
// Ajiye sakamakon WebP zuwa faifan gida
FileOutputStream fos = new FileOutputStream("/Users/nayyer/Documents/" + "output.webp");
fos.write(resultantImage);
fos.close();
Lura: - Domin adana sakamakon WebP akan ajiyar girgije, wuce bayanan hanyar yanar gizo yayin ƙirƙirar wani abu na CreateConvertedImageRequest. A wannan hanya, mataki na ƙarshe don adana rafin fitarwa zuwa tuƙi na gida za a tsallake shi.
GIF zuwa WebP ta amfani da Umarnin CURL
Banda lambar Java, REST API ɗin kuma ana iya samun dama ga umarnin cURL. Don haka a cikin wannan sashe, za mu yi GIF zuwa WebP hira ta amfani da umarnin CURL. Yanzu abin da ake bukata shine don samar da alamar samun damar JWT (dangane da shaidar abokin ciniki) ta amfani da umarni mai zuwa.
curl -v "https://api.aspose.cloud/connect/token" \
-X POST \
-d "grant_type=client_credentials&client_id=bb959721-5780-4be6-be35-ff5c3a6aa4a2&client_secret=4d84d5f6584160cbd91dba1fe145db14" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept: application/json"
Da zarar an ƙirƙiri JWT, da fatan za a aiwatar da umarni mai zuwa don yin GIF zuwa Juyin Yanar gizo kuma adana fitarwa zuwa faifan gida.
curl -X -v GET "https://api.aspose.cloud/v3.0/imaging/UpdateGIFSampleImage.gif/convert?format=webp" \
-H "accept: application/json" \
-H "authorization: Bearer <JWT>" \
-o "output.webp"
Kammalawa
Labarin ya ba da duk cikakkun bayanai masu mahimmanci akan canza JPG zuwa WebP, PNG zuwa WebP da GIF zuwa WebP ta amfani da Java. A lokaci guda, mun kuma koyi matakan canza GIF zuwa WebP ta amfani da umarnin CURL. Da fatan za a sani cewa muna ƙarfafa ku sosai don bincika Takardun Samfura wanda shine tushen bayanai mai ban mamaki. Zai haskaka muku wasu abubuwan ban sha’awa waɗanda REST API ke bayarwa a halin yanzu.
Bugu da ƙari, kuna iya yin la’akari da gwada fasalin API ta hanyar SwaggerUI (a cikin mai binciken gidan yanar gizo) kuma idan kuna buƙatar zazzagewa da gyara lambar tushe, ana iya samun dama daga GitHub (an buga shi). karkashin lasisin MIT). A ƙarshe, idan kun ci karo da wasu batutuwa yayin amfani da API, da fatan za ku yi shakka a tuntuɓar mu don ƙuduri mai sauri ta hanyar Tallafin tallafin samfur.
Labarai masu alaka
Da fatan za a ziyarci hanyoyin haɗin yanar gizon don ƙarin koyo game da: