关注微信公众号【OpenCV学堂】

-应知空谷幽人意,只在春风淡荡中...

JAVA读取EMF文件并转化为PNG,JPG,GIF格式

JAVA读取EMF文件并转化为PNG,JPG,GIF格式

使用第三方库下载地址为:http://java.freehep.org/vectorgraphics/index.html

主要思路:

使用EMFInputStream读取EMF文件,然后使用EMFRenderer对象将EMF绘制到创建

好的空白的BufferedImage里面。这样就把EMF图片转化为Java中的BufferedImage对

象,然后可以重新保存为Java 2D支持图像格式如PNG或者JPG等。

代码如下:

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.freehep.graphicsio.emf.EMFInputStream;
import org.freehep.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, could 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
		    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.setDefaultCloseOperation(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();
		}
	}

}

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jia20003/article/details/8212524
文章标签: EMF java Java JAVA JPG
个人分类: 杂项技巧
想对作者说点什么? 我来说一句

EMF图片格式转换成PNG

2015年08月04日 573KB 下载

wmf格式自动转换为jpgpng图片

2016年07月13日 3.01MB 下载

将WMF图片格式转换成PNG

2015年08月04日 3.06MB 下载

没有更多推荐了,返回首页

不良信息举报

JAVA读取EMF文件并转化为PNG,JPG,GIF格式

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭