Convert Blob to JPG and update blob

拈花ヽ惹草 提交于 2019-11-29 12:28:03

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();
            }
        }
    }
}
/
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!