How do I create a BufferedImage from array containing pixels?

后端 未结 3 648
旧时难觅i
旧时难觅i 2021-01-14 18:38

I get the pixels from BufferedImage using the method getRGB(). The pixels are stored in array called data[]. After some manipulation o

3条回答
  •  佛祖请我去吃肉
    2021-01-14 19:18

    I get the pixels from the BufferedImage using the method getRGB(). The pixels are stored in array called data[].

    Note that this can possibly be terribly slow. If your BufferedImage supports it, you may want to instead access the underlying int[] and directly copy/read the pixels from there.

    For example, to fastly copy your data[] into the underlying int[] of a new BufferedImage:

    BufferedImage bi = new BufferedImage( w, h, BufferedImage.TYPE_INT_ARGB );
    final int[] a = ( (DataBufferInt) res.getRaster().getDataBuffer() ).getData();
    System.arraycopy(data, 0, a, 0, data.length);
    

    Of course you want to make sure that your data[] contains pixels in the same representation as your BufferedImage (ARGB in this example).

提交回复
热议问题