java下cmyk图片读取和转换rgb,以及图片压缩

首先感谢

https://www.cnblogs.com/interdrp/p/7076202.html

https://blog.csdn.net/ybn187/article/details/52185269

https://blog.csdn.net/keketrtr/article/details/88129641。

 

package test;

import com.joyhua.utils.CMYKUtil;
import com.joyhua.utils.CMYKUtil1;
import net.coobird.thumbnailator.Thumbnails;

import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import java.awt.image.BufferedImage;
import java.awt.image.Raster;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;
import java.util.*;

public class test4 {
    public static void main(String[] args) {

        String sourceString = "E:\\test\\p1.jpg";
        File source = new File(sourceString);

        String toString = "E:\\test\\p1_rgb.jpg";
        File to = new File(toString);

        String toString1 = "E:\\test\\p1_s.jpg";
        File to1 = new File(toString);
        try {
            Thumbnails.of(CMYKUtil.readImage(source)).width(500).height(704).outputFormat("jpg").toFile(to1);

            //图片是cmyk,需要转换为rgb
           // Thumbnails.of(source).width(500).height(704).outputFormat("jpg").toFile(to);
        } catch (IOException e) {
            e.printStackTrace();
        }



    }
}
package com.joyhua.utils;

import org.apache.log4j.Logger;

import javax.imageio.IIOException;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import java.awt.*;
import java.awt.color.ColorSpace;
import java.awt.image.*;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;

/**
 * CREATE BY zz ON 2019-03-26
 **/
public class CMYKUtil {

    static Logger log = Logger.getLogger(CMYKUtil.class);
    /**
     * @description: CMYK格式图片转RGB格式图片的处理,因为Thumbnails无法处理CMYK格式图片
     */
        public static BufferedImage readImage(File file) throws IOException {

            return readImage(ImageIO.createImageInputStream(file));
        }

        public static BufferedImage readImage(InputStream stream) throws IOException {

            return readImage(ImageIO.createImageInputStream(stream));
        }

        public static BufferedImage readImage(ImageInputStream input) throws IOException {
            Iterator readers = ImageIO.getImageReaders(input);
            if (readers == null || !readers.hasNext()) {
                return null;
            }

            ImageReader reader = (ImageReader) readers.next();
            reader.setInput(input);

            BufferedImage image;
            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);
        }
}

 

转换完后会发现图片颜色不对了,需要引入一下三个maven


			com.twelvemonkeys.imageio
			imageio-jpeg
			3.5
		
		
			com.twelvemonkeys.imageio
			imageio-tiff
			3.5
		
		
			com.twelvemonkeys.servlet
			servlet
			3.5
		

jdk的ImageIO有自动发现功能,会自动查找相关的编解码类并使用,而不使用jdk默认的编解码类,所以使用这个库是完全无入侵的。


 

 

你可能感兴趣的:(java,CMYK转RGB)