Šioje mokymo programoje pateikiama pagrindinė informacija apie tai, kaip konvertuoti EPS į PNG vaizdą naudojant Java. Galite eksportuoti EPS į PNG vaizdą Java naudodami paprastus API iškvietimus bet kurioje operacinėje sistemoje, pvz., Linux, MS Windows ir macOS. Galite sukurti PNG vaizdą labai tiksliai, nes jis gali būti naudingas norint peržiūrėti Encapsulated PostScript (EPS) failus, nereikalaujant jokio konkretaus įrankio ar programos.
Veiksmai, kaip konvertuoti EPS į PNG vaizdą naudojant Java.
- Konfigūruokite savo projektą nurodydami Aspose.Page JAR failą iš Maven saugyklos
- Inicijuoti išvesties vaizdo formatą kaip PNG
- Inicijuoti PsDocument klasės objektą su įvesties EPS failu
- Sukurkite ImageDevice klasės objektą su reikiamais vaizdo matmenimis
- Išsaugokite sugeneruotą PNG vaizdą diske
Norėdami generuoti PNG iš EPS naudojant Java, pirmiausia nurodysime išvesties formatą naudodami ImageFormat sąrašą. Naudodami FileInputStream klasę, įkelsime įvesties EPS failą ir inicijuosime PsDocument klasės objektą. Tada naudosime ImageDevice klasę su reikalingais vaizdo matmenimis ir išsaugosime išvesties PNG vaizdą į diską naudodami Java.
Kodas, skirtas konvertuoti EPS į PNG vaizdą naudojant Java.
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++; | |
} | |
} | |
} |
Ankstesnėje temoje sužinojome kaip sukurti HTML failą naudojant Java. Tuo tarpu ši tema apie Java sukurti PNG iš EPS gali būti naudinga peržiūrint EPS failus arba generuojant miniatiūras turiniui rodyti.