我正在尝试将RGB(色彩空间)图片转换为CMY(色彩空间)图片。我可以读取特定的(RGB)图片,但实际上我的问题是将其写入或保存为CMY图片。图片,我要转换如下:
我写了以下代码:
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.awt.image.ColorConvertOp;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Aufgabe2c {
public static void main(String[] args) {
try {
BufferedImage image = ImageIO.read(new File("blumen.bmp"));
iterateThroughImage(image);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void iterateThroughImage(BufferedImage image) {
int w = image.getWidth();
int h = image.getHeight();
System.out.println("width, height: " + w + ", " + h);
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
System.out.println("x,y: " + j + ", " + i);
int pixel = image.getRGB(j, i);
getPixelARGB(pixel);
System.out.println("");
}
}
}
public static void getPixelARGB(int pixel) {
int alpha = (pixel >> 24) & 0xff;
int red = (pixel >> 16) & 0xff;
int green = (pixel >> 8) & 0xff;
int blue = (pixel) & 0xff;
System.out.println("argb: " + alpha + ", " + red + ", " + green + ", " + blue);
}
public static void convertRGBToCMY(int red, int green, int blue) {
int cyan = 255 - red;
int magenta = 255 - green;
int yellow = 255 - blue;
System.out.println("argb: "+ red + ", " + green + ", " + blue);
}
}
我在方法iterateThroughImage(BufferedImage image)
中将rgb值作为单个int值获取,并将其存储在int变量pixel
中。我在方法getPixelARGB(int pixel)
中获得了每个Red,Green,Blue和Alpha值。
我的问题实际上是我不知道如何将给定的特定RGB图片转换为CMY(色彩空间)图片。
顺便说一句:Pazhamalai G答案中的链接不可用,因为“ under”页面ist不再可用。
我发现了一个与此问题相关的非常相似的问题。我在那儿张贴了问题,但已被删除(由于我不知道的原因)。其以下问题: Writing java program about RGB-CMY
我希望你能帮助我。
答案 0 :(得分:0)
这可能是一个解决方案:
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Aufgabe2c {
public static void main(String[] args) {
try {
BufferedImage image = ImageIO.read(new File("blumen.bmp"));
iterateThroughImageToGetPixel(image);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void iterateThroughImageToGetPixel(BufferedImage image) {
try {
BufferedImage cmyImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
System.out.println("width, height: " + image.getWidth() + ", " + image.getHeight());
for (int column = 0; column < image.getHeight(); column++) {
for (int row = 0; row < image.getWidth(); row++) {
System.out.println("x,y: " + row + ", " + column);
int pixel = image.getRGB(row, column);
// getPixelCMYValuesFromARGBValuesPerPixel(pixel).getRGB();
cmyImage.setRGB(row, column, getPixelCMYValuesFromARGBValuesPerPixel(image.getRGB(row, column)).getRGB());
System.out.println("");
System.out.println("----------------------------------------------------------------------------");
}
}
System.out.println("#####################################################");
ImageIO.write(cmyImage, "bmp", new File("blumen_cmy.bmp") );
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // Save as BMP
}
/*
* Quelle: https://alvinalexander.com/blog/post/java/getting-rgb-values-for-each-pixel-in-image-using-java-bufferedi
* http://openbook.rheinwerk-verlag.de/javainsel9/javainsel_20_006.htm#mj4c12381d5bacf8fb6ee31448d26890bb
*/
public static Color getPixelCMYValuesFromARGBValuesPerPixel(int pixel) {
int alpha = (pixel >> 24) & 0xff;
int red = (pixel >> 16) & 0xff;
int green = (pixel >> 8) & 0xff;
int blue = (pixel) & 0xff;
System.out.println("argb: " + alpha + ", " + red + ", " + green + ", " + blue);
return convertRGBToCMY(red, green, blue);
}
public static Color convertRGBToCMY(int red, int green, int blue) {
int[] cmyArray = new int[3];
//cyan
int cyan = 255 - red;
//magenta
int magenta = 255 - green;
//yellow
int yellow = 255 - blue;
return new Color(cyan, magenta, yellow);
}
}
希望是有帮助的