将CMYK彩色空间的图片转换为RGB彩色空间的图片

  最近给网站增加一个会员头像的功能,这就必然的会有用户上传图片然后压缩啊,剪裁啊 什么什么的。

   做出来了,自己独自测试,通过了。然后交由老大验收,结果他在测试的时候,上传了一张比较大的jpg格式的图片,然后程序运行不通过了。我原本以为是图片太大导致,自己又重新测试,用了一张更大的图,通过了。这是为毛呢。然后看了看报错,Unsupport Image Type .度娘&谷哥后,原来是CMYK这么个东西,就是我自己测试的时候用的全是RGB的图片,老大测试的时候用的那张是CMYK的,然后就出现了这结果。 于是,继续度娘&谷哥,找到了一些将CMYK的图片转为RGB的源代码,COPY测试不通过。经过对几种方法的测试和"研究".ND 终于将这个问题搞定了。下面给出代码:

 

 

package com.test.common;

import com.sun.image.codec.jpeg.JPEGCodec;
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.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;


/**
 *
 * @author Dsmart
 */
public class CMYK {
  public void readImage(String filename) throws IOException {        
       File file = new File(filename);
        ImageInputStream input = ImageIO.createImageInputStream(file);
        Iterator 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();  
            
        }
        
       
       
    }

 

    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 void main(String[] args) throws IOException 
    { 
          String filename = "h:/img/test.jpg";
          
          CMYK cm = new CMYK();
          cm.readImage(filename);
                   
    } 
 
}

  

由于CMYK和RGB的不同,所以在转换后会出现颜色变亮。

 

你可能感兴趣的:(图片)