发布日期:2023-03-31来源:武汉北大青鸟武汉校区作者:027hpit
java技术之读取EMF文件并转化为PNG,JPG,GIF格式,你了解这些技术吗?下面北大青鸟武汉宏鹏光谷校区讲师为你介绍相关java技术。
主要思路:
使用EMFInputStream读取EMF文件,然后使用EMFRenderer对象将EMF绘制到创建好的空白的BufferedImage里面。这样就把EMF图片转化为Java中的BufferedImage对象,然后可以重新保存为Java 2D支持图像格式如PNG或者JPG等。
代码如下:
[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.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 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.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();
}
}
}
北大青鸟武汉宏鹏光谷校区java技术课程会在以后给出更多的案例,请学员们继续关注武汉北大青鸟官方网站。
Copyright (c) 2006-2023 武汉宏鹏教育咨询有限公司 版权所有 All Rights Reserved.