简体   繁体   English

JAVA:如何从BufferedImage创建“ png”图像。 映像必须在DevIL库中可打开

[英]JAVA: How to create “png” image from BufferedImage. Image has to be openable in DevIL library

I need to save BufferedImage as png image. 我需要将BufferedImage保存为png图像。 At next step,I will need to open the image in another program(Avisynth). 下一步,我需要在另一个程序(Avisynth)中打开图像。 This program is able to open images, which I have drawn in ms-paint, but images created by my java program it is not able to open. 该程序可以打开我在ms-paint中绘制的图像,但是我的Java程序创建的图像无法打开。 Images from my program and from ms-paint are type of png, and in windows seems good. 来自我的程序和ms-paint的图像是png类型,在Windows中看起来不错。 It means I am able to open it, and image contains all what I have drawn. 这意味着我能够打开它,并且图像包含我绘制的所有内容。 In external program it throws following error: 在外部程序中,它引发以下错误:

Avisynth open failure:
ImageReader error 'Could not open file' in DevIL library.
reading image"C:\Images\mask\mirror.png"
DevIL version 166.
(C:\User\admin\Documents\4555.avs) 

here is code. 这是代码。 I tried it even with commented line. 我甚至用注释行尝试了它。 I found something on google. 我在Google上找到了一些东西。 This message is typical for bad image format. 此消息是典型的不良图像格式。 But I do not know how to do the same image like ms-paint does. 但是我不知道如何做像ms-paint一样的图像。

    BufferedImage img = new BufferedImage(video.getWidth(), video.getHeight(), BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = (Graphics2D) img.getGraphics();
    img.getGraphics().setColor(Color.white);
    c.paintAll(img.getGraphics());
    File f = new File(path + ".png");

    opencv_core.IplImage imgs = IplImage.createFrom(img);
    opencv_highgui.cvSaveImage(f.getPath(), imgs);
    //ImageIO.write(img, "png", f);

In code: 在代码中:

c is JComponent, whitch contains the image, whitch I want to save, and I used JavaCL library c是JComponent,其中包含图像,我要保存图像,并且我使用了JavaCL库

To read/write a BufferedImage, you can use javax.imageio.ImageIO. 要读取/写入BufferedImage,可以使用javax.imageio.ImageIO。 You can also add the TwelveMonkeys extension which improves the I/O operations with imageio. 您还可以添加TwelveMonkeys扩展,该扩展可改进imageio的I / O操作。

An other solution is to use JAI, but it has a memory leak issues when reading images. 另一种解决方案是使用JAI,但在读取图像时会出现内存泄漏问题。

The png format is a standard, supported by almost all the libraries/softwares. png格式是一种标准,几乎所有库/软件都支持。 If Avisynth cannot open an image created with your java software, it is certainly because you saved it into a rare format. 如果Avisynth无法打开使用Java软件创建的图像,那肯定是因为您将其保存为稀有格式。

I've looked at your attached images, and I they both seem perfectly valid PNG files, and open fine in any viewer/library that I have tried. 我查看了您附带的图像,它们看上去都是完全有效的PNG文件,并且可以在我尝试过的任何查看器/库中正常打开。 They both use standard deflate compression, adaptive filtering and are non-interlaced. 它们都使用标准的deflate压缩,自适应滤波并且是非隔行的。

However, there's one important difference, and that is the one written from Java has transparency (alpha channel), while the one from Paint does not. 但是,有一个重要的区别,那就是用Java编写的具有透明性(alpha通道),而用Paint编写的则没有透明性。 None of the images have transparent pixels, so you could try to just throw away the alpha channel, and see if that helps (only change the first line of your code): 没有图像具有透明像素,因此您可以尝试丢弃Alpha通道,看看是否有帮助(仅更改代码的第一行):

int w = video.getWidth();
int h = video.getHeight();
BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_3BYTE_BGR); 

TYPE_3BYTE_BGR is what the ImageIO PNGReader returns for an opaque PNG, so it's probably faster, but you could just as well use TYPE_INT_RGB I think. TYPE_3BYTE_BGR是ImageIO PNGReader为不透明的PNG返回的内容,因此它可能更快,但是我认为也可以使用TYPE_INT_RGB

This small change in code, should produce an image that is closer to what Paint creates. 代码上的这一小变化,应该会产生与Paint创建的图像更接近的图像。 I'm quite confident that this change should produce a PNG that DevIL/Avisynth can read. 我非常有信心此更改将产生一个DevIL / Avisynth可以读取的PNG。

(There's also another small difference between the images, that is the Paint image explicitly contains an sRGB chunk (and some other auxiliary chunks), but that shouldn't matter). (图像之间还有另一个小差异,即Paint图像显式包含sRGB块(和其他一些辅助块),但这无关紧要)。

Other than that, I see that DevIL is using LibPNG (which is probably the most standard and widely used PNG library out there) to read PNGs, so I do find it kind of strange that it cannot read this particular PNG. 除此之外,我还发现DevIL正在使用LibPNG(这可能是最标准且使用最广泛的PNG库)来读取PNG,因此我确实感到奇怪,因为它无法读取此特定的PNG。 But, it could be something about this version, the way its built or something (I'm no C/C++ programmer, nor do I know the library in depth). 但是,这可能与该版本有关,其构建方式或其他原因(我不是C / C ++程序员,也不了解库的详细信息)。 You should probably talk to (file an issue with) the developers of the library/program about this. 您可能应该就此与图书馆/程序的开发人员进行交谈(并向其提出问题)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM