2015-01-08 3 views
0

Blob을 읽고 JPG로 변환 한 다음 Blob에 다시 쓰려고합니다. (참조로 전달되지만 TOAD에서 컴파일하려고하면 ImageIO.write에 오류 나는 그래서 다시 물방울로 JPG 버전을 쓸 수있는 RenderedImage의로 BufferedImage를 변환 할 방법Blob을 JPG로 변환하고 Blob을 업데이트하십시오.

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

이 업데이트 :.? 오류 메시지가

[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 
+0

어떤 오류가 발생 했습니까? 그것을 포함시켜 주시겠습니까? – Ascalonian

+0

편집 및 질문에 추가 – ubersnack

답변

3
입니다

Turned ou 그것은 단순한 실수였습니다. 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(); 
      } 
     } 
    } 
} 
/
관련 문제