ImageIcon image processing test [some special processing methods], imageicon Image Processing
********************** ********/
Package cn. jason. ios. images;
Import java. awt. FileDialog;
Import java. awt. Image;
Import java. io. File;
Import java. io. FileInputStream;
Import java. io. FileNotFoundException;
Import java. io. IOException;
Import java.net. URL;
Import javax. swing. ImageIcon;
Import javax. swing. JFrame;
Import javax. swing. JLabel;
/**
*
* ImageIcon image processing test [some special processing methods]
*
*/
Public class MyImageIcon extends JFrame {
/**
* Open the local Manager dialog box and select an image file.
* @ Return returns the absolute path of the image.
*/
Private static String getLocalImageAbsolutePath (){
JFrame frame = new JFrame ();
// Create dialog box
FileDialog dialog = new FileDialog (frame );
// Set the dimension
Dialog. setBounds (300,100,500,450 );
// Open the dialog box
Dialog. setVisible (true );
// Obtain the absolute returned path
StringBuilder absolutePath = new StringBuilder ();
AbsolutePath. append (dialog. getDirectory () = "null "? "Null": dialog. getDirectory ());
AbsolutePath. append (dialog. getFile () = "null "? "Null": dialog. getFile ());
// Filter information
If (absolutePath. toString (). contains ("null ")){
Return "NO-Path ";
}
Return absolutePath. toString ();
}
/**
* Terminate the program
*/
Private static void exitPrograming (){
System. exit (0 );
}
/**
* Obtain image binary data: [1024*1024*10]: 10 M
* @ Param imagePath: Absolute image address
* @ Return returns binary data
*/
Private static byte [] getImageBytes (String imagePath ){
// Binary object
Byte [] B = new byte [1024*1024*10];
// Create an image file
File imageFile = new File (imagePath );
// Image Stream Object
Try {
FileInputStream FCM = new FileInputStream (imageFile );
// Write
FS. read (B );
} Catch (FileNotFoundException e ){
E. printStackTrace ();
} Catch (IOException e ){
E. printStackTrace ();
}
Return B! = Null? B: null;
}
/**
* Obtain the ImageIcon object from the binary data of the image.
* @ Param B binary data
* @ ReturnImageIcon object
*/
Private static ImageIcon getImageIcon (byte [] B ){
Return new ImageIcon (B );
}
/**
* Obtain the ImageIcon object through the image address
* @ Param filename: Image address
* @ ReturnImageIcon object
*/
Private static ImageIcon getImageIcon (String filename ){
Return new ImageIcon (filename );
}
/**
* Obtain an ImageIcon object from an image address object
* @ Param locationURL address object
* @ ReturnImageIcon object
*/
Private static ImageIcon getImageIcon (URL location ){
Return new ImageIcon (location );
}
/**
* Obtain an ImageIcon object from an image object
* @ Param imageImage address object
* @ ReturnImageIcon object
*/
Private static ImageIcon getImageIcon (Image image ){
Return new ImageIcon (image );
}
/**
* Adjust the ImageIcon size.
* @ Param iconIcon object
* @ Param width the Adjusted width
* @ Param height the adjusted height
* @ Return
*/
Private static ImageIcon imageSizeCorrect (ImageIcon icon,
Int width,
Int height ){
Image images = icon. getImage ();
Images = images. getScaledInstance (width, height, Image. SCALE_DEFAULT );
Return new ImageIcon (images );
}
/**
* Program entry
* @ Param params
*/
Public static void main (String [] params ){
String path = getLocalImageAbsolutePath ();
System. out. println ("open address:" + path );
MyImageIcon testImages = new MyImageIcon ();
TestImages. setBounds (200,100,500,500 );
JLabel jLabel = new JLabel ();
JLabel. setBounds (100,100,300,300 );
JLabel. setIcon (imageSizeCorrect (MyImageIcon. getImageIcon (MyImageIcon. getImageBytes (path), 300,300 ));
TestImages. add (jLabel );
TestImages. setVisible (true );
// End the program
MyImageIcon. exitPrograming ();
}
}