如何在ColdFusion(Java)中的CMYK和RGB之间转换图像?
我需要将图像从CMYK转换为RGB不一定要再次返回,但是,嘿,如果可以的话...
随着ColdFusion 8的发布,我们获得了CFImage标签,但是它不支持这种转换。 Image.cfc或者Alagad的图像组件也没有。
但是,在Java中应该可以实现。我们可以通过CF加以利用。例如,以下是创建Java线程以使进程休眠的方法:
<cfset jthread = createObject("java", "java.lang.Thread")/> <cfset jthread.sleep(5000)/>
我猜想可以使用类似的方法来利用Java来完成此图像转换,但是由于不是Java开发人员,所以我不知道从哪里开始。有人可以在这里伸出援手吗?
解决方案
回答
从CMYK转换为RGB而忽略所有颜色配置文件的一个非常简单的公式是:
R = ( (255-C)*(255-K) ) / 255; G = ( (255-M)*(255-K) ) / 255; B = ( (255-Y)*(255-K) ) / 255;
此代码要求CMYK值的范围为0-255. 如果我们是0到100或者0.0到1.0,则必须转换这些值。
希望这会入门。
对于Java和ColdFusion接口,很抱歉,但是我不知道该怎么做。
回答
标签cfx_image可能对我们有用。我已经有一段时间没有使用它了,但我记得它有很多功能。
或者,我们可能可以编写Windows应用程序(例如Irfanview)(通过使用cfexecute的命令行)来处理图像。
希望能有所帮助
回答
我使用Java ImageIO库(https://jai-imageio.dev.java.net)。它们不是完美的,但可以很简单,就可以完成工作。至于从CMYK转换为RGB,这是我能想到的最好的方法。
下载并安装适用于我们平台的ImageIO JAR和本机库。本地库是必不可少的。没有它们,ImageIO JAR文件将无法检测CMYK图像。最初,我给人的印象是本机库可以提高性能,但是任何功能都不是必需的。我错了。
我注意到的唯一另一件事是,转换后的RGB图像有时比CMYK图像要亮得多。如果有人知道如何解决该问题,我将不胜感激。
以下是一些代码,可以将CMYK图像转换为任何受支持格式的RGB图像。
谢谢,
兰迪·斯蒂格鲍尔(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; } }