How do I create a BufferedImage from array containing pixels?

后端 未结 3 618
旧时难觅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).

    0 讨论(0)
  • 2021-01-14 19:35

    You can set the RGB (int) values for the pixels in the new image using the setRGB methods.

    0 讨论(0)
  • 2021-01-14 19:36
    BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    

    Then set the pixels again.

    bufferedImage.setRGB(x, y, your_value);
    

    PS: as stated in the comments, please use the answer from @TacticalCoder

    0 讨论(0)
提交回复
热议问题