2 个答案:
答案 0 :(得分:1)
我在网上找到了自己的答案,我将在下面总结一下:
在带有ColorModel的BufferedImages中,像素设置为所选的最接近的颜色。这意味着您可能无法获得所需的颜色,因为您可以设置的颜色仅限于ColorModel中的颜色。
您可以通过创建自己的BufferedImage并将源图像绘制到其上然后操纵这些像素来解决这个问题。
BufferedImage original = ImageIO.read(new File(file.getPath()));
image= new BufferedImage(original.getWidth(), original.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
image.getGraphics().drawImage(original, 0, 0, null);
for(int y = 0; y < original.getHeight(); y++){
for(int x = 0; x < original.getWidth(); x++){
image.setRGB(x,y, original.getRGB(x,y));
}
}
解决了这个问题。很明显,ColorModel没有我指定的颜色,因此将像素调整到最接近的颜色。
答案 1 :(得分:0)
我猜你要找的是WritableRaster,它有助于写入读取的图像。
使用ImageIO将最终更改写入新文件或提供相同的文件进行更改。
public class ImageTest {
BufferedImage image;
File imageFile = new File("C:\\Test\\test.bmp");
public ImageTest() {
try {
image = ImageIO.read(imageFile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void editImage() throws IOException {
WritableRaster wr = image.getRaster();
int width = image.getWidth();
int height = image.getHeight();
for(int ii=0; ii
for(int jj=0; jj
int color = image.getRGB(ii, jj);
wr.setSample(ii, jj, 0 , 156);
}
}
ImageIO.write(image, "BMP", new File("C:\\Test\\test.bmp"));
}
public static void main(String[] args) throws IOException {
ImageTest test = new ImageTest();
test.editImage();
}
}