-4

I have a problem with adding BufferedImages to my ArrayList, well it adds them but, well better I show some code:

        }
            }
        }
        this.listOfLines.add(this.image.getSubimage(xMin, yMin, xMax, yMax - yMin));
        this.image = fillWhite(xMin, yMin, xMax, yMax, this.image);
        isWhite = white(this.image);
    }
    saveLines();
}

private void saveLines() {
    for(int i = 0; i < this.listOfLines.size(); i++){
        save(this.listOfLines.get(i), i);
    }
}

this is the piece of code where the problem occurs. (The code before and after doesn't matter, only thing to know is there is a BufferedImage, and I cut out some subimages in a loop (here I find lines of text and want to add them in the listOfLines ArrayList), and then fill the area of the original image white, but AFTER adding the subimage to the list!). The save method at the bottom is just getting a BufferedImage and save it to a specific path, for checking purposes only, I don't want to save everything later because that would be too many images.

The Problem: If I now add a Image to the listOfLines ArrayList and save it instantly, then fill the area in the original image white, and I look it up in the folder, it works perfectly. BUT in the code shown, the problem is that if I fill the original image white directly after I added it to the arraylist, then the image in the arraylist is white... but why?

The Code that works when saving each subimage (where n is the n-th image saved):

}
        this.listOfLines.add(this.image.getSubimage(xMin, yMin, xMax, yMax - yMin));
        save(this.image.getSubimage(xMin, yMin, xMax, yMax - yMin), n);
        this.image = fillWhite(xMin, yMin, xMax, yMax, this.image);
        isWhite = white(this.image);
    }

Hope I described it accurate :/ Hope you can help me or suggest another way. My Plan B would be to save every image in a folder, and then read every image again in a arrayList and work with that one, but that would be waste of power I think ;)

Greetings

EDIT:

Ok, the whole Code for cutting is that:

    public void cutLines(){
    boolean isWhite = false;

    while(!isWhite){

        int yMin = 0;
        int yMax = 0;
        int xMin = 0;
        int xMax = this.image.getWidth();
        boolean finished = false;
        boolean active = false;
        boolean lineWhite = false;

        for(int i = 0; i < this.image.getHeight(); i++){
            if(!finished){
                if(!active){
                    for(int j = 0; j < this.image.getWidth(); j++){
                        if(this.image.getRGB(j, i) == Color.BLACK.getRGB()){
                            active = true;
                            yMin = i;
                        }
                    }
                } else if(active){
                    lineWhite = true;
                    for(int j = 0; j < this.image.getWidth(); j++){
                        if(this.image.getRGB(j, i) == Color.BLACK.getRGB()){
                            lineWhite = false;
                        }
                    }

                    if(lineWhite){
                        active = false;
                        yMax = i;
                        finished = true;
                    }
                }
            }
        }
        this.listOfLines.add(this.image.getSubimage(xMin, yMin, xMax, yMax - yMin));
        this.image = fillWhite(xMin, yMin, xMax, yMax, this.image);
        isWhite = white(this.image);
    }
    saveLines();
}

private void saveLines() {
    for(int i = 0; i < this.listOfLines.size(); i++){
        save(this.listOfLines.get(i), i);
    }
}

if anyone want to test it on themselves, it need a black and white image (just paint and type 2/3 lines of text). if you change the last lines to:

listOfLines.add(this.image.getSubimage(xMin, yMin, xMax, yMax - yMin));
        save(this.image.getSubimage(xMin, yMin, xMax, yMax - yMin), n);
        n++;
        this.image = fillWhite(xMin, yMin, xMax, yMax, this.image);
        isWhite = white(this.image);

It saves the right Images for me. But in the code above only white images in the ArrayList.

Peter Badida
  • 11,310
  • 10
  • 44
  • 90
Mechamod
  • 21
  • 8
  • `"The code before and after doesnt matter..."` -- maybe not to you, but it does to most of us. You're posting code snippets, nothing that we can run or test, making it hard for us to fully understand your question or your problem. If you are in serious need of a decent answer, please consider improving the code that you've posted by creating and posting a valid [mcve]/[sscce](http://sscce.org). Please check the links before replying. – Hovercraft Full Of Eels Jan 06 '17 at 22:34

1 Answers1

2

Did you look at what BufferedImage.getSubImage() actually does?

The returned BufferedImage shares the same data array as the original image.

If you really want to save at a later point, create a copy.

Community
  • 1
  • 1
KompjoeFriek
  • 3,572
  • 1
  • 22
  • 35
  • thank you for the advice, didnt knew that before! sadly this doesnt work for me aswell, getting many errors no matter what i try – Mechamod Jan 06 '17 at 22:45
  • @Mechamod Sorry, i linked the wrong answer (which fails when used on subimages). Please see http://stackoverflow.com/a/26894825/3931225 – KompjoeFriek Jan 06 '17 at 22:49