• 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;
    }
  • 相关阅读:
    一次线上问题引发的对于C#中相等判断的思考
    Node中的模块引入机制
    Node 各个版本支持ES2015特性的网站
    使用Chrome 中的 ssh 插件登陆 linux 服务器
    vmWare 虚机文件不能启动的事故处理
    JaveScript 中使用 XSLT转换XML文档
    浏览器上的坐标体系相关概念(客户区,页面,屏幕)
    visual Studio 中使用正则表达式来进行查找替换
    JavaScript 执行环境及作用域
    Laravel save部分字段失效的bug问题解决
  • 原文地址:https://www.cnblogs.com/cleverbo/p/6170577.html
Copyright © 2020-2023  润新知