CMYK图片转成rgb模式图片

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Chenzh_758/article/details/84689618
package com.makr.common.framework.common.util.img;

import com.sun.image.codec.jpeg.ImageFormatException;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageDecoder;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

import java.awt.Transparency;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.ComponentColorModel;
import java.awt.image.DataBuffer;
import java.awt.image.DataBufferByte;
import java.awt.image.Raster;
import java.awt.image.WritableRaster;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;

import javax.imageio.IIOException;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
*
* @ClassName: CmykToRgb
* @Description: cmyk模式图转换为RGB模式图
* @copyright
* @author 陈智航
* @date 2014年7月16日 上午10:51:22
*/
public class CmykToRgb {
public static Log logger = LogFactory.getLog(CmykToRgb.class);

public void readImage(String filename) throws IOException {
File file = new File(filename);
ImageInputStream input = ImageIO.createImageInputStream(file);
Iterator<ImageReader> readers = ImageIO.getImageReaders(input);
if (readers == null || !readers.hasNext()) {
throw new RuntimeException("1 No ImageReaders found");
}
ImageReader reader = (ImageReader) readers.next();
reader.setInput(input);
String format = reader.getFormatName();
BufferedImage image;

if ("JPEG".equalsIgnoreCase(format) || "JPG".equalsIgnoreCase(format)) {
try {
// 尝试读取图片 (包括颜色的转换).
image = reader.read(0); // RGB
} catch (IIOException e) {
// 读取Raster (没有颜色的转换).
Raster raster = reader.readRaster(0, null);// CMYK
image = createJPEG4(raster);
}
image.getGraphics().drawImage(image, 0, 0, null);
String newfilename = filename.substring(0,
filename.lastIndexOf("."))
+ "_rgb" + filename.substring(filename.lastIndexOf("."));
File newFile = new File(newfilename);
FileOutputStream out = new FileOutputStream(newFile);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);
out.flush();
out.close();

}
}

public static BufferedImage cmyk2rgb(File file) throws IOException {
ImageInputStream input = ImageIO.createImageInputStream(file);
Iterator<ImageReader> readers = ImageIO.getImageReaders(input);
if (readers == null || !readers.hasNext()) {
throw new RuntimeException("1 No ImageReaders found");
}
ImageReader reader = (ImageReader) readers.next();
reader.setInput(input);
String format = reader.getFormatName();
BufferedImage image = null;

if ("JPEG".equalsIgnoreCase(format) || "JPG".equalsIgnoreCase(format)) {
try {
// 尝试读取图片 (包括颜色的转换).
image = reader.read(0); // RGB

} catch (IIOException e) {
// 读取Raster (没有颜色的转换).
Raster raster = reader.readRaster(0, null);// CMYK
image = createJPEG4(raster);
}
}
return image;
}

private static BufferedImage createJPEG4(Raster raster) {
int w = raster.getWidth();
int h = raster.getHeight();
byte[] rgb = new byte[w * h * 3];
// 彩色空间转换
float[] Y = raster.getSamples(0, 0, w, h, 0, (float[]) null);
float[] Cb = raster.getSamples(0, 0, w, h, 1, (float[]) null);
float[] Cr = raster.getSamples(0, 0, w, h, 2, (float[]) null);
float[] K = raster.getSamples(0, 0, w, h, 3, (float[]) null);
for (int i = 0, imax = Y.length, base = 0; i < imax; i++, base += 3) {
float k = 220 - K[i], y = 255 - Y[i], cb = 255 - Cb[i], cr = 255 - Cr[i];

double val = y + 1.402 * (cr - 128) - k;
val = (val - 128) * .65f + 128;
rgb[base] = val < 0.0 ? (byte) 0 : val > 255.0 ? (byte) 0xff
: (byte) (val + 0.5);

val = y - 0.34414 * (cb - 128) - 0.71414 * (cr - 128) - k;
val = (val - 128) * .65f + 128;
rgb[base + 1] = val < 0.0 ? (byte) 0 : val > 255.0 ? (byte) 0xff
: (byte) (val + 0.5);

val = y + 1.772 * (cb - 128) - k;
val = (val - 128) * .65f + 128;
rgb[base + 2] = val < 0.0 ? (byte) 0 : val > 255.0 ? (byte) 0xff
: (byte) (val + 0.5);
}
raster = Raster.createInterleavedRaster(new DataBufferByte(rgb,
rgb.length), w, h, w * 3, 3, new int[] { 0, 1, 2 }, null);
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
ColorModel cm = new ComponentColorModel(cs, false, true,
Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
return new BufferedImage(cm, (WritableRaster) raster, true, null);
}

public static boolean isCmyk(File file) {
boolean result = false;
BufferedImage img = null;
try {
img = ImageIO.read(file);
} catch (IOException e) {
try {
JPEGImageDecoder decoder = JPEGCodec
.createJPEGDecoder(new FileInputStream(file));
img = decoder.decodeAsBufferedImage();
result = true;
} catch (ImageFormatException ef) {
logger.error("该图片不是JPG格式", ef);
} catch (IOException ex) {
logger.error("I0异常", ex);
}
}
return result;
}

public static void main(String[] args) throws IOException {
String filename = "E://0.jpg";
File file = new File(filename);
System.out.println(isCmyk(file));

}
}
展开阅读全文

cmyk模式图片转成rgb

06-26

在网上找了一天资料,都没有看到一个完整好用的方法,都是些零星的片段。以下是我综合网上找到的几种说法写的代码,icc文件是下载的AdobeICCProfiles里面的。能转,但是转出来图片的白色都变成绿色了,感觉答案就在眼前,就差那么一点点。哪位有这方面的经验,请指教一二,不胜感激。rnrn try rn ColorSpace cs = new ICC_ColorSpace(ICC_Profile.getInstance("JapanColor2001Coated.icc"));rn File file = new File("D:\\cmyk.jpg");rn FileInputStream input = new FileInputStream(file);rn JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(input);rn Raster srcRaster = decoder.decodeAsRaster();rnrn BufferedImage result = new BufferedImage(srcRaster.getWidth(), srcRaster.getHeight(), BufferedImage.TYPE_INT_RGB);rn WritableRaster resultRaster = result.getRaster();rnrn ColorConvertOp cmykToRgb = new ColorConvertOp(cs, result.getColorModel().getColorSpace(), null);rn cmykToRgb.filter(srcRaster, resultRaster);rn rn for (int x = srcRaster.getMinX(); x < srcRaster.getWidth(); ++x)rn for (int y = srcRaster.getMinY(); y < srcRaster.getHeight(); ++y) rnrn float[] p = srcRaster.getPixel(x, y, (float[])null);rnrn for (int i = 0; i < p.length; ++i)rn p[i] = 1 - p[i] / 255f;rn rnrn p = cs.toRGB(p);rnrn for (int i = 0; i < p.length; ++i)rnrn p[i] = p[i] * 255f;rn rn resultRaster.setPixel(x, y, p);rn rn BufferedImage imageb = new BufferedImage(result.getColorModel(), resultRaster, true, null);rn FileOutputStream out = new FileOutputStream("D://rgb.jpg");rn JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);rn encoder.encode(imageb);rn out.close();rn catch (IOException e) rn // TODO Auto-generated catch blockrn e.printStackTrace();rn 论坛

没有更多推荐了,返回首页