ImageIcon image processing test [some special processing methods], imageicon Image Processing

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.