我有这样的问题:我已经在JAVA程序中准备好了,该程序应该保存图片中的一些数据并将它们保存到txt文件中。然后程序应该将每 25 行中图像的所有像素变为黑色,并在显示器上显示图像(已经带有黑线)。但出了点问题,我不知道是什么 - 整个图片从显示中删除,没有任何显示。这是代码:
private void saveButtonActionPerformed(java.awt.event.ActionEvent evt)
..........
BufferedImage out = new BufferedImage(in.getWidth(), in.getHeight(), in.getType());
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j=j+25) {
out.setRGB(i,j,0);
}
}
ImageIcon img = new ImageIcon(out);
imagePanel.removeAll();
imagePanel.setIcon(img);
} catch (IOException e) {
System.out.print("ERROR");
}
}
}
public static BufferedImage loadImage(File file) {
try {
BufferedImage out = ImageIO.read(file);
return out;
} catch (IOException e) {
return null;
}
}
我正在使用 NetBeans,尽管如此,一切都工作正常。
最佳答案
您创建一个空图像并在每第 25 行开始绘制,实际上您应该从原始图像开始并在其中开始绘制。
// you create an empty image with same width and height of the original
//BufferedImage out = new BufferedImage(in.getWidth(), in.getHeight(), in.getType());
BufferedImage out = ImageIO.read(new File("path/to/Original/image"));
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j=j+25) {
out.setRGB(i,j,0);
}
}
关于java - JAVA中像素改变后的图像实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35458837/