简体   繁体   English

如何将缓冲的图像转换为图像,反之亦然?

[英]How to convert buffered image to image and vice-versa?

Actually i am working on a image editing software and now i want to convert the buffered-image ie : 其实我正在研究图像编辑软件,现在我想转换缓冲图像,即:

  BufferedImage buffer = ImageIO.read(new File(file));

to Image ie in the format something like : 到图像,即格式如下:

  Image image  = ImageIO.read(new File(file));

Is it possible to so?? 有可能吗? If yes, then how?? 如果是,那怎么样?

BufferedImage is a(n) Image, so the implicit cast that you're doing in the second line is able to be compiled directly. BufferedImage 是一个(n)图像,因此您在第二行中执行的隐式转换可以直接编译。 If you knew an Image was really a BufferedImage, you would have to cast it explicitly like so: 如果你知道一个Image真的是一个BufferedImage,你必须像这样明确地转换它:

Image image = ImageIO.read(new File(file));
BufferedImage buffered = (BufferedImage) image;

Because BufferedImage extends Image, it can fit in an Image container. 因为BufferedImage扩展了Image,所以它可以放在Image容器中。 However, any Image can fit there, including ones that are not a BufferedImage, and as such you may get a ClassCastException at runtime if the type does not match, because a BufferedImage cannot hold any other type unless it extends BufferedImage. 但是,任何Image都可以适合那里,包括那些不是BufferedImage的Image,因此如果类型不匹配,你可能会在运行时获得ClassCastException,因为BufferedImage除非扩展BufferedImage,否则不能保存任何其他类型。

Example: say you have an 'image' you want to scale you will need a bufferedImage probably, and probably will be starting out with just 'Image' object. 例如:假设你有一个想要扩展的'图像',你可能需要一个bufferedImage,并且可能只是从'Image'对象开始。 So this works I think... The AVATAR_SIZE is the target width we want our image to be: 所以我认为这是有效的... AVATAR_SIZE是我们希望图像的目标宽度:

Image imgData = image.getScaledInstance(Constants.AVATAR_SIZE, -1, Image.SCALE_SMOOTH);     

BufferedImage bufferedImage = new BufferedImage(imgData.getWidth(null), imgData.getHeight(null), BufferedImage.TYPE_INT_RGB);

bufferedImage.getGraphics().drawImage(imgData, 0, 0, null);

BufferedImage is a subclass of Image . BufferedImageImage的子类。 You don't need to do any conversion. 您无需进行任何转换。

The right way is to use SwingFXUtils.toFXImage(bufferedImage,null) to convert a BufferedImage to a JavaFX Image instance and SwingFXUtils.fromFXImage(image,null) for the inverse operation. 正确的方法是使用SwingFXUtils.toFXImage(bufferedImage,null)将BufferedImage转换为JavaFX Image实例,使用SwingFXUtils.fromFXImage(image,null)进行逆操作。

Optionally the second parameter can be a WritableImage to avoid further object allocation. 可选地,第二参数可以是WritableImage以避免进一步的对象分配。

You can try saving (or writing) the Buffered Image with the changes you made and then opening it as an Image. 您可以尝试使用所做的更改保存(或写入)缓冲图像 ,然后将其作为图像打开。

EDIT: 编辑:

try {
    // Retrieve Image
    BufferedImage buffer = ImageIO.read(new File("old.png"));;
    // Here you can rotate your image as you want (making your magic)
    File outputfile = new File("saved.png");
    ImageIO.write(buffer, "png", outputfile); // Write the Buffered Image into an output file
    Image image  = ImageIO.read(new File("saved.png")); // Opening again as an Image
} catch (IOException e) {
    ...
}

Just an information: let us all remember that the Image class is actually an abstract class and referencing a variable of this with a BufferedImage only stores or returns any Object's memory adress. 只是一个信息:让我们都记住Image类实际上是一个抽象类,并使用BufferedImage引用它的变量只存储或返回任何Object的内存地址。

Also, wherefore, static java.awt.image.imageIO 's read() method returns a BufferedImage object, therefore no doubt that using operator/expression instanceof BufferedImage on that object will return true . 此外,静态java.awt.image.imageIOread()方法返回一个BufferedImage对象,因此毫无疑问,在该对象上使用运算符/表达式instanceof BufferedImage将返回true

In fact, being abstract, Image class has such method signatures as: 实际上,作为抽象, Image类具有以下方法签名:

  1. public abstract Graphics getGraphics()
  2. public abstract ImageProducer getSource()

among others. 等等。

I emphasize, an actual Image variable only holds memory adress of a concrete Image-subclass object, almost like pointers in C, C++, Ada, etc. 我强调,实际的Image变量只保存具体Image-subclass对象的内存地址,几乎就像C,C ++,Ada等中的指针一样。

If you're introduced or advanced in those languages, and also of Java interface instances like Runnable , javax.sound.Clip , AWT's Shape , etc.. . 如果您使用这些语言介绍或高级,还有Java接口实例,如Runnablejavax.sound.Clip ,AWT的Shape等等。 Take note that Image has: public abstract Image getScaledInstance(...) - you get the point. 注意Image有: public abstract Image getScaledInstance(...) - 你明白了。 (Of course, scaling in 2D Graphics programming is interchangeable to resizing, for which precision is desirable). (当然,2D图形编程中的缩放可以与调整大小互换,因此需要精度)。

But in an impossible case when herein ImageIO method return ! (instanceof BufferedImage) 但在一个不可能的情况下,这里ImageIO方法返回! (instanceof BufferedImage) ! (instanceof BufferedImage) just create a new BufferedImage object with this ImgObjNotInstncfBufImg apassed to one of its constructor argument. ! (instanceof BufferedImage)只是创建一个新的BufferedImage对象,其ImgObjNotInstncfBufImg包含在其构造函数参数之一中。 Then, at (rational) will manipulate this in the logic of your code. 然后,在(理性)将在代码的逻辑中操纵它。

Anyways, the Affine Transform class is appropriate for transforming Shapes and Images to thier scaled, rotated, relocated, etc forms, so I recommend you to study about using an "affine transform". 无论如何,仿射变换类适用于将形状和图像转换为缩放,旋转,重定位等形式,因此我建议您研究使用“仿射变换”。

Take note that you can manipulate the actual pixels in such Image's Raster - well another technical 2D Graphics jargon which must be referenced from a technical glossary - which perhaps a excercised skill in Java ways of binary blitwise operations will be needed, in types of Image buffers that store individual color attributes in a compact in of 32-bytes - 7-bits each for the alpha and RGB values. 请注意,您可以操纵此类图像栅格中的实际像素 - 这是另一个必须从技术词汇表中引用的技术2D图形术语 - 在图像缓冲区的类型中,可能需要以Java方式进行二进制blitwise操作的精通技巧将单个颜色属性存储在32字节的紧凑中 - 每个7位用于alpha和RGB值。

I suspect your gonna use it in layering images. 我怀疑你会用它来分层图像。 So, FINALLY, the rational is that you only reference BufferedImage with the abstract Image, and if ever your Image object isn't a BufferedImage one yet, then you can just make an image out of this related-but-non-BufferedImage-instance without having to worry about any conversion, casting, autoboxing or whatever; 所以,最后,理性的是你只用抽象的Image引用BufferedImage ,如果你的Image对象还不是BufferedImage ,那么你可以用这个相关但非BufferedImage实例制作一个图像。无需担心任何转换,铸造,自动装箱等等; manipulating a BufferedImage really means manipulating also the underlying root Image data-bearing object that it points to. 操纵BufferedImage实际上意味着还要操纵它所指向的基础图像数据承载对象。

Okay, finished; 好的,完成了; I think I certainly extracted and splintered out what deadlock you may have thought you are facing to. 我想我肯定已经解开了你可能认为你所面临的僵局。 As I have said abstract classes in java, and also interfaces, are very much the equivaleng of the low-level, more-close-to-hardware operators called pointers in other languages. 正如我所说的java中的抽象类以及接口,非常类似于其他语言中称为指针的低级,更接近硬件的运算符。

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

相关问题 如何将CLOB转换为图像,反之亦然? - How to convert CLOB to image or vice versa? Java:如何将二进制值的字符串转换为Float,反之亦然? - Java: How to convert a String of Binary values to a Float and vice-versa? 如何将int []转换为OpenCV Mat? (反之亦然) - How to convert an int[] to an OpenCV Mat? (and vice-versa) 有多少种方法可以将位图转换为字符串,反之亦然? - How many ways to convert bitmap to string and vice-versa? 将xml转换为hashmap,反之亦然 - Convert xml to hashmap and vice-versa 将byte转换为int,反之亦然 - Convert byte to int and vice-versa 在android'java'中如何将Bitmap对象转换为Image对象,反之亦然 - How convert Bitmap Object to Image Object and vice versa in android 'java' 如何在Java中将RGB图像转换为CMYK,反之亦然? - how can I convert an RGB image to CMYK and vice versa in Java? 如何编写 Apache 骆驼路线来检查 XML 内容并将其转换为 JSON 和 Z3418008FDD81C28 引导服务反之亦然 - How to write a Apache Camel route to check and convert XML contents to JSON and vice-versa for a Spring boot service 编年史队列:将循环整数转换为时间戳,反之亦然 - Chronicle Queue: Convert cycle integer to timestamp and vice-versa
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM