JAVA reads EMF files and converts them to PNG, JPG, and GIF formats.
Main ideas:
Use EMFInputStream to read the EMF file, and then use the EMFRenderer object to draw the EMF to the creation
Okay, in the blank BufferedImage. In this way, the EMF image is converted into a BufferedImage pair in Java.
Images, and then can be resaved as Java 2D support image formats such as PNG or JPG.
The Code is as follows:
[Java] view plaincopy
Import java. awt. BorderLayout;
Import java. awt. Dimension;
Import java. awt. Graphics;
Import java. awt. Graphics2D;
Import java. awt. image. BufferedImage;
Import java. io. File;
Import java. io. FileInputStream;
Import java. io. FileNotFoundException;
Import java. io. IOException;
Import javax. imageio. ImageIO;
Import javax. swing. JFrame;
Import javax. swing. JPanel;
Import org. freerip. graphicsio. emf. EMFInputStream;
Import org. freerip. graphicsio. emf. EMFRenderer;
/**
* Download the third-party libraries here:
* Http://java.freehep.org/vectorgraphics/index.html
*
* @ Author gloomyfish
*
*/
Public class EMFReader {
Public static void main (String [] args ){
Try {
EMFInputStream inputStream = new EMFInputStream (new FileInputStream ("D: \ export. emf"), EMFInputStream. DEFAULT_VERSION );
System. out. println ("height =" + inputStream. readHeader (). getBounds (). getHeight ());
System. out. println ("widht =" + inputStream. readHeader (). getBounds (). getWidth ());
// HeaderInfo of bitmap API always tell a lie, cocould not get
// Correct width and height
// BitmapInfoHeader headerInfo = new BitmapInfoHeader (inputStream );
// System. out. println ("Big Error on reading emf format picture ");
EMFRenderer emfRenderer = new EMFRenderer (inputStream );
// Create buffered image object from EMF render
Final int width = (int) inputStream. readHeader (). getBounds (). getWidth ();
Final int height = (int) inputStream. readHeader (). getBounds (). getHeight ();
System. out. println ("widht =" + width + "and height =" + height );
Final BufferedImage result = new BufferedImage (width, height, BufferedImage. TYPE_INT_RGB );
Graphics2D g2 = (Graphics2D) result. createGraphics ();
EmfRenderer. paint (g2 );
// Write it as png/jpg/gif, up to you !!!
File outputfile = new File ("D: \ result.png ");
ImageIO. write (result, "png", outputfile );
// Display it www.heatpress123.net
JPanel resultPanel = new JPanel (){
/**
*
*/
Private static final long serialVersionUID = 1L;
Public void paintComponent (Graphics g ){
Super. paintChildren (g );
Graphics2D g2 = (Graphics2D) g;
G2.drawImage (result, 0, 0, width, height, null );
}
};
JFrame ui = new JFrame ("EMF Reader ");
Ui. getContentPane (). setLayout (new BorderLayout ());
Ui. getContentPane (). add (resultPanel, BorderLayout. CENTER );
Ui. setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE );
Ui. setSize (new Dimension (width + 20, height + 40 ));
Ui. setVisible (true );
} Catch (FileNotFoundException e ){
E. printStackTrace ();
} Catch (IOException e ){
E. printStackTrace ();
}
}
}