【问题标题】:Java Converting RGB to CMY pictureJava将RGB转换为CMY图片
【发布时间】:2023-12-20 23:13:01
【问题描述】:

我正在尝试将 RGB(色彩空间)图片转换为 CMY(色彩空间)图片。我可以读取特定(RGB)图片,但我的问题实际上是将其写入或保存为 CMY 图片。图片,我要转换的内容如下:

Picture

我写了以下代码:

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 的答案中的链接无法访问,因为“下”页面不再可用。

我发现了一个与此问题相关的非常相似的问题。我在那里发布了我的问题,但它被删除了(原因我不知道)。它的以下问题: Writing java program about RGB-CMY

我希望你能帮助我。

【问题讨论】:

  • 是的,我的问题是写一张CMY图片。这并不意味着我正在用 Java 创建图片。它只意味着我已经有一个图像(独立于它是用 Java 创建的还是它是一个给定的图片,它是之前读过的)。如果你处理了这个问题,你就会知道已经有一张特定的图片。顺便说一句:至少在我看来,外部库并不总是最好的开发方式。不,我有一个特定的格式,它的 bmp,阅读代码。哦,我忘记了,你没有处理这个问题。

标签: java image rgb


【解决方案1】:

这可能是一个解决方案:

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);
    }
}

希望对你有帮助

【讨论】: