1

我正在尝试将图像转换为 PNG 格式,我拥有的数据是由 LZW 压缩的 4 波段 32 位类似 TIFF 的图像。通过使用 Java2D 和 JAI,我现在有未压缩的数据来表示 CMYK 空间中的颜色,并且可以在存储在 tiff 中时以与 4 波段 32 位格式相同的设置导出和查看。

问题是当我尝试转换为PNG等其他格式时,它会产生零大小的数据,所以我想问一下有没有人在转换此类图像方面有类似的经验?我把我的一些代码贴在下面供大家参考,如果发现错误还请指正,谢谢!!

int bands = 4;
int w = sizeParam.getHorizonPts();
int h = sizeParam.getVerticalPts();
ColorModel cm = new ComponentColorModel(new CMYKColorSpace(), new int[]{8,8,8,8},
                false, false, Transparency.OPAQUE, DataBuffer.TYPE_FLOAT);

// Create WritableRaster with four bands
WritableRaster r = RasterFactory.createBandedRaster(
                DataBuffer.TYPE_FLOAT, w, h, bands, null);
for (int i = 0; i < bandStreams.length; i++) {
        int x, y;
        x = y = 0;
        byte[] uncomp = new byte[w * h];
        decoder.decode(bandStreams[i], uncomp, h);
        for (int pos = 0; pos < uncomp.length; pos++) {
                r.setSample(x++, y, i, (float) (uncomp[pos] & 0xff) / 255);
                if (x >= w) {
                        x = 0;
                        y++;
                }
        }
}

// Create TiledImage
TiledImage tiledImage = new TiledImage(0, 0, w, h, 0, 0,
                RasterFactory.createBandedSampleModel(DataBuffer.TYPE_FLOAT, w,
                                h, bands), cm);
tiledImage.setData(r);
JAI.create("filestore", tiledImage, "test.tif", "TIFF");
4

1 回答 1

0

我终于通过将 CMYK 转换为 RGB 来解决这个问题,这样它就可以生成 PNG 图像,课程中使用了以下代码,

// Create target image with RGB color.
BufferedImage result = new BufferedImage(w, h,
            BufferedImage.TYPE_INT_RGB);

// Convert pixels from YMCK to RGB.
ColorConvertOp cmykToRgb = new ColorConvertOp(new CMYKColorSpace(),
            result.getColorModel().getColorSpace(), null);
cmykToRgb.filter(r, result.getRaster());
于 2012-11-01T23:21:28.513 回答