ArrayList 不添加 BufferedImage 吗?
ArrayList doesnt add BufferedImage?
我在将 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);
}
}
这是出现问题的代码段。 (前后的代码无所谓,只知道有一个BufferedImage,我循环切出一些子图像(这里我找到了文本行并想将它们添加到listOfLines ArrayList中),然后将原始图像的区域填充为白色,但在将子图像添加到列表之后!)。底部的保存方法只是获取一个BufferedImage并保存到特定路径,仅用于检查目的,我不想稍后保存所有内容,因为那样会太多图像。
问题:如果我现在将图像添加到 listOfLines ArrayList 并立即保存,然后将原始图像中的区域填充为白色,然后在文件夹中查找它,效果很好。但是在显示的代码中,问题是如果我将原始图像添加到arraylist 后直接将其填充为白色,那么arraylist 中的图像是白色的...但是为什么?
保存每个子图像时有效的代码(其中 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);
}
希望我描述的准确:/ 希望你能帮助我或建议另一种方式。我的 B 计划是将每个图像保存在一个文件夹中,然后再次读取 arrayList 中的每个图像并使用那个图像,但我认为这会浪费能量;)
问候
编辑:
好的,整个切割代码是:
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);
}
}
如果有人想自己测试它,它需要一张黑白图像(只需绘制并键入 2/3 行文本)。如果您将最后几行更改为:
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);
它为我保存了正确的图像。但是在上面的代码中,ArrayList 中只有白色图像。
你看过 BufferedImage.getSubImage() 的实际作用了吗?
The returned BufferedImage shares the same data array as the original
image.
如果您真的想稍后保存,create a copy。
我在将 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);
}
}
这是出现问题的代码段。 (前后的代码无所谓,只知道有一个BufferedImage,我循环切出一些子图像(这里我找到了文本行并想将它们添加到listOfLines ArrayList中),然后将原始图像的区域填充为白色,但在将子图像添加到列表之后!)。底部的保存方法只是获取一个BufferedImage并保存到特定路径,仅用于检查目的,我不想稍后保存所有内容,因为那样会太多图像。
问题:如果我现在将图像添加到 listOfLines ArrayList 并立即保存,然后将原始图像中的区域填充为白色,然后在文件夹中查找它,效果很好。但是在显示的代码中,问题是如果我将原始图像添加到arraylist 后直接将其填充为白色,那么arraylist 中的图像是白色的...但是为什么?
保存每个子图像时有效的代码(其中 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);
}
希望我描述的准确:/ 希望你能帮助我或建议另一种方式。我的 B 计划是将每个图像保存在一个文件夹中,然后再次读取 arrayList 中的每个图像并使用那个图像,但我认为这会浪费能量;)
问候
编辑:
好的,整个切割代码是:
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);
}
}
如果有人想自己测试它,它需要一张黑白图像(只需绘制并键入 2/3 行文本)。如果您将最后几行更改为:
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);
它为我保存了正确的图像。但是在上面的代码中,ArrayList 中只有白色图像。
你看过 BufferedImage.getSubImage() 的实际作用了吗?
The returned BufferedImage shares the same data array as the original image.
如果您真的想稍后保存,create a copy。