Home » excel » java – Fill an ArrayList with the RGB value of every pixel of an Image

java – Fill an ArrayList with the RGB value of every pixel of an Image

Posted by: admin May 14, 2020 Leave a comment

Questions:

Hello I wanted to create a programm which reads in an image and after outputs an excel with the image like this —> http://www.boydevlin.co.uk/images/screenshots/eascreen04.png

To realize this i think i have to read the rgb value from every pixel in the image to an ArrayList
I want to save it in the following order

Example 5x5px Image

01,02,03,04,05
06,07,08,09,10  
11,12,13,14,15
.......

I allready have this but it’s not working out correctly Could someone helpe me with the algorrithm

    public class Engine {

    private int x = 0;
    private int y = 0;
    private int count = 50;
    private boolean isFinished = false; 
    ArrayList<Color> arr = new ArrayList<Color>();

    public void process(){
        BufferedImage img = null;
        try {
            img = ImageIO.read(new File("res/images.jpg"));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("img file not found");
        }

        while(isFinished = false){
        int rgb = img.getRGB(x, y);
        Color c = new Color(rgb);
        arr.add(c);
        System.out.println("y:"+ y);
        x++;}
        if(x == 49){
            y++;
            x = 0;
            }else if(x == 49 && y == 49){
                isFinished = true;
            }
        }

};
How to&Answers:

You need to know that if image would become big, ArrayList would be really big, better use plain array( you know.. []), and make it two dimmensional.
Even better if you could create excel in place and do not save all the data in array, just set proper values in place where you write the data out to console.
I haven’t tested the code but should be ok.
If you get any Exception post its content so that we could help.

try something like that:

public class Engine {

    private int x = 0;
    private int y = 0;
    ArrayList<Color> arr = new ArrayList<Color>();

    public void process() {
        BufferedImage img = null;
        try {
            img = ImageIO.read(new File("res/images.jpg"));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("img file not found");
        }

        for(int x=0;x<img.getWidth();x++){
            for(int y=0;y<img.getHeight();y++){
                int rgb = img.getRGB(x, y);
                Color c = new Color(rgb);
                arr.add(c);
                System.out.println("x: "+ x + " y:" + y +" color: " + c);
            }
        }
    }
};

Answer:

First : you have an error in while loop

convert it from :

while (isFinished=false)

to

while (isFinished==false) 

second : use for loop instead of while loop

for (int x = 0; x < img.getWidth(); x++) {
            for (int y = 0; y < img.getHeight(); y++) {
                int rgb = img.getRGB(x, y);
                Color c = new Color(rgb);
                arr.add(c);
            }

        }

and if you want it by using while loop , Try this:

while (isFinished == false) {
            int rgb = img.getRGB(x, y);
            Color c = new Color(rgb);
            arr.add(c);
            x++;
            if (x == img.getWidth()) {
                y++;
                x = 0;
            } else if (x == img.getWidth() - 1 && y == img.getHeight() - 1) {
                isFinished = true;
            }
        }