【问题标题】:Convert Blob to JPG and update blob将 Blob 转换为 JPG 并更新 Blob
【发布时间】:2015-03-06 22:02:04
【问题描述】:

我正在尝试读取一个 blob,将其转换为 JPG,然后写回该 blob(它是通过引用传入的,但是当尝试在 TOAD 中编译时,我在 ImageIO.write 上遇到错误。

CREATE OR REPLACE AND RESOLVE JAVA SOURCE NAMED BANNADMIN.IMAGE_CONVERTER
   AS package uk.co.ImageUtil;

import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
import java.awt.image.BufferedImage;
import oracle.sql.*;
import java.io.OutputStream;

public class ImageConverter {
    public static void convertImage(BLOB[] blob) {
       BufferedImage image = null;
       OutputStream outputStream = null;
        try {
            image = ImageIO.read(blob[0].getBinaryStream());

            outputStream = blob[0].setBinaryStream(0);

            ImageIO.write(image, "JPG", outputStream);

        } catch (IOException e) {
            e.printStackTrace();
        }
        catch (SQLException e) {
            e.printStackTrace();
        }
        catch(IllegalArgumentException e) {
            e.printStackTrace();
        }
        finally {
            try {
                if (outputStream !== null) {
                    outputStream.flush();
                    outputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
/

如何将 BufferedImage 转换为 RenderedImage,以便将 JPG 版本写回 Blob?

更新:错误信息是

[Error]  (1: 0): IMAGE_CONVERTER:28: cannot find symbol
[Error]  (1: 0): symbol  : method    write(java.awt.image.BufferedImage,java.lang.String,java.lang.Object)
[Error]  (1: 0): location: class javax.imageio.ImageIO
[Error]  (1: 0):             ImageIO.write(image, "jpg", outputStream);
[Error]  (1: 0):                    ^
[Error]  (1: 0): 1 error

【问题讨论】:

  • 你遇到了什么错误?可以请附上吗?
  • 已编辑并添加到问题中

标签: java plsql toad


【解决方案1】:

原来这是一个简单的错误,ImageIO.write 接受了 RenderedImage,这意味着我必须将 BufferedImage 转换为 RenderedImage,并且我在 finally 块中写了 !== 而不是 !=。编译成功的内容见下文

CREATE OR REPLACE AND RESOLVE JAVA SOURCE NAMED BANNADMIN.IMAGE_CONVERTER AS package uk.co.ImageUtil;

import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import oracle.sql.*;
import java.io.OutputStream;
import java.sql.SQLException;

public class ImageConverter {
    /**
      * Take in a BLOB file (specified as an array parameter but we only ever use [0])
      * Read in the binary stream of the BLOB
      * Change the binary stream to jpg
      * Write the binary stream jpg to the BLOB
      * The BLOB parameter is passed in via out - so there is no need to return the BLOB, only edit it
      */
    public static void convertImage(BLOB[] blob) {
       BufferedImage bufferedImage = null;
       OutputStream outputStream = null;
        try {
            bufferedImage = ImageIO.read(blob[0].getBinaryStream());

            outputStream = blob[0].setBinaryStream(0);

            RenderedImage renderedImage = (RenderedImage)bufferedImage;

            ImageIO.write(renderedImage, "JPG", outputStream);

        } catch (IOException e) {
            e.printStackTrace();
        }
        catch (SQLException e) {
            e.printStackTrace();
        }
        catch(IllegalArgumentException e) {
            e.printStackTrace();
        }
        finally {
            try {
                if (outputStream != null) {
                    outputStream.flush();
                    outputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
/

【讨论】:

    猜你喜欢
    • 2011-01-31
    • 2021-10-14
    • 2012-03-28
    • 2018-08-26
    • 1970-01-01
    • 2019-01-03
    • 2013-09-10
    • 2020-06-15
    • 2019-03-28
    相关资源
    最近更新 更多