Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java apache POI XWPF文档中的图像存在颜色失真_Java_Apache Poi_Bufferedimage_Xwpf_Bufferedoutputstream - Fatal编程技术网

Java apache POI XWPF文档中的图像存在颜色失真

Java apache POI XWPF文档中的图像存在颜色失真,java,apache-poi,bufferedimage,xwpf,bufferedoutputstream,Java,Apache Poi,Bufferedimage,Xwpf,Bufferedoutputstream,我正试图设计一个报告模板,其中有许多(数百)图片被超链接引用。我希望文档小于25Mb(出于电子邮件和其他原因),因此我尝试使用以下代码压缩图像: //I get the input stream InputStream ins = entity.images.getInputStream(img); BufferedImage bufImg = ImageIO.read(ins); //I compress the image ByteArrayOutputStream compressed

我正试图设计一个报告模板,其中有许多(数百)图片被超链接引用。我希望文档小于25Mb(出于电子邮件和其他原因),因此我尝试使用以下代码压缩图像:

//I get the input stream
InputStream ins = entity.images.getInputStream(img);
BufferedImage bufImg = ImageIO.read(ins);

//I compress the image
ByteArrayOutputStream compressed = new ByteArrayOutputStream();
ImageOutputStream outputStream = ImageIO.createImageOutputStream(compressed);
ImageWriter jpgWriter = ImageIO.getImageWritersByFormatName("jpg").next();
ImageWriteParam jpgWriteParam = jpgWriter.getDefaultWriteParam();
jpgWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
jpgWriteParam.setCompressionQuality(0.98f);
jpgWriter.setOutput(outputStream);
jpgWriter.write(null, new IIOImage(bufImg, null, null), jpgWriteParam);
jpgWriter.dispose();
byte[] jpegData = compressed.toByteArray();

//I attempt to add the compressed image
imgRun.addPicture(new ByteArrayInputStream(jpegData), Document.PICTURE_TYPE_JPEG,"text", Units.toEMU(newWidth), Units.toEMU(newHeight));

图像写入文档,但其颜色失真。就我而言,它们都是红色/橙色的。你知道是什么原因导致了这一切吗?

看起来问题在于一些图像是.gif

无论如何,我解决了这个问题,首先简单地将所有图像在压缩之前转换为.jpg。见下文:

BufferedImage newBufferedImage = new BufferedImage(bufImg.getWidth(),
          bufImg.getHeight(), BufferedImage.TYPE_INT_RGB);
newBufferedImage.createGraphics().drawImage(bufImg, 0, 0, Color.WHITE, null);

我没有答案,但看起来您正在对文件使用JPG,然后将其添加为PNG。代码正确吗?我修复了帖子,应该是PICTURE\u TYPE\u JPEG。但问题依然存在。有什么想法吗?图像在放入文档之前可以吗?可以。如果我添加未压缩的图像,就可以了。因此,问题似乎在于压缩本身。输入图像的文件格式是什么?它使用什么颜色型号?它有阿尔法通道吗?你能附上一张图片,用扭曲的颜色显示结果吗?PS:如果您希望文件大小更小,使用非常高的JPEG质量,如
0.98
可能不是您想要的。值越低,图像越小,
0.75
是默认值。