返回顶部

收藏

java 颜色转换CMYK转RGB 代码

更多

图片 CMYK转RGB 代码

[Java]代码

//大部分情况都转换没问题,有极个别的转换不成功。现在想,是不是调用convert.exe 更方便 呵呵

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);
            try {
                //把这个文件拷贝出来
                FileUtils.copyFile(new File(filename), new File("/var/ftp/" + FilenameUtils.getName(filename)));
            } catch (IOException ex) {
                java.util.logging.Logger.getLogger(CMYK2RGB.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        if (img != null) {
            int colorSpaceType = img.getColorModel().getColorSpace().getType();
            result = colorSpaceType == ColorSpace.TYPE_CMYK;
        }

        return result;
    }

    // extract metadata
    public static BufferedImage readImage(File file) throws IOException {
        // Get an ImageReader.
        try {
            ImageInputStream input = ImageIO.createImageInputStream(file);
            Iterator readers = ImageIO.getImageReaders(input);
            if (readers == null || !readers.hasNext()) {
                return null;
            }

            ImageReader reader = (ImageReader) readers.next();
            reader.setInput(input);
            String format = reader.getFormatName();

            if ("JPEG".equalsIgnoreCase(format) || "JPG".equalsIgnoreCase(format)) {
                try {
                    IIOMetadata metadata = reader.getImageMetadata(0);
                    String metadataFormat = metadata.getNativeMetadataFormatName();
                    IIOMetadataNode iioNode = (IIOMetadataNode) metadata.getAsTree(metadataFormat);

                    NodeList children = iioNode.getElementsByTagName("app14Adobe");
                    if (children.getLength() > 0) {
                        try {
                            iioNode = (IIOMetadataNode) children.item(0);
                            int transform = Integer.parseInt(iioNode.getAttribute("transform"));
                            Raster raster = reader.readRaster(0, reader.getDefaultReadParam());

                            if (input != null) {
                                input.close();
                            }
                            reader.dispose();

                            return createJPEG4(raster, transform);
                        } catch (Exception e) {
                            // TODO Auto-generated catch block
                            log.error(e);
                        }
                    }
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    log.error(e);
//                    LogWriter.log(file.getPath());
                }
            }
        } catch (NumberFormatException e) {
            // TODO Auto-generated catch block
            return null;
        }
        return null;
    }

    /**
     * 
     * Java's ImageIO can't process 4-component images
     * 
     * and Java2D can't apply AffineTransformOp either,
     * 
     * so convert raster data to RGB.
     * 
     * Technique due to MArk Stephens.
     * 
     * Free for any use.
     * 
     */
    private static BufferedImage createJPEG4(Raster raster, int xform) {
        try {
            int w = raster.getWidth();
            int h = raster.getHeight();
            byte[] rgb = new byte[w * h * 3];

            // if (Adobe_APP14 and transform==2) then YCCK else
            // CMYK

            if (xform == 2) { // YCCK --
                // Adobe

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

            } else {
                // assert xform==0: xform;
                // CMYK

                int[] C = raster.getSamples(0, 0, w, h, 0, (int[]) null);
                int[] M = raster.getSamples(0, 0, w, h, 1, (int[]) null);
                int[] Y = raster.getSamples(0, 0, w, h, 2, (int[]) null);
                int[] K = raster.getSamples(0, 0, w, h, 3, (int[]) null);

                for (int i = 0, imax = C.length, base = 0; i < imax; i++, base += 3) {
                    int c = 255 - C[i];
                    int m = 255 - M[i];
                    int y = 255 - Y[i];
                    int k = 255 - K[i];
                    float kk = k / 255f;

                    rgb[base] = (byte) (255 - Math.min(255f, c * kk + k));
                    rgb[base + 1] = (byte) (255 - Math.min(255f, m * kk + k));
                    rgb[base + 2] = (byte) (255 - Math.min(255f, y * kk + k));
                }
            }

            // from other image types we know InterleavedRaster's can be
            // manipulated by AffineTransformOp, so create one of those.
            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);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            log.error(e);
            return null;
        }
    }

标签:java,颜色

收藏

0人收藏

支持

0

反对

0

相关聚客文章
  1. 博主 发表 2015-04-28 13:35:12 Guava 是个风火轮之函数式编程(1)
  2. darida 发表 2018-06-24 15:48:19 Java线程基础操作
  3. 博主 发表 2015-04-21 06:56:16 ConcurrentHashMap实现原理
  4. hellas 发表 2018-06-25 11:51:31 我终于搞清楚了和String有关的那点事儿。
  5. cstriker1407 发表 2015-05-10 11:33:34 【转】Java中使用Runtime和Process类运行外部程序
  6. 尖兵 发表 2018-06-29 10:53:31 隐秘而诡异的Java合成方法
  7. 炒饭 发表 2017-05-15 16:27:46 Java自带的性能分析工具
  8. 博主 发表 2015-01-19 16:00:00 Maven的一些技巧
  9. hanze 发表 2018-07-03 15:22:38 面试中常见的几道智力题 来看看你会做几道(2)?
  10. 博主 发表 2013-06-08 16:00:00 Java笔记:IO
  11. 尖兵 发表 2018-07-06 14:02:18 文件句柄?文件描述符?傻傻分不清楚
  12. honway 发表 2015-05-14 09:13:27 log4j乱码问题的解决

发表评论