如何在ColdFusion(Java)中在CMYK和RGB之间转换图像? [英] How do I convert images between CMYK and RGB in ColdFusion (Java)?

查看:126
本文介绍了如何在ColdFusion(Java)中在CMYK和RGB之间转换图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将图像从CMYK转换为RGB - 不一定再回来,但嘿,如果可以做...

I have a need to convert images from CMYK to RGB - not necessarily back again, but hey, if it can be done...

随着发布ColdFusion 8,我们获得了 CFImage 标记,但它不支持此转换;也不会 Image.cfc Alagad的图像组件

With the release of ColdFusion 8, we got the CFImage tag, but it doesn't support this conversion; and nor does Image.cfc, or Alagad's Image Component.

但是,应该可以在Java中;我们可以通过CF。例如,下面是如何创建一个Java线程来休眠一个进程:

However, it should be possible in Java; which we can leverage through CF. For example, here's how you might create a Java thread to sleep a process:

<cfset jthread = createObject("java", "java.lang.Thread")/>
<cfset jthread.sleep(5000)/>

我猜想类似的方法可以用来利用java做这个图像转换,一个Java开发人员,我没有一个线索从哪里开始。

I would guess a similar method could be used to leverage java to do this image conversion, but not being a Java developer, I don't have a clue where to start. Can anyone lend a hand here?

推荐答案

我使用Java ImageIO库( https://jai-imageio.dev.java.net )。他们不是完美的,但可以很简单,完成工作。至于从CMYK转换为RGB,这是我能够想出的最好的。

I use the Java ImageIO libraries (https://jai-imageio.dev.java.net). They aren't perfect, but can be simple and get the job done. As far as converting from CMYK to RGB, here is the best I have been able to come up with.

下载并安装您的平台的ImageIO JAR和本机库。本地库是必不可少的。没有它们,ImageIO JAR文件将无法检测到CMYK图像。最初,我的印象是,本机库将提高性能,但不是任何功能所必需的。我错了。

Download and install the ImageIO JARs and native libraries for your platform. The native libraries are essential. Without them the ImageIO JAR files will not be able to detect the CMYK images. Originally, I was under the impression that the native libraries would improve performance but was not required for any functionality. I was wrong.

我注意到的另一件事是,转换的RGB图像有时比CMYK图像轻得多。如果有人知道如何解决这个问题,我会很感激。

The only other thing that I noticed is that the converted RGB images are sometimes much lighter than the CMYK images. If anyone knows how to solve that problem, I would be appreciative.

下面是一些代码将CMYK图像转换为任何支持格式的RGB图像。

Below is some code to convert a CMYK image into an RGB image of any supported format.

谢谢

Randy Stegbauer

Thank you,
Randy Stegbauer

package cmyk;

import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.ColorConvertOp;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.apache.commons.lang.StringUtils;

public class Main
{

    /**
     * Creates new RGB images from all the CMYK images passed
     * in on the command line.
     * The new filename generated is, for example "GIF_original_filename.gif".
     *
     */
    public static void main(String[] args)
    {
        for (int ii = 0; ii < args.length; ii++)
        {
            String filename = args[ii];
            boolean cmyk = isCMYK(filename);
            System.out.println(cmyk + ": " + filename);
            if (cmyk)
            {
                try
                {
                    String rgbFile = cmyk2rgb(filename);
                    System.out.println(isCMYK(rgbFile) + ": " + rgbFile);
                }
                catch (IOException e)
                {
                    System.out.println(e.getMessage());
                }
            }
        }
    }

    /**
     * If 'filename' is a CMYK file, then convert the image into RGB,
     * store it into a JPEG file, and return the new filename.
     *
     * @param filename
     */
    private static String cmyk2rgb(String filename) throws IOException
    {
        // Change this format into any ImageIO supported format.
        String format = "gif";
        File imageFile = new File(filename);
        String rgbFilename = filename;
        BufferedImage image = ImageIO.read(imageFile);
        if (image != null)
        {
            int colorSpaceType = image.getColorModel().getColorSpace().getType();
            if (colorSpaceType == ColorSpace.TYPE_CMYK)
            {
                BufferedImage rgbImage =
                    new BufferedImage(
                        image.getWidth(), image.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
                ColorConvertOp op = new ColorConvertOp(null);
                op.filter(image, rgbImage);

                rgbFilename = changeExtension(imageFile.getName(), format);
                rgbFilename = new File(imageFile.getParent(), format + "_" + rgbFilename).getPath();
                ImageIO.write(rgbImage, format, new File(rgbFilename));
            }
        }
        return rgbFilename;
    }

    /**
     * Change the extension of 'filename' to 'newExtension'.
     *
     * @param filename
     * @param newExtension
     * @return filename with new extension
     */
    private static String changeExtension(String filename, String newExtension)
    {
        String result = filename;
        if (filename != null && newExtension != null && newExtension.length() != 0);
        {
            int dot = filename.lastIndexOf('.');
            if (dot != -1)
            {
                result = filename.substring(0, dot) + '.' + newExtension;
            }
        }
        return result;
    }

    private static boolean isCMYK(String filename)
    {
        boolean result = false;
        BufferedImage img = null;
        try
        {
            img = ImageIO.read(new File(filename));
        }
        catch (IOException e)
        {
            System.out.println(e.getMessage() + ": " + filename);
        }
        if (img != null)
        {
            int colorSpaceType = img.getColorModel().getColorSpace().getType();
            result = colorSpaceType == ColorSpace.TYPE_CMYK;
        }

        return result;
    }
}

这篇关于如何在ColdFusion(Java)中在CMYK和RGB之间转换图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆