--BASE64转图片
CREATE OR REPLACE FUNCTION DECODE_BASE64(P_CLOB_IN IN CLOB) RETURN BLOB IS
V_BLOB BLOB;
V_OFFSET INTEGER;
V_TEM_CLOB CLOB;
V_BUFFER_VARCHAR VARCHAR2(32000);
V_BUFFER_RAW RAW(32000);
V_BUFFER_SIZE BINARY_INTEGER := 32000;
BEGIN
IF P_CLOB_IN IS NULL THEN
RETURN NULL;
END IF;
DBMS_LOB.CREATETEMPORARY(V_BLOB, TRUE);
V_OFFSET := 1;
FOR I IN 1 .. CEIL(DBMS_LOB.GETLENGTH(P_CLOB_IN) / V_BUFFER_SIZE) LOOP
DBMS_LOB.READ(P_CLOB_IN, V_BUFFER_SIZE, V_OFFSET, V_BUFFER_VARCHAR);
V_BUFFER_RAW := UTL_ENCODE.BASE64_DECODE(UTL_RAW.CAST_TO_RAW(V_BUFFER_VARCHAR));
DBMS_LOB.WRITEAPPEND(V_BLOB,
UTL_RAW.LENGTH(V_BUFFER_RAW),
V_BUFFER_RAW);
V_OFFSET := I*V_BUFFER_SIZE+1;
END LOOP;
RETURN V_BLOB;
END DECODE_BASE64;
/
--图片转BASE64
CREATE OR REPLACE FUNCTION ENCODE_BASE64(P_BLOB IN BLOB)
RETURN CLOB
IS
L_CLOB CLOB;
L_STEP PLS_INTEGER := 12000;
BEGIN
FOR I IN 0 .. TRUNC((DBMS_LOB.GETLENGTH(P_BLOB) - 1 )/L_STEP) LOOP
L_CLOB := L_CLOB || UTL_RAW.CAST_TO_VARCHAR2(UTL_ENCODE.BASE64_ENCODE(DBMS_LOB.SUBSTR(P_BLOB, L_STEP, I * L_STEP + 1)));
END LOOP;
RETURN L_CLOB;
END;
/