Hoton JPG sanannen tsari ne don bugu da dalilai na gyarawa. Bugu da ƙari, sanannen tsarin hoto don raba hotuna da sauran hotuna akan intanit da tsakanin masu amfani da Wayar hannu da PC. Ƙananan girman fayil ɗin hotunan JPG yana ba da damar adana dubban hotuna a cikin ƙaramin sarari na ƙwaƙwalwar ajiya. Yanzu da matsi ya yi hasara, yana nufin cewa an goge wasu bayanan da ba dole ba har abada. A gefe guda, babban fa’idar PNG akan JPEG shine matsawar ba ta da asara, ma’ana babu asara cikin inganci duk lokacin da aka buɗe kuma a sake ajiyewa. PNG kuma yana sarrafa cikakkun bayanai, hotuna masu girma da kyau. Yanzu a cikin wannan labarin, za mu tattauna cikakkun bayanai kan yadda ake canza JPG zuwa PNG ta amfani da Java Cloud SDK.
API ɗin Canjin Hoto
API ɗin mu na REST yana ba da damar ƙirƙira, shiryawa da canza fayilolin hoto zuwa nau’ikan tsararrun fayil masu goyan baya. Yanzu don aiwatar da fasalin don lodawa da canza jpg zuwa png a bayyane a cikin aikace-aikacen Java, muna buƙatar amfani da Aspose.Imaging Cloud SDK don Java azaman abin rufewa a kusa da Cloud API. Don haka mataki na gaba shine ƙara bayaninsa a cikin aikin java ta haɗa da bin bayanai a cikin pom.xml na maven build type project.
<repositories>
<repository>
<id>aspose-cloud</id>
<name>artifact.aspose-cloud-releases</name>
<url>http://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 JDK a cikin aikin, muhimmin mataki na gaba shine ƙirƙirar asusun kyauta akan Aspose Cloud Dashboard. Kawai rajista don Gwaji Kyauta ta hanyar ingantaccen adireshin imel. Yanzu shiga ta amfani da sabon asusun da aka ƙirƙira kuma bincika/ƙirƙiri ID na abokin ciniki da Sirrin Abokin ciniki a Cloud Dashboard. Ana buƙatar waɗannan cikakkun bayanai don dalilai na tantancewa a cikin sassan masu zuwa.
Canza JPG zuwa PNG a Java
Yanzu bari mu tattauna cikakkun bayanai kan yadda za mu iya canza JPG zuwa PNG ta amfani da snippet code na Java. Domin yin jujjuyawar, muna da API guda biyu don canza hoto zuwa wani tsari:
GET API yana sa ran mu fara loda hoto zuwa Cloud Storage sannan mu wuce sunansa a cikin API URL. Bayan an sabunta sigogin hoton, API ɗin yana mayar da hoton da aka sabunta a cikin martani. Idan kuna son adana hoton da aka sabunta akan Ma’ajiyar gajimare, kuna buƙatar yin hakan a sarari kamar yadda aka nuna a misalan ƙasa.
A gefe guda, lokacin kiran API na POST na biyu, zaku iya wuce hoton kai tsaye a jikin buƙatar. Hakanan yana ba ku damar adana hoton da aka sabunta akan Ma’ajiyar gajimare ta hanyar tantance ƙimar siga na waje. Koyaya, idan baku fayyace ƙimar ba, martanin ya ƙunshi hoto mai yawo.
- Ƙirƙiri misali na ImagingApi dangane da keɓaɓɓen shaidar abokin ciniki
- Karanta duk fayilolin JPG suna yin babban fayil na gida ta amfani da abun Fayil[].
- Maimaita ta hanyar fayiloli a tsararru kuma isa ga abun ciki na hoto ɗaya cikin misalin byte[].
- Yanzu ƙirƙiri wani abu na CreateConvertedImageRequest inda muka ƙididdige tsarin fitarwa azaman PNG
- Kira hanyar ƙirƙirarConvertedImage(…) wanda ke adana JPG azaman PNG kuma yana dawo da sakamako azaman rafin amsawa
- A ƙarshe ajiye fayil ɗin PNG zuwa faifan gida ta amfani da abu FileOutputStream
// Samu ClientID da ClientSecret daga https://dashboard.aspose.cloud/
String clientId = "7ef10407-c1b7-43bd-9603-5ea9c6db83cd";
String clientSecret = "ba7cc4dc0c0478d7b508dd8ffa029845";
// haifar da Hoto abu
ImagingApi imageApi = new ImagingApi(clientSecret, clientId);
File directory = new File("/Users/");
//Samo duk fayiloli daga babban fayil
File[] allFiles = directory.listFiles();
if (allFiles == null || allFiles.length == 0) {
throw new RuntimeException("No files present in the directory: " + directory.getAbsolutePath());
}
//Saita karin hoton da ake buƙata anan.
List<String> supportedImageExtensions = Arrays.asList("jpg","jpeg");
int counter =0;
//Tace jerin fayilolin hoton JPG
List<File> acceptedImages = new ArrayList<>();
// Yi maimaita kowane fayil ɗin hoto da aka karanta daga babban fayil na gida
for (File file : allFiles) {
//Fassara tsawo na fayil
String fileExtension = file.getName().substring(file.getName().lastIndexOf(".") + 1);
//Bincika idan an jera tsawo a cikin Hotunan da ake tallafawa
if (supportedImageExtensions.stream().anyMatch(fileExtension::equalsIgnoreCase)) {
//Ƙara hoton zuwa jerin abubuwan da aka tace
acceptedImages.add(file);
// karanta JPG abun ciki na hoto
byte[] bytes = Files.readAllBytes(acceptedImages.get(counter).toPath());
// ƙirƙirar buƙatun canza hoto tare da tsarin sakamako azaman PNG
CreateConvertedImageRequest request = new CreateConvertedImageRequest(bytes, "PNG", null, null);
// canza JPG zuwa PNG kuma dawo da fayil ɗin sakamako a cikin rafin amsawa
byte[] exportedImage = imageApi.createConvertedImage(request);
// Ajiye hoton da aka fitar zuwa ma'ajiyar gida
FileOutputStream fos = new FileOutputStream(directory + "/"+file.getName().replaceFirst("[.][^.]+$", "")+".png");
fos.write(exportedImage);
fos.close();
}
}
Canza JPG zuwa PNG ta amfani da Umarnin CURL
Kamar yadda za mu iya samun dama ga REST APIs ta amfani da umarnin cURL, don haka a cikin wannan sashe, muna bincika cikakkun bayanai don canza JPG zuwa PNG m. Yanzu a matsayin buƙatun farko, muna buƙatar fara samar da alamar samun damar JWT (bisa ga shaidar abokin ciniki) yayin aiwatar 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"
Yanzu za mu yi amfani da kiran API na ConvertImage, yana tsammanin shigar da JPG ya riga ya kasance a cikin ajiyar girgije. Yanzu da fatan za a aiwatar da umarni mai zuwa don canza JPG zuwa PNG.
curl -v -X GET "https://api.aspose.cloud/v3.0/imaging/image1.jpg/convert?format=PNG" \
-H "accept: application/json" \
-H "authorization: Bearer <JWT Token>" \
-o Converted.png
Kammalawa
Wannan labarin ya ba da cikakkun bayanai masu ban mamaki kan yadda za mu iya canza JPG zuwa PNG ta amfani da snippet code na Java. Hakazalika, mun kuma tattauna matakan canza JPG zuwa PNG ta hanyar amfani da Umarnin CURL. Lura cewa Takardun Samfura wuri ne mai ban mamaki don koyo game da wasu abubuwan ban sha’awa da API ke bayarwa. Idan kuna buƙatar gwada waɗannan APIs a cikin mai bincike, da fatan za a gwada amfani da swagger API Reference.
Da kyau, da fatan za a lura cewa duk Cloud SDKs an buga su ƙarƙashin lasisin MIT, don haka kuna iya yin la’akari da zazzage cikakkiyar lambar tushe daga GitHub kuma gyara ta gwargwadon buƙatun ku. A ƙarshe, idan kun haɗa da wasu batutuwa yayin amfani da API, kuna iya yin la’akari da kusantar mu don ƙuduri mai sauri ta hanyar [zarun tallafin samfur] kyauta 9.
Labarai masu alaka
Da fatan za a ziyarci hanyoyin haɗin yanar gizon don ƙarin koyo game da: