我正在以这种方式将gif图像转换为java:
File infile = new File(filename);
BufferedImage bufimg = ImageIO.read(infile);
ImageWriter writer = ImageIO.getImageWritersByFormatName("tiff").next();
ImageWriteParam param = writer.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionType(COMPRESSION_TYPE_GROUP4FAX);
File outfile = new File(outFilename);
ImageOutputStream ios = ImageIO.createImageOutputStream(outfile);
writer.setOutput(ios);
// image4j for reducing to black & white
BufferedImage convertedImage = ConvertUtil.convert1(bufimg);
writer.write(null, new IIOImage(convertedImage ,null,null),param);
writer.dispose();
我也尝试过不使用image4j:
IndexColorModel icm = new IndexColorModel(1, 2, new byte[] { (byte) 0,
(byte) 0xFF }, new byte[] { (byte) 0, (byte) 0xFF },
new byte[] { (byte) 0, (byte) 0xFF });
BufferedImage convertedImage = new BufferedImage(bufimg.getWidth(), bufimg.getHeight(),
BufferedImage.TYPE_BYTE_BINARY, icm);
cco.filter(bufimg, convertedImage);
两种方法都可以正常工作,除了生成的文件比原始gif大得多。 tiffinfo(来自libtiff)将其识别为G4,irfanview告诉我当前的内存大小(读取后的图像大小)为120 kB(与914 kB文件大小相比)。
我错过了什么 - 我怎么能减少结果图像的文件大小?