我在 oracle 12c 中的 blob 列中调整图像大小有问题 [英] I have problem with image resize in blob column in oracle 12c

查看:94
本文介绍了我在 oracle 12c 中的 blob 列中调整图像大小有问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想调整来自 blob 列的图像的大小以最大化它.我为此做了一个功能,但收到此错误消息

I want to resize image came from blob column to maximize it. I make a FUNCTION for this but I get this error message

PLS-00201 - 必须声明标识符 ORDSYS.OrdImage

PLS-00201 - identifier ORDSYS.OrdImage must be declared

CREATE OR REPLACE FUNCTION "resize_img" (resize_img in BLOB) 
RETURN BLOB 
IS 
        vImageData BLOB; 
        vSizedImage BLOB; 
BEGIN 
   DBMS_Lob.createTemporary(vSizedImage, FALSE, DBMS_LOB.CALL); 
   ORDSYS.OrdImage.processCopy(vImageData, 'maxScale=75 75', vSizedImage); 
   RETURN vSizedImage; 
END;

推荐答案

Ordimage 已从 19c 中删除(我知道您正在使用 12c)但这里有一种无需 Ordimage 的方法.我花了大约一周的时间从多个来源和大量阅读、谷歌搜索和反复试验中拼凑起来.

Ordimage has been removed from 19c (I know you are using 12c) but here is a way to do it without Ordimage. It took me about a week to piece together from multiple sources and alot of reading, googling and trial and error.

希望这能帮助需要在 Oracle 19c 中调整图像大小的其他人.

Hopefully this will help someone else that needs to resize an image in Oracle 19c.

Oracle Functions:
function BLOB_THUMBNAIL(P_BLOB in Blob, P_MAX_SIZE in Number, P_ATTACH_SID in varchar2 := null) return Blob is

        V_DST              Blob;

    begin

        V_DST := resizeBLOB(P_BLOB, P_MAX_SIZE, P_MAX_SIZE);

    end BLOB_THUMBNAIL;

    function BLOB_THUMBNAIL_OLD(P_BLOB in Blob, P_MAX_SIZE in Number) return Blob is

        V_DST   Blob;

    begin
        DBMS_LOB.CREATETEMPORARY(V_DST, true);

        DBMS_LOB.OPEN(V_DST, DBMS_LOB.LOB_READWRITE);

        ORDSYS.ORDIMAGE.PROCESSCOPY(P_BLOB, 'maxScale=' || P_MAX_SIZE || ' ' || P_MAX_SIZE, V_DST);

        DBMS_LOB.CLOSE(V_DST);

        return V_DST;

end BLOB_THUMBNAIL_OLD;


Java Class:
SET DEFINE OFF;
CREATE OR REPLACE AND RESOLVE JAVA SOURCE NAMED "ResizeImage" as 
import java.lang.*;
import java.sql.*;
import java.io.*;
import oracle.sql.*;
import java.awt.image.BufferedImage;
import java.awt.Image;
import java.awt.Color;
import javax.imageio.ImageIO;
import oracle.jdbc.driver.*;

public class ResizeImage extends Object
{
 public static java.sql.Blob resizeBLOB(java.sql.Blob img, int newW, int newH)
 {
  try
     {
      byte [] newdata = img.getBytes(1L, (int)img.length());
      newdata = scale(newdata, newW, newH);

      oracle.jdbc.OracleConnection conn = (oracle.jdbc.OracleConnection)new OracleDriver().defaultConnection();
      java.sql.Blob retBlob = conn.createBlob();

      try
         {
          java.io.OutputStream outStr = retBlob.setBinaryStream(0);
          outStr.write(newdata, 0, newdata.length);
          outStr.flush();
          outStr.close();
         }
      catch (IOException ioe)
           {
            System.out.println("IO Error trying to write the outputstream.");  
            ioe.printStackTrace();
           } 
           
      return retBlob;
     }
  catch (SQLException ex)
       {
        System.out.println("SQLException Error.");  
        ex.printStackTrace();
       }  

  return img;
 }

 public static byte[] scale(byte[] fileData, int width, int height) 
 {
  double newW;
  double newH;
 
  ByteArrayInputStream in = new ByteArrayInputStream(fileData);
  try 
     {
      BufferedImage img = ImageIO.read(in);

      if(height == 0) 
        height = 100; 

      if(width == 0)
        width = 100;

      //Figure new Width and Height with Aspect Ratio
      double imgW = img.getWidth();
      double imgH = img.getHeight();

      if(imgH>imgW)
        {
         newW = (imgW/imgH)*width;
         newH = height;
        }
      else
        {
         newH = (imgH/imgW)*height;
         newW = width;
        }

      Image scaledImage = img.getScaledInstance((int)newW, (int)newH, Image.SCALE_SMOOTH);
      BufferedImage imageBuff = new BufferedImage((int)newW, (int)newH, BufferedImage.TYPE_INT_RGB);
      imageBuff.getGraphics().drawImage(scaledImage, 0, 0, new Color(0,0,0), null);

      ByteArrayOutputStream buffer = new ByteArrayOutputStream();

      ImageIO.write(imageBuff, "jpg", buffer);
      return buffer.toByteArray();
     } 
   catch (IOException e)
        {
         //throw new ApplicationException("IOException in scale");
         e.printStackTrace();       
        }
  return fileData;
 }
}

Oracle Function to call Java:
CREATE OR REPLACE function resizeBLOB( p_img in blob, newW in number, newH in number) return blob
as
language java
name 'ResizeImage.resizeBLOB(java.sql.Blob, int, int) return java.sql.Blob';
/


这篇关于我在 oracle 12c 中的 blob 列中调整图像大小有问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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