Oracle Visual Information Retrieval Java Classes User's Guide and Reference Release 8.1.7 Part Number A85333-01 |
|
This chapter provides a full-length example of user-defined classes using Visual Information Retrieval Java Classes. A sample SQL script that demonstrates how to set up a schema on your database server is also included.
This code will not necessarily match the code shipped as VirExample.java with the Java Classes installation. If you want to run an example on your system, use the files provided with the Java Classes installation; do not attempt to compile and run the code presented in this chapter.
The Visual Information Retrieval example contains user-defined methods that use SQL, JDBC, and Visual Information Retrieval Java Classes APIs to perform the following operations:
Example 2-1 shows the contents of VirExample.sql.
set echo on -- Please change system password. connect system/manager drop user VIRUSER cascade; [1] grant connect,resource to VIRUSER identified by VIRUSER; -- Replace 'C:\Oracle\Ora81' with your Oracle home directory [2] create or replace directory ORDVIRDIR as 'C:\Oracle\Ora81\ord\vir\demo'; grant read on directory ORDVIRDIR to public with grant option; [3] connect VIRUSER/VIRUSER; [4] create table ordvirtab(id number, image ORDSYS.ORDVir, image2 ORDSYS.ORDVir); -- Note - the ORDVir.init method was added in 8.1.7. If you are running -- against an older release of Visual Information Retrieval and Oracle, -- you will have to modify the following INSERT statements to use the -- OrdVir default constructor. -- [5] insert into ordvirtab values (1, ORDSYS.ORDVir.init( ), ORDSYS.ORDVir.init( )); insert into ordvirtab values (2, ORDSYS.ORDVir.init( ), ORDSYS.ORDVir.init( )); insert into ordvirtab values (3, ORDSYS.ORDVir.init( ), ORDSYS.ORDVir.init( )); insert into ordvirtab values (4, ORDSYS.ORDVir.init( ), ORDSYS.ORDVir.init( )); [5] insert into ordvirtab values (5, ORDSYS.ORDVir.init('file','ORDVIRDIR','stripes1.bmp'), ORDSYS.ORDVir.init('file','ORDVIRDIR','stripes2.bmp')); insert into ordvirtab values (6, ORDSYS.ORDVir.init('file','ORDVIRDIR','lcolor1.bmp'), ORDSYS.ORDVir.init('file','ORDVIRDIR','lcolor2.bmp')); commit; set echo off exit;
The SQL statements in VirExample.sql perform the following operations:
The ORDVir.init method was added in release 8.1.7. If you are running against a previous release of Visual Information Retrieval and Oracle8i, you will have to modify the INSERT statements in steps 5 and 6 to use the ORDVir default constructor.
See Oracle8i Visual Information Retrieval User's Guide and Reference for more information on the init method.
Section 2.2.1 through Section 2.2.9 show the methods contained in the VirExample.java sample file.
Example 2-2 shows the main( ) method.
public static void main (String args[ ]){ byte[ ] ctx = new byte[4000]; OracleConnection con = null; try{ VirExample ve = new VirExample( ); [1] con = ve.connect( ); //Only include the following line if you are running //an Oracle 8.1.7 database or later. //If you are running a database server prior to 8.1.7, //the call will fail. [2] OrdMediaUtil.virCompatibilityInit(con); [3] ve.setPropertiesExample(con); ve.displayPropertiesExample(con); ve.fileBasedExample(con); ve.streamBasedExample(con); ve.byteArrayBasedExample(con); ve.processExample(con); ve.similarExample(con); ve.scoreExample(con); [4] con.commit( ); [5] con.close( ); System.out.println("Done."); } [6] catch (Exception e){ try{ System.out.println("Exception : " + e); con.close( ); } catch(Exception ex){ System.out.println("Close Connection Exception : " + ex); } } }
The code in the main( ) method performs the following operations:
Section 2.2.2 through Section 2.2.9 will provide information on the methods called from the main( ) method.
Example 2-3 shows a user-defined method named connect( ), which makes a connection from the application to the database.
public OracleConnection connect( ) throws Exception{ String connectString; [1] Class.forName ("oracle.jdbc.driver.OracleDriver"); [2] connectString = "jdbc:oracle:oci8:@"; [3] OracleConnection con = (OracleConnection)DriverManager.getConnection (connectString,"VIRUSER","VIRUSER"); [4] con.setAutoCommit(false); return con; }
The connect( ) method performs the following operations:
Example 2-4 shows a user-defined method named setPropertiesExample( ) that sets the properties in the application object.
public void setPropertiesExample(OracleConnection con){ try{ int index = 0; [1] Statement s = con.createStatement( ); [2] OracleResultSet rs = (OracleResultSet)s.executeQuery ("select * from ordvirtab where id = 5 for update"); [3] while(rs.next( )){ [4] index = rs.getInt(1); [5] OrdVir imgObj = (OrdVir)rs.getCustomDatum (2, OrdVir.getFactory( )); [6] imgObj.setProperties( ); System.out.println("set Properties called"); [7] if(imgObj.checkProperties( )){ System.out.println("checkProperties called"); System.out.println("setProperties successful"); System.out.println("checkProperties successful"); System.out.println("successful"); } else{ System.out.println("checkProperties called"); System.out.println("setProperties not successful"); System.out.println("checkProperties successful"); } [8] OraclePreparedStatement stmt1 = (OraclePreparedStatement) con.prepareCall("update ordvirtab set image = ? where id = " + index); stmt1.setCustomDatum(1,imgObj); stmt1.execute( ); stmt1.close( ) ; } rs.close( ); s.close( ); } [9] catch(Exception e){ System.out.println("exception raised " + e); } }
The setPropertiesExample( ) method performs the following operations:
Example 2-5 shows a user-defined method named displayPropertiesExample( ) that prints the attributes of the application object to the screen.
public void displayPropertiesExample(OracleConnection con){ try{ int index = 0; [1] Statement s = con.createStatement( ); OracleResultSet rs = (OracleResultSet)s.executeQuery( "select * from ordvirtab where id = 5 "); while(rs.next( )){ index = rs.getInt(1); OrdVir imgObj = (OrdVir) rs.getCustomDatum(2, OrdVir.getFactory( )); [2] System.out.println("format : " + imgObj.getFormat( )); System.out.println("mimeType: " + imgObj.getMimeType( )); System.out.println("height: " + imgObj.getHeight( )); System.out.println("width: " + imgObj.getWidth( )); System.out.println("contentLength: " + imgObj.getContentLength( )); System.out.println("contentFormat: " + imgObj.getContentFormat( )); System.out.println("compressionFormat: " + imgObj.getCompressionFormat( )); System.out.println("source type: " + imgObj.getSourceType( )); System.out.println("source loc: " + imgObj.getSourceLocation( )); System.out.println("source name: " + imgObj.getSourceName( )); System.out.println("source : " + imgObj.getSource( )); [3] try{ String attrString = getAllAttributesAsString(imgObj); System.out.println(attrString); } [4] catch (Exception e){ System.out.println("Exception raised in getAllAttributesAsString:"); } System.out.println("successful"); } } [5] catch(Exception e) { System.out.println("exception raised " + e); } }
The displayPropertiesExample( ) method performs the following operations:
Example 2-6 shows a user-defined method named getAllAttributesAsString( ) that creates a String object that contains the values of the application object attributes.
public String getAllAttributesAsString (OrdVir imgObj) throws Exception{ [1] String attStr = imgObj.getSource( ) + " mimeType = " + imgObj.getMimeType( ) + ", fileFormat = " + imgObj.getFormat( ) + ", height = " + imgObj.getHeight( ) + ", width = " + imgObj.getWidth( ) + ", contentLength = " + imgObj.getContentLength( ) + ", contentFormat = " + imgObj.getContentFormat( ) + ", compressionFormat = " + imgObj.getCompressionFormat( ); [2] return attStr; }
The getAllAttributesAsString( ) method performs the following operations:
Example 2-7 shows a user-defined method named fileBasedExample( ) that uses the loadDataFromFile( ) method to load media data into the application object.
public void fileBasedExample(OracleConnection con){ try{ int index = 0; [1] Statement s = con.createStatement( ); OracleResultSet rs = (OracleResultSet)s.executeQuery( "select * from ordvirtab where id = 2 for update "); while(rs.next( )){ index = rs.getInt(1); OrdVir imgObj = (OrdVir) rs.getCustomDatum(2, OrdVir.getFactory( )); [2] imgObj.loadDataFromFile("virdemo1.dat"); [3] imgObj.setProperties( ); [4] imgObj.getDataInFile("fileexample.dat"); [5] OraclePreparedStatement stmt1 = (OraclePreparedStatement) con.prepareCall("update ordvirtab set image = ? where id = " + index); stmt1.setCustomDatum(1,imgObj); stmt1.execute( ); stmt1.close( ) ; } System.out.println("successful"); } [6] catch(Exception e){ System.out.println("exception raised " + e); } }
The fileBasedExample( ) method performs the following operation:
Example 2-8 shows a user-defined method named streamBasedExample( ), which uses the loadDataFromInputStream( ) method to load media data into the application object.
public void streamBasedExample(OracleConnection con){ try{ int index = 0; [1] Statement s = con.createStatement( ); OracleResultSet rs = (OracleResultSet)s.executeQuery( "select * from ORDVIRTAB where id = 3 for update "); while(rs.next( )){ index = rs.getInt(1); OrdVir imgObj = (OrdVir) rs.getCustomDatum(2, OrdVir.getFactory( )); [2] FileInputStream fStream = new FileInputStream ("virdemo1.dat"); [3] imgObj.loadDataFromInputStream(fStream); [4] fStream.close( ); [5] imgObj.setProperties( ); [6] InputStream inpStream = imgObj.getDataInStream( ); int length = 32300; byte[ ] tempBuffer = new byte[length]; [7] int numRead = inpStream.read(tempBuffer,0,length); FileOutputStream outStream=null; try{ [8] outStream = new FileOutputStream ("streamexample.dat"); [9] while(numRead != -1){ [10] if (numRead < length){ length = numRead; outStream.write(tempBuffer,0,length); break; } [11] else outStream.write(tempBuffer,0,length); [12] numRead = inpStream.read(tempBuffer,0, length); } } [13] finally{ if (outStream != null) outStream.close( ); inpStream.close( ); } [14] OraclePreparedStatement stmt1 = (OraclePreparedStatement) con.prepareCall("update ordvirtab set image = ? where id = " + index); stmt1.setCustomDatum(1,imgObj); stmt1.execute( ); stmt1.close( ) ; } System.out.println("successful"); } [15] catch(Exception e){ System.out.println("exception raised " + e); } }
The streamBasedExample( ) method performs the following operations:
Example 2-9 shows a user-defined method named byteArrayBasedExample( ) that uses the loadDataFromByteArray( ) method to load media data into the application object.
public void byteArrayBasedExample(OracleConnection con){ try{ int index = 0; [1] Statement s = con.createStatement( ); OracleResultSet rs = (OracleResultSet)s.executeQuery ("select * from ORDVIRTAB where id = 4 for update "); while(rs.next( )){ index = rs.getInt(1); OrdVir imgObj = (OrdVir) rs.getCustomDatum(2, OrdVir.getFactory( )); [2] File ff = new File("virdemo1.dat"); int fileLength = (int) ff.length( ); byte[ ] data = new byte[fileLength]; [3] FileInputStream fStream = new FileInputStream("virdemo1.dat"); [4] fStream.read(data,0,fileLength); [5] imgObj.loadDataFromByteArray(data); [6] fStream.close( ); [7] imgObj.setProperties( ); [8] byte[ ] resArr = imgObj.getDataInByteArray( ); [9] System.out.println("byte array length : " + resArr.length); [10] OraclePreparedStatement stmt1 = (OraclePreparedStatement) con.prepareCall("update ordvirtab set image = ? where id = " + index); stmt1.setCustomDatum(1,imgObj); stmt1.execute( ); stmt1.close( ) ; } System.out.println("successful"); } [11] catch(Exception e){ System.out.println("exception raised " + e); } }
The byteArrayBasedExample( ) method performs the following operations:
Example 2-10 shows a user-defined method named processExample( ) that uses the process( ) and processCopy( ) methods to manipulate the media data in the application object.
public void processExample(OracleConnection con){ try{ int index1 = 0; [1] Statement s1 = con.createStatement( ); OracleResultSet rs1 = (OracleResultSet)s1.executeQuery ("select * from ORDVIRTAB where id = 2 for update "); while(rs1.next( )){ index1 = rs1.getInt(1); OrdVir imgObj = (OrdVir) rs1.getCustomDatum(2, OrdVir.getFactory( )); [2] OrdVir imgObj2 = (OrdVir) rs1.getCustomDatum(3, OrdVir.getFactory( )); try{ [3] imgObj.processCopy("maxScale=32 32, fileFormat= GIFF", imgObj2); [4] imgObj.process("fileFormat=JFIF"); [5] System.out.println(getAllAttributesAsString (imgObj)); [6] System.out.println(getAllAttributesAsString(imgObj2)); } [7] catch (Exception e){ System.out.println("Exception raised in process" + e ); } [8] OraclePreparedStatement stmt1 = (OraclePreparedStatement) con.prepareCall("update ordvirtab set image = ?, image2 = ? where id = " + index1); stmt1.setCustomDatum(1,imgObj); stmt1.setCustomDatum(2,imgObj2); stmt1.execute( ); stmt1.close( ) ; } rs1.close( ); s1.close( ); } [9] catch(Exception e){ System.out.println("Exception raised: " + e); } System.out.println("successful"); }
The processExample( ) method performs the following operations:
Example 2-11 shows a user-defined method named similarExample( ) that uses the analyze( ) and similar( ) methods to compare two application objects.
public void similarExample(OracleConnection con){ try{ int index1 = 0; [1] Statement s1 = con.createStatement( ); OracleResultSet rs1 = (OracleResultSet)s1.executeQuery ("select * from ordvirtab where id = 5 for update "); while(rs1.next( )){ index1 = rs1.getInt(1); OrdVir imgObj1 = (OrdVir) rs1.getCustomDatum (2, OrdVir.getFactory( )); [2] OrdVir imgObj2 = (OrdVir) rs1.getCustomDatum (3, OrdVir.getFactory( )); [3] try{ imgObj1.analyze( ); imgObj2.analyze( ); } [4] catch (Exception e){ System.out.println("Exception raised in analyze" + e ); } [5] if (1 == imgObj1.similar(imgObj2.getSignature( ), "texture=50,structure=50", 5)){ System.out.println("imgObj1 is similar to imgObj2"); } else{ System.out.println("imgObj1 is not similar to imgObj2"); } [6] OraclePreparedStatement stmt1 = (OraclePreparedStatement) con.prepareCall("update ordvirtab set image = ?, image2 = ? where id = " + index1); stmt1.setCustomDatum(1,imgObj1); stmt1.setCustomDatum(2,imgObj2); stmt1.execute( ); stmt1.close( ) ; } rs1.close( ); s1.close( ); } [7] catch(Exception e){ System.out.println("Exception raised: " + e); } System.out.println("successful"); }
The similarExample( ) method performs the following operations:
Example 2-12 shows a user-defined method named scoreExample( ) that uses the analyze( ) and score( ) methods to create a score for an application object.
public void scoreExample(OracleConnection con){ try{ int index1 = 0; [1] Statement s1 = con.createStatement( ); OracleResultSet rs1 = (OracleResultSet)s1.executeQuery ("select * from ordvirtab where id = 6 for update "); while(rs1.next( )){ index1 = rs1.getInt(1); OrdVir imgObj1 = (OrdVir) rs1.getCustomDatum (2, OrdVir.getFactory( )); [2] OrdVir imgObj2 = (OrdVir) rs1.getCustomDatum (3, OrdVir.getFactory( )); [3] try{ imgObj1.analyze( ); imgObj2.analyze( ); } [4] catch (Exception e){ System.out.println("Exception raised in analyze" + e ); } [5] System.out.println("Score = " + imgObj1.score (imgObj2.getSignature( ),"globalColor=100")); [6] OraclePreparedStatement stmt1 = (OraclePreparedStatement) con.prepareCall("update ordvirtab set image = ?, image2 = ? where id = " + index1); stmt1.setCustomDatum(1,imgObj1); stmt1.setCustomDatum(2,imgObj2); stmt1.execute( ); stmt1.close( ) ; } rs1.close( ); s1.close( ); } [7] catch(Exception e){ System.out.println("Exception raised: " + e); } System.out.println("successful"); }
The similarExample( ) method performs the following operations:
|
![]() Copyright © 1996-2000, Oracle Corporation. All Rights Reserved. |
|