java cmyk和rgb的转换_如何在ColdFusion(Java)中在CMYK和RGB之间转换图像?

小编典典

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

下载并安装适用于您平台的ImageIO JAR和本机库。本地库是必不可少的。没有它们,ImageIO

JAR文件将无法检测CMYK图像。最初,我给人的印象是本机库可以提高性能,但是任何功能都不是必需的。我错了。

我注意到的唯一另一件事是,转换后的RGB图像有时比CMYK图像要轻得多。如果有人知道如何解决该问题,我将不胜感激。

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

谢谢你,

兰迪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;

}

}

2020-10-09

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值