The return of the Keep the Initial Image Quality
By francois degrelle on Friday, March 27 2015, 15:15 - PJC - Javabean - Permalink
Purpose
Here is a Java Bean that allows reading, displaying with scaling and writing images.
(See the last evolution of this solution here)
This Bean allows scaling images to fit the target item size, and includes a FileChooser with image preview dialog.
This
JavaBean connects to the database through the JDBC driver to read and
write the images, and does not need the Webutil library to pick up the
image files from the JAR file, the client machine files or the Internet.
The Java code
HandleImage.java LectureBlob.java GetImageFileName.java
The implementation class of the Bean Item
oracle.forms.fd.HandleImage
The methods you can call
Set the connection string
Set_Custom_Property('BLOCK.ITEM',1,'SETCONNthe_connection_string');
e.g. :
Set_Custom_Property( 'BL.BEAN', 1, 'SETCONN', 'jdbc:oracle:thin:@my-machine:1521:XE' ) ;
(Provide the complete jdbc:oracle:thin:... syntax)
Set the username
Set_Custom_Property('BLOCK.ITEM',1SETUSER','username');
Set the password
Set_Custom_Property('BLOCK.ITEM',1SETPWD','password');
Set the database image table information
Set_Custom_Property('BLOCK.ITEM',1SETTABLEINFO','p1,p2');
p1 is the table name that holds the image Blob column
p2 is the Blob column name that holds the image
e.g.:
Set_Custom_Property( 'BLZ.BEAN', 1, 'SETTABLEINFO', 'PHOTOS,IMAGE' ) ;
The instruction above indicates that the PHOTOS table handle images in the IMAGE Blob column..
Read an image from a file
Set_Custom_Property('BLOCK.ITEM',1READIMGFILE','the_complete_filename');
e.g.:
Set_Custom_Property( 'BLZ.BEAN', 1, 'READIMGFILE', 'D:/image.jpg' ) ;
Read an image from the database table
Set_Custom_Property('BLOCK.ITEM',1,'READIMGBASE,'where_clause');
e.g.:
Set_Custom_Property( 'BLZ.BEAN', 1, 'READIMGBASE', 'IDENTIFIANT=20' ) ;
where_clause is the SQL clause to identify a unique row to update in the table. The above instruction will be interpreted as :
Select IMAGE From PHOTOS Where IDENTIFIANT=20
Put this method into the When-New-Record-Instance block-level trigger
Write an image to the database table
Set_Custom_Property('BLOCK.ITEM',1,'WRITEIMGFILE','where_clause');
e.g.:
Set_Custom_Property( 'BLZ.BEAN', 1, 'WRITEIMGBASE', 'IDENTIFIANT=20' ) ;
where_clause is the SQL clause to identify a unique row to update in the table. The above instruction will be interpreted as :
Update PHOTOS Set IMAGE = ? Where IDENTIFIANT=20
Put this method into the Post-Insert and Post-Update block-level triggers
Clear the image
Set_Custom_Property( 'BL.BEAN', 1, 'CLEAR' , '' ) ;
This function has to be called in a When-New-Record-Instance trigger if the record does not exist:
When-New-Record-Instance trigger:
If :PHOTOS.IDENTIFIANT is not null Then
Set_Custom_Property( 'BLZ.BEAN', 1, 'READIMGBASE', 'IDENTIFIANT=12' ) ;
Else
Set_Custom_Property( 'BLZ.BEAN', 1, 'CLEAR', '' ) ;
End if ;
:BLZ.IMG_SIZE := Get_Custom_Property( 'BLZ.BEAN', 1, 'GETSIZE' ) ;
Set the display image scalling
This function concerns only the image displayed. The image stored with the WRITEIMGBASE method will be the initial image.
Set_Custom_Property( 'BL.BEAN', 1, 'SCALE_IMAGE' , 'Width=x,Height=y' ) ;
Both Width and Height parameters must be transmitted
x and y can take the following values:
Any value greater than 0 that express an absolute size in pixels
the special value: FIT that extend to fit the exact size of the image panel' size
-1 that to keep the corresponding aspect ratio
0 that indicates no scale at all
Examples:
-- Scale the image to fit horizontally and vertically the image item --
Set_Custom_Property( 'BLZ.BEAN', 1, 'SCALE_IMAGE', 'Width=FIT,Height=FIT' ) ;
-- Scale the image to 200x100 pixels --
Set_Custom_Property( 'BLZ.BEAN', 1, 'SCALE_IMAGE', 'Width=200,Height=100' ) ;
-- Scale the image to 200 horizontal pixels and keep vertical aspect ratio --
Set_Custom_Property( 'BLZ.BEAN', 1, 'SCALE_IMAGE', 'Width=200,Height=-1' ) ;
Display/hide the image panel' scrollbars
Set_Custom_Property('BLOCK.ITEM',1SETSCROLL','true|false');
e.g.:
Set_Custom_Property( 'BLZ.BEAN', 1, 'SETSCROLL', 'true' ) ;
Set the JFileChooser title and starting directory
Set_Custom_Property('BLOCK.ITEM',1SET_FILECHOOSER_TITLE','title[,directory]');
e.g.:
Set_Custom_Property( 'BLZ.BEAN', 1, 'SET_FILECHOOSER_TITLE', 'Select a file,D:/' ) ;
Set the JFileChooser Look and Feel
Set_Custom_Property('BLOCK.ITEM',1SET_FILECHOOSER_LAF','SYSTEM|other_value');
e.g.:
-- use the current O.S. L&F --
Set_Custom_Property( 'BLZ.BEAN', 1, 'SET_FILECHOOSER_LAF', 'SYSTEM' ) ;
-- use the Metal L&F --
Set_Custom_Property( 'BLZ.BEAN', 1, 'SET_FILECHOOSER_LAF', 'JAVA' ) ;
Set the log mode to output the Bean messages
Set_Custom_Property( 'BLZ.BEAN', 1, 'SETLOG' , 'true|false' ) ;
In the sample dialog provided, here is the code used in the When-New-Form-Instance trigger:
PROCEDURE InitForm IS
BEGIN
-- switch the log to true --
Set_Custom_Property( 'BLZ.BEAN', 1, 'SETLOG', 'true' ) ;
-- set the baen image bckground color --
Set_Custom_Property( 'BLZ.BEAN', 1, 'SETBG', '255,255,255' ) ;
-- set the JDBC connection information --
Set_Custom_Property( 'BLZ.BEAN', 1, 'SETCONN', 'jdbc:oracle:thin:@my-machine:1521:XE' ) ;
Set_Custom_Property( 'BLZ.BEAN', 1, 'SETUSER', 'tutoforms' ) ;
Set_Custom_Property( 'BLZ.BEAN', 1, 'SETPWD', 'tutoforms' ) ;
-- set the image table and column name --
Set_Custom_Property( 'BLZ.BEAN', 1, 'SETTABLEINFO', 'PHOTOS,PHOTO_JAVA,IDENTIFIANT' ) ;
-- Set the JFileChooser title --
Set_Custom_Property( 'BLZ.BEAN', 1, 'SET_FILECHOOSER_TITLE', 'Select an image file' ) ;
-- Set the JFileChooser L&F --
Set_Custom_Property( 'BLZ.BEAN', 1, 'SET_FILECHOOSER_LAF', 'SYSTEM' ) ;
-- scalling properties --
Set_Custom_Property( 'BLZ.BEAN', 1, 'SCALE_IMAGE' , 'Width=FIT,Height=FIT' ) ;
-- display scrollbars policy --
Set_Custom_Property( 'BLZ.BEAN', 1, 'SETSCROLL', 'false' ) ;
END;
Here is the description of the database table used:
CREATE TABLE PHOTOS
(
"IDENTIFIANT" NUMBER(5,0) NOT NULL ENABLE,
"NOM" VARCHAR2(50 BYTE),
"PHOTO" BLOB,
"PHOTO_JAVA" BLOB,
CONSTRAINT "PHOTO_PK" PRIMARY KEY ("IDENTIFIANT") ENABLE
)
/
The properties you can get from the JavaBean
Get the image size
Varchar2 := Get_Custom_Property( 'BL.BEAN', 1, 'GETSIZE' ) ;
The format returned (width,height) is like the following : 50,120
Get the selected filename (JFileChooser)
Varchar2 := Get_Custom_Property( 'BL.BEAN', 1, 'GET_FILE_NAME' ) ;
This method, both open the JFileChooser dialog box, then returns the selected file.
Accessing to a remote database
Because
the JavaBean is executed within an applet, it is not possible to
connect to a remote database without a little adaptation:
- first, the jar file must be signed.
- second : you have to update the java.policy file stored in the Jinitiator directory
For example, to access to the database located on the machine-name server on the port 1524, add the following lines to the java.policy file:
C:/Program Files/Oracle/JInitiator 1.3.1.xx/lib/security/java.policy file
permission java.net.SocketPermission "machine-name:1524-", "accept,connect,listen,resolve";
permission java.security.AllPermission;
The sample dialog
. Download the handleimage2.zip file
. Unzip the file
. run the create_table.sql under the Oracle user you want to test (It creates the sample image table).
. copy the handleimage2.jar file in the <ORACLE_HOME>/forms/java directory
. Edit your /forms/server/formsweb.cfg file to add both handleimage2.jar and classes12.jar
(the classes12.jar is located in the <DEV_HOME>/jdbc/lib
directory. add a copy of this file in the <DEV_HOME>/forms/java
directory).
. Open the HANDLEIMAGE2.fmb module (Oracle Forms 10.1.2)
. Compile all and run the module