简体   繁体   English

在java中将java.awt.Image保存到磁盘

[英]Save java.awt.Image to disk in java

I have read clob from oracle procedure and convert them into java.awt.Image object by the following code . 我从oracle过程中读取了clob,并通过以下代码将它们转换为java.awt.Image对象。

InputStream stream = clob.getAsciiStream();
                ByteArrayOutputStream output = new ByteArrayOutputStream();
                int a1 = stream.read();
                while (a1 >= 0) {
                  output.write((char) a1);
                  a1 = stream.read();
                }
                Image myImage = Toolkit.getDefaultToolkit().createImage(output.toByteArray());
                output.close();

Now, I want to save myImage to hard disk. 现在,我想将myImage保存到硬盘。 So, What must I do to save these java.awt.Image to file ? 那么,我该怎么做才能将这些java.awt.Image保存到文件中?

My try : 我的尝试:

I have cast the myImage to BufferedImage and then write this to ImageIo by the following code : 我已将myImage强制转换为BufferedImage,然后通过以下代码将其写入ImageIo:

BufferedImage bi = (BufferedImage)myImage;
               ImageIO.write(bi, "jpg",new File("E:\\out.jpg"));

But I am getting the following exception : 但我得到以下异常:

Got Exception as : sun.awt.image.ToolkitImage cannot be cast to java.awt.image.BufferedImage
java.lang.ClassCastException: sun.awt.image.ToolkitImage cannot be cast to java.awt.image.BufferedImage
    at com.connect.home.Home.getHomeParameter(Home.java:103)
    at com.connect.home.Home.main(Home.java:141)

How can I remove this error ? 如何删除此错误? Any advice is of great help . 任何建议都有很大帮助。

First 第一

Oracle JDBC drivers support the manipulation of data streams in either direction between server and client. Oracle JDBC驱动程序支持在服务器和客户端之间以任一方向操纵数据流。 The drivers support all stream conversions: binary, ASCII, and Unicode. 驱动程序支持所有流转换:二进制,ASCII和Unicode。 Following is a brief description of each type of stream: 以下是每种类型的流的简要说明:

Binary. 二进制。 Used for RAW bytes of data, and corresponds to the getBinaryStream method 用于RAW字节数据,对应于getBinaryStream方法

ASCII. ASCII。 Used for ASCII bytes in ISO-Latin-1 encoding, and corresponds to the getAsciiStream method 用于ISO-Latin-1编码中的ASCII字节,对应于getAsciiStream方法

Unicode. Unicode格式。 Used for Unicode bytes with the UTF-16 encoding, and corresponds to the getUnicodeStream method 用于UTF-16编码的Unicode字节,对应于getUnicodeStream方法

So probably you will need to use clob.getBinaryStream to retrieve your stream. 所以你可能需要使用clob.getBinaryStream来检索你的流。

Second. 第二。 You can not save ToolKitImage whith ImageIO::write because it expect a RenderedImage instance.And ToolkitImage not implements this interface. 你不能将ToolKitImage保存ToolKitImage ImageIO :: write,因为它期望一个RenderedImage实例。 ToolkitImage不实现这个接口。

So you have two options. 所以你有两个选择。

Read the stream with ImageIO 使用ImageIO读取流

    RenderedImage image = ImageIO.read(clob.getBinaryStream());
    ImageIO.write(image, "JPG",new File("E:\\out.jpg"));

Or draw the ToolkitImage on a RenderedImage 或者在RenderedImage上绘制ToolkitImage

    BufferedImage bi = new BufferedImage(myImage.getWidth(null), myImage.getHeight(null), BufferedImage.TYPE_INT_RGB);
    bi.getGraphics().drawImage(myImage,0,0, null);
    ImageIO.write(bi, "JPG", new File("E:\\out.jpg"));

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

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