sake girman Hoto

Gyara girman Hoto ta amfani da Java Cloud SDK

Rayuwarmu ta sirri zuwa hukuma ta ƙunshi hotunan raster. Yayin amfani da wayoyin hannu, na’urorin daukar hoto, na’urorin daukar hoto da sauran kyamarori daban-daban da ake amfani da su a cikin na’urori iri-iri, muna samar da adadi mai yawa ta fuskar hotuna. Yanzu ya danganta da matsi algorithm, kowane na’ura yana samar da hotuna cikin girma dabam da girma. Amma idan muna da buƙatu don adana hotuna (JPG, PNG, GIF, TIFF da sauransu) a cikin ma’auni guda ɗaya, don haka muna buƙatar sake girman hotuna. Muna da plethora na aikace-aikacen resizer na hoto da ake samu a kasuwa amma idan muna buƙatar sake girman hoto akan layi a cikin girma, mafita na shirye-shirye koyaushe hanya ce mai dacewa. Don haka, a cikin wannan labarin, za mu tattauna cikakkun bayanai/matakai kan yadda ake haɓaka resizer na hoto ta amfani da Java Cloud SDK.

API ɗin Girman Hoto

Domin canza girman Hoto, za mu yi amfani da Aspose.Imaging Cloud SDK for Java. Saboda tsarin gine-ginen REST ɗin sa, za mu iya samun dama ga API akan kowane dandamali da haɓaka mai canza hoto. Baya ga fasalin don sake girman hoto akan layi, yana kuma ba ku damar ƙirƙira, shiryawa da canza fayilolin hoto zuwa nau’ikan tsararrun fayil masu goyan baya. Yanzu don farawa, muna buƙatar ƙara bayanin sa a cikin aikin java 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>

Idan baku da asusu akan Aspose Cloud Dashboard, da fatan za a ƙirƙiri asusun kyauta ta hanyar adireshin imel mai inganci. 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.

Maimaita Hoto a Java

Wannan sashe yana bayyana matakan yadda ake sake girman hoton PNG. API ɗin yana tsammanin ɗaukar hoton daga motar gida wanda muke buƙatar wuce hoton kai tsaye a jikin buƙatar. Bayan sake girman aikin png, za mu iya adana hoton da aka sabunta akan Ma’ajiyar gajimare ta hanyar tantance ƙimar siga na waje. Koyaya, idan ba mu ƙididdige ƙimar ba, martanin ya ƙunshi hoto mai gudana.

  • Ƙirƙiri misali na ImagingApi dangane da keɓaɓɓen shaidar abokin ciniki
  • Karanta fayil ɗin PNG babban fayil na gida ta amfani da abu Fayil
  • Ƙirƙiri misalin byte[] kuma karanta duk bytes daga fayil ta amfani da hanyar readAllBytes(…).
  • Ƙayyade sabon girman hoto ta amfani da abubuwan Integer
  • Yanzu ƙirƙiri misali na CreateResizedImageRequest inda muka ƙididdige sunan don sakamakon sakamakon
  • A ƙarshe ƙara girman hoto ta amfani da hanyar ƙirƙirarResizedImage(…).
// 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);

// loda fayil daga rumbun gida
File f = new File("PinClipart.png");

// karanta abun ciki na hoton PNG zuwa tsararrun byte
byte[] bytes = Files.readAllBytes(f.toPath());
			
// Sabbin girma don hoton sakamako
int newWidth = 150;
int newHeight = 150;
		    
// ƙirƙiri sake girman buƙatun hoto yayin tantance sunan hoton sakamako
CreateResizedImageRequest resizeRequest = new CreateResizedImageRequest(bytes, newWidth, newHeight, null, "Updated.png", null);

// sake girman hoto akan layi kuma adana fitarwa a cikin ma'ajin gajimare
imageApi.createResizedImage(resizeRequest);

Maimaita GIF ta amfani da Umarnin CURL

Yayin aikin girman hoto, za mu iya kuma ƙididdige tsarin da aka sabunta don hoto. Don haka a cikin wannan sashe, za mu mayar da girman GIF kuma mu adana kayan aiki a tsarin JPG ta amfani da umarnin cURL. Don haka 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"

Da zarar an ƙirƙiro alamar JWT, da fatan za a kira ResizeImage API don sake girman hoto da adana abin fitarwa a tsarin JPG akan faifan gida (sakamakon sigar fayil ɗin zaɓi ne).

curl -v -X GET "https://api.aspose.cloud/v3.0/imaging/winter.gif/resize?newWidth=200&newHeight=200&format=JPG" \
-H  "accept: application/json" \
-H  "authorization: Bearer <JWT Token>" \
-o "resultant.jpg"
sake girman Hoto

Maimaita girman Preview GIF

Hoton GIF na tushen ana iya sauke shi daga winter.gif da kuma sakamakon sakamako daga resultant.jpg.

Kammalawa

Mun koyi hanya mai ban mamaki don sake girman hoto akan layi ta amfani da snippet code na Java. Hakanan ya haskaka mana cikakkun bayanai kan yadda ake sake girman hoto ba tare da rasa inganci ta amfani da umarnin cURL ba. Lura cewa zaku iya bincika iyawar API a cikin mai binciken gidan yanar gizo yayin amfani da swagger API Reference. Yanzu ban da damar Resizer Hoto, zaku iya bincika da koyo game da wasu abubuwan ban sha’awa na API daga Takardun Samfura. Koyaya, duk Cloud SDKs ɗin mu ana buga su ƙarƙashin lasisin MIT, don haka kuna iya yin la’akari da zazzage cikakkiyar lambar tushe daga GitHub (wanda zaku iya canzawa 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: