我想看看我的照片:
我想通过按下按钮来过滤图像
目前,我有一个BufferedImage
,而且我不知道如何设置像素。
这将我的照片转换为蓝色照片,但我不知道为什么以及如何设置绿色和红色?
int width = img.getWidth();
int height = img.getHeight();
for(int y = 0; y < height; y++){
for(int x = 0; x < width; x++){
int p = img.getRGB(x,y);
int r = (p>>16)&0xff;
int g = (p>>8)&0xff;
int b = p & 0xff;
img.setRGB(x,y,r);
}
}
this.lblFilteredImage.setIcon(new ImageIcon(img));
答案 0 :(得分:1)
您可以通过B
类轻松分隔红色,绿色和蓝色值,该类将java.awt.Color
的颜色值作为构造函数参数:
img.getRGB(x,y)
现在,您可以通过另一个Color color = new Color(img.getRGB(x,y));
int red = color.getRed();
int green = color.getGreen();
int blue = color.getBlue();
对象将它们转换回img.setRGB()
所需的值,该对象也可以在构造函数java.awt.Color
中获取红色,绿色和蓝色值,并具有方法Color(red, green, blue)
以获取完整的颜色代码:
.getRGB()
现在要获得完全红色的图像,您只需要编写int onlyRed = new Color(red, 0, 0).getRGB();
int onlyGreen = new Color(0, green, 0).getRGB();
int onlyBlue = new Color(0, 0, blue).getRGB();
。
答案 1 :(得分:1)
获取单独彩色图像的一种方法是将其他颜色清零。
此GUI模型类通过更改掩码位从原始图像生成红色,绿色和蓝色图像。
package com.ggl.rgbdisplay.model;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
public class RGBDisplayModel {
private BufferedImage originalImage;
private BufferedImage redImage;
private BufferedImage greenImage;
private BufferedImage blueImage;
public BufferedImage getOriginalImage() {
return originalImage;
}
public void setOriginalImage(BufferedImage originalImage) {
this.originalImage = originalImage;
this.redImage = createColorImage(originalImage, 0xFFFF0000);
this.greenImage = createColorImage(originalImage, 0xFF00FF00);
this.blueImage = createColorImage(originalImage, 0xFF0000FF);
}
public BufferedImage getRedImage() {
return redImage;
}
public BufferedImage getGreenImage() {
return greenImage;
}
public BufferedImage getBlueImage() {
return blueImage;
}
public static BufferedImage createTestImage() {
BufferedImage bufferedImage = new BufferedImage(200, 200,
BufferedImage.TYPE_INT_ARGB);
Graphics g = bufferedImage.getGraphics();
for (int y = 0; y < bufferedImage.getHeight(); y += 20) {
if (y % 40 == 0) {
g.setColor(Color.WHITE);
} else {
g.setColor(Color.BLACK);
}
g.fillRect(0, y, bufferedImage.getWidth(), 20);
}
g.dispose();
return bufferedImage;
}
private BufferedImage createColorImage(BufferedImage originalImage, int mask) {
BufferedImage colorImage = new BufferedImage(originalImage.getWidth(),
originalImage.getHeight(), originalImage.getType());
for (int x = 0; x < originalImage.getWidth(); x++) {
for (int y = 0; y < originalImage.getHeight(); y++) {
int pixel = originalImage.getRGB(x, y) & mask;
colorImage.setRGB(x, y, pixel);
}
}
return colorImage;
}
}