Java实现emf图片字节流转png(jpg)图片字节流

private byte[] emfToPng(InputStream is){
// InputStream inputStream=null;
byte[] by=null;
EMFInputStream emf = null; 
EMFRenderer emfRenderer = null; 
//创建储存图片二进制流的输出流
ByteArrayOutputStream baos = null;
//创建ImageOutputStream流
ImageOutputStream imageOutputStream = null;
try { 
emf = new EMFInputStream(is, EMFInputStream.DEFAULT_VERSION); 
emfRenderer = new EMFRenderer(emf); 

final int width = (int)emf.readHeader().getBounds().getWidth(); 
final int height = (int)emf.readHeader().getBounds().getHeight(); 
final BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); 
Graphics2D g2 = (Graphics2D)result.createGraphics(); 
emfRenderer.paint(g2); 

//创建储存图片二进制流的输出流
baos = new ByteArrayOutputStream();
//创建ImageOutputStream流
imageOutputStream = ImageIO.createImageOutputStream(baos);
//将二进制数据写进ByteArrayOutputStream
ImageIO.write(result, "png", imageOutputStream);
//inputStream = new ByteArrayInputStream(baos.toByteArray());
by=baos.toByteArray();
/*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); 
g2.dispose();
} 
};

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, height)); 
ui.setVisible(true);*/
} catch (FileNotFoundException e) { 
e.printStackTrace(); 
} catch (IOException e) { 
e.printStackTrace(); 
} finally{
try {
if(imageOutputStream!=null){
imageOutputStream.close();
}
if(baos!=null){
baos.close();
}
if(emfRenderer!=null){
emfRenderer.closeFigure();
}
if(emf!=null){
emf.close(); 
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
return by;
}

 

转载于:https://www.cnblogs.com/cleverbo/p/6170577.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值