简体   繁体   English

ArrayList 没有添加 BufferedImage?

[英]ArrayList doesnt add BufferedImage?

I have a problem with adding BufferedImages to my ArrayList, well it adds them but, well better I show some code:我在将 BufferedImages 添加到我的 ArrayList 时遇到了问题,它会添加它们,但是,我最好显示一些代码:

        }
            }
        }
        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!). (前后的代码无关紧要,唯一要知道的是有一个BufferedImage,我在循环中切出一些子图像(这里我找到了几行文本并想将它们添加到listOfLines ArrayList中),然后将原始图像的区域填充为白色,但在将子图像添加到列表之后!)。 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.底部的 save 方法只是获取一个 BufferedImage 并将其保存到特定路径,仅用于检查目的,我不想稍后保存所有内容,因为那会太多图像。

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.问题:如果我现在将图像添加到 listOfLines ArrayList 并立即保存,然后将原始图像的区域填充为白色,然后在文件夹中查找,它可以完美运行。 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?但是在显示的代码中,问题是如果我将原始图像添加到arraylist后直接将其填充为白色,那么arraylist中的图像是白色的......但为什么呢?

The Code that works when saving each subimage (where n is the n-th image saved):保存每个子图像时工作的代码(其中 n 是保存的第 n 个图像):

}
        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 ;)我的 B 计划是将每个图像保存在一个文件夹中,然后再次读取 arrayList 中的每个图像并使用该图像,但我认为这会浪费电力;)

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).如果有人想自己测试它,它需要一个黑白图像(只需绘制并键入 2/3 行文本)。 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.但是在上面的代码中,ArrayList 中只有白色图像。

Did you look at what BufferedImage.getSubImage() actually does?你有没有看BufferedImage.getSubImage()实际上是做什么的?

The returned BufferedImage shares the same data array as the original image.返回的 BufferedImage 与原始图像共享相同的数据数组。

If you really want to save at a later point, create a copy .如果您确实想稍后保存,请创建一个副本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM