我正在嘗試更改圖像的顏色。因此,我使用以下代碼更改java中的圖像顏色
public class Picture {
String img_name;
BufferedImage buf_img;
int width;
int height;
public Picture(String name) {
this.img_name = name;
try {
buf_img = ImageIO.read(new File(img_name));
} catch (IOException ex) {
Logger.getLogger(Picture.class.getName()).log(Level.SEVERE, null, ex);
}
}
public Picture(int w, int h) {
this.width = w;
this.height = h;
buf_img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
}
public int width() {
width = buf_img.getWidth();
return width;
}
public int height() {
height = buf_img.getHeight();
return height;
}
public Color get(int col, int row) {
Color color = new Color(buf_img.getRGB(col, row));
return color;
}
public void set(int col, int row, Color color) {
buf_img.setRGB(col, row, color.getRGB());
}
public void show() {
try {
File saveAs = new File("D:\\temp\\" + new Random().nextInt() + ".png");
ImageIO.write(buf_img, "png", saveAs);
Desktop.getDesktop().open(saveAs);
} catch (IOException ex) {
Logger.getLogger(Picture.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public class ColorSeparation {
public static void main(String[] args) {
// read in the picture specified by command-line argument
Picture picture = new Picture("D:\\qwe1.jpg");
int width = picture.width();
int height = picture.height();
// create three empy pictures of the same dimension
Picture pictureR = new Picture(width, height);
// separate colors
for (int col = 0; col < width; col++) {
for (int row = 0; row < height; row++) {
Color color = picture.get(col, row);
int r = color.getRed();
int g = color.getGreen();
int b = color.getBlue();
pictureR.set(col, row, new Color(r, 0, 0));
}
}
// display picture in its own window
pictureR.show();
}
}
現在我想設置整個圖像的顏色爲rgb 255,153,51。我試圖設置 pictureR.set(col, row, new Color(255, 153, 51))
。但結果輸出如下圖。
我怎樣才能得到正確的圖像?請幫忙。
這看起來很像,你必須「設置整個圖像的顏色,255,153,51 RGB」我的圖像。 –
發佈'圖片'類。更好的是,發佈一個[mcve] – Reimeus
@Reimeus我現在已經更新了這個問題 –