After Java SDK 1.4, the image processing function has been greatly improved. Its ImageIO class provides near-
Silly operation. Now let's see what surprises they bring us.
Core code for reading and displaying images:
Public void showpic (){
File F = new file ("E:/tt.jpg ");
Bufferedimage image = NULL;
Try {
Image = ImageIO. Read (f );
} Catch (ioexception ex ){
Logger. getlogger (javaimage. Class. getname (). Log (level. Severe, null, ex );
}
Setcontentpane (New jlabel (New imageicon (image )));
Validate ();
}
The result is as follows:
Writing a file is also quite simple.
Use ImageIO. Write (bufferedimage image, string format, file F.
Of course, if you still need other processing functions, such as adding words to an image or deformation an image, you need other java2d knowledge, which is beyond our scope.
So how can you implement GIF animation in the graphic area and separate all the images?
The core code is as follows:
Private void jbutton1actionreceivmed (Java. AWT. event. actionevent EVT ){
Jfilechooser chooser = new jfilechooser ();
// Chooser. setcurrentdirectory (new file ("."));
Final list = new arraylist ();
List. Add ("jpg ");
List. Add ("PNG ");
List. Add ("GIF ");
Chooser. setfilefilter (New javax. Swing. filechooser. filefilter (){
@ Override
Public Boolean accept (file F ){
If (F. isdirectory () return true;
String name = f. getname ();
Int P = Name. lastindexof ('.');
If (P =-1) return false;
String suffix = Name. substring (p + 1). tolowercase ();
Return list. Contains (suffix );
}
@ Override
Public String getdescription (){
Return "image files ";
}
});
Int r = chooser. showopendialog (this );
If (R! = Jfilechooser. approve_option) return;
File F = chooser. getselectedfile ();
String name = f. getname ();
String suffix = Name. substring (name. lastindexof ('.') + 1 );
Iterator iter = ImageIO. getimagereadersbyformatname (suffix );
Imagereader reader = NULL;
Box box = Box. createverticalbox ();
While (ITER. hasnext ())
{
Reader = (imagereader) ITER. Next ();
}
Imageinputstream imagein = NULL;
Bufferedimage [] Bi = NULL;
Try {
Imagein = ImageIO. createimageinputstream (f );
Reader. setinput (imagein );
Int COUNT = reader. getnumimages (true );
Bi = new bufferedimage [count];
For (INT I = 0; I <count; I ++)
{
Bi [I] = reader. Read (I );
Box. Add (New jlabel (New imageicon (Bi [I]);
}
} Catch (ioexception ex ){
Logger. getlogger (javaimage. Class. getname (). Log (level. Severe, null, ex );
}
Setcontentpane (New jscrollpane (box ));
Validate ();
}
The result is as follows:
Let's explain some of the Code.
Some of the preceding code is selected by filtering jfilechooser files. We will not analyze this item for the moment. We
Take a look at the following.
Iterator iter = ImageIO. getimagereadersbyformatname (suffix );
Get the corresponding imagereader through the file format. Of course, the returned value here is an iterator.
While (ITER. hasnext ())
{
Reader = (imagereader) ITER. Next ();
}
Obtain the corresponding imagereader through traversal.
The following code mainly obtains the input stream of the corresponding image.
Int COUNT = reader. getnumimages (true); returns the number of images contained in the GIF image.
Bi [I] = reader. Read (I); obtain the corresponding internal image
--------------
Welcome to the launch of the new website
Website Transaction CenterYou can purchase or sell your website here.
Website information publishing centerVarious transaction information is released here. At the same time
Free use of some software (with source code ).
Website Blog systemYou can register your own blog here. An unlimited number of blogs under one account
Contact info:
Support@websiteempire.cn
QQ: 563828566
MSN: zhuhailin123@hotmail.com