将Blob转换为JPG并更新blob [英] Convert Blob to JPG and update blob

查看:947
本文介绍了将Blob转换为JPG并更新blob的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to read in a blob, convert it to JPG and then write back to the blob (it is being passed in by reference, but when trying to compile in TOAD I get an error on 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?

How would I convert a BufferedImage into a RenderedImage so I can write the JPG version back into the 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


推荐答案

原来这是一个简单的错误, ImageIO.write 接受一个RenderedImage,这意味着我有将BufferedImage强制转换为RenderedImage,我在<$ c $中编写了!== 而不是!= c>最后阻止。请参阅下面有关成功编译的内容

Turned out it was a simple mistake, ImageIO.write takes in a RenderedImage which meant I had to cast the BufferedImage to RenderedImage, and I had written !== instead of != in the finally block. See below for what compiles successfully

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();
            }
        }
    }
}
/

这篇关于将Blob转换为JPG并更新blob的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆