I’m looking for the fastest way to get pixel data (int the form int[][]) from a BufferedImage. My goal is to be able to address pixel (x, y) from the image using int[x][y]. All the methods I have found do not do this (most of them return int[]s).
I’m looking for the fastest way to get pixel data (int the form int[][]
Share
I was just playing around with this same subject, which is the fastest way to access the pixels. I currently know of two ways for doing this:
getRGB()method as described in @tskuzzy’s answer.By accessing the pixels array directly using:
If you are working with large images and performance is an issue, the first method is absolutely not the way to go. The
getRGB()method combines the alpha, red, green and blue values into one int and then returns the result, which in most cases you’ll do the reverse to get these values back.The second method will return the red, green and blue values directly for each pixel, and if there is an alpha channel it will add the alpha value. Using this method is harder in terms of calculating indices, but is much faster than the first approach.
In my application I was able to reduce the time of processing the pixels by more than 90% by just switching from the first approach to the second!
Here is a comparison I’ve setup to compare the two approaches:
Can you guess the output? 😉