Moving from one getRGB group to one large getRGB to copy the entire image into an array, the execution time decreased by an order of 33,000 milliseconds to 3200 milliseconds, and the time it created an array was only 31 milliseconds. Del>
There is no doubt that one large read into the array and direct indexing of the array is much faster than many individual reads.
The performance difference is due to the use of the breakpoint operator at the end of the class. While the breakpoint was outside the loop, each line of code inside the class seems to be tested for a breakpoint. Switching to individual techniques DOES NOT improve speed.
Since the code is still correct, the remainder of the answer may still be used.
Old reading instruction
colorRed=new Color(bi.getRGB(x,y)).getRed();
Read instruction to copy bitmap to array
int[] rgbData = bi.getRGB(0,0, bi.getWidth(), bi.getHeight(), null, 0,bi.getWidth());
GetRGB into the array places all 3 color values in one element of the array, so the individual colors must be extracted by rotation and "and". The y coordinate should be multiplied by the width of the image.
Code for reading individual colors from an array
colorRed=(rgbData[(y*bi.getWidth())+x] >> 16) & 0xFF; colorGreen=(rgbData[(y*bi.getWidth())+x] >> 8) & 0xFF; colorBlue=(rgbData[(y*bi.getWidth())+x]) & 0xFF;
Fred f
source share