Ovaj vodič pokriva osnovne informacije o tome kako pretvoriti EPS u PNG sliku pomoću Jave. Možete izvesti EPS u PNG sliku u Javi pomoću jednostavnih API poziva na bilo kojem operativnom sustavu kao što su Linux, MS Windows i macOS. Možete izraditi PNG sliku visoke vjernosti jer može biti od pomoći za pregled Encapsulated PostScript (EPS) datoteka bez potrebe za bilo kojim posebnim alatom ili aplikacijom.
Koraci za pretvaranje EPS u PNG sliku pomoću Jave
- Konfigurirajte svoj projekt navođenjem Aspose.Page JAR datoteke iz Maven repozitorija
- Inicijalizirajte izlazni format slike kao PNG
- Inicijalizirajte objekt klase PsDocument s ulaznom EPS datotekom
- Napravite objekt klase ImageDevice s potrebnim dimenzijama slike
- Spremite generiranu PNG sliku na disk
Kako bismo generirali PNG iz EPS-a pomoću Jave, prvo ćemo navesti izlazni format koristeći ImageFormat enumeraciju. Koristeći klasu FileInputStream, učitati ćemo ulaznu EPS datoteku i inicijalizirati objekt klase PsDocument. Zatim ćemo upotrijebiti klasu ImageDevice s potrebnim dimenzijama slike i spremiti izlaznu PNG sliku na disk pomoću Jave.
Kod za pretvaranje EPS u PNG sliku pomoću Jave
import com.aspose.eps.License; | |
import com.aspose.eps.ImageFormat; | |
import com.aspose.eps.PsDocument; | |
import com.aspose.eps.device.ImageDevice; | |
import com.aspose.eps.device.ImageSaveOptions; | |
import java.awt.Dimension; | |
import java.io.FileInputStream; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
public class ConvertEPStoPNG { | |
public static void main(String[] ePSArgs) throws Exception { // main method for converting EPS to PNG | |
// Set Aspose.Page license before converting EPS to PNG in Java | |
License pageLicense = new License(); | |
pageLicense.setLicense("Aspose.Page.lic"); | |
//Initialize output image format as PNG | |
ImageFormat targetImageFormat = ImageFormat.PNG; | |
// Initialize FileInputStream to load input EPS file | |
FileInputStream psStream = new FileInputStream("input.eps"); | |
// Initialize PsDocument class object with loaded EPS file | |
PsDocument document = new PsDocument(psStream); | |
// Set the flag if you want to convert Postscript file ignoring minor errors | |
boolean suppressErrors = true; | |
// Initialize options object with required parameters. | |
ImageSaveOptions options = new ImageSaveOptions(suppressErrors); | |
// Initialize ImageDevice class object with needed image dimensions | |
ImageDevice ePSImageDevice = new ImageDevice(new Dimension(595, 842), targetImageFormat); | |
try { | |
document.save(ePSImageDevice, options); | |
} finally { | |
psStream.close(); | |
} | |
byte[][] imagesBytes = ePSImageDevice.getImagesBytes(); | |
int i = 0; | |
for (byte [] imageBytes : imagesBytes) { | |
String imagePath = "EPSToImage" + i + "." + imageFormat.toString().toLowerCase(); | |
FileOutputStream fs = new FileOutputStream(imagePath); | |
try { | |
fs.write(imageBytes, 0, imageBytes.length); | |
} catch (IOException ex) { | |
System.out.println(ex.getMessage()); | |
} finally { | |
fs.close(); | |
} | |
i++; | |
} | |
} | |
} |
U prethodnoj temi smo naučili kako stvoriti HTML datoteku pomoću Jave. S druge strane, ova tema o Java stvara PNG iz EPS-a može biti korisna za pregled EPS datoteka ili generiranje minijatura za prikaz sadržaja.