How to store a byte array as an image file on disk?

前端 未结 3 1118
广开言路
广开言路 2021-02-20 09:08

I have a byte array representation of a Image. How to save it on disk as an image file.

I have already done this

OutputStream out = new FileOutputStream(         


        
3条回答
  •  隐瞒了意图╮
    2021-02-20 09:45

    You can use ImageIO API.

    The details can be a bit hairy, but first you'll probably want to create a BufferedImage using TYPE_BYTE_INDEXED type and some suitable IndexColorModel instance. Then put your byte array there. Hint: you can get the internal representation of BufferedImage with:

    myDataBuffer = myBufferedImage.getRaster().getDataBuffer();
    

    Which will likely return a data buffer of type DataBufferByte (check!), from which you get a byte array with

    myByteArray = ((DataBufferByte) myDataBuffer).getData();
    

    Then you can use System.arraycopy to copy your byte array onto that.

提交回复
热议问题