Java-based image file format conversion and linear scaling

Source: Internet
Author: User

The core problem of converting image file formats is to solve the encoding and decoding of various image formats. We recommend that you use the Jimi package to download the package from http://java.sun.com/products/jimi/and its source code and demo.

Note that Jimi provides decoding support for almost all image formats, but provides APIs for encoding GIF and TIF/tiff formats, for GIF encoding problems, you can find the county-related source code on the Internet. Tiff is like Jai: http://java.sun.com/products/java-media/jai/index.jspcoding,
But I didn't use Jai, And I used JPEG encoding for the moment.

Check Jimi source code. In COM. Sun. Jimi. core, it is the core processing API of Jimi.

Public void tojpg (string source, string DEST, int quality ){

If (DEST = NULL | DeST. Trim (). Equals (""))
DeST = source;

If (! DeST. tolowercase (). Trim (). endswith ("jpg ")){
DeST + = ". jpg ";
System. Out. println ("overriding to JPG, output file:" + DEST );
}
If (Quality <0 | quality> 100 | (quality + "") = NULL
| (Quality + ""). Equals ("")){
System. Out. println ("quality must between '0' and '123 '");
System. Out. println ("set to default value: '75 '");
Quality = 75;

}
Try {
Jpgoptions Options = new jpgoptions ();
Options. setquality (quality );
Imageproducer image = Jimi. getimageproducer (source );
Jimiwriter writer = Jimi. createjimiwriter (DEST );
Writer. setsource (image );
// Add attribute settings, not required
///*
Writer. setoptions (options );
//*/
Writer. putimage (DEST );
} Catch (jimiexception je ){
System. Err. println ("error:" + je );
}
}
During format conversion, you do not need to care about the format of the source image. As long as the format of the image to be converted is within the range of Jimi's decoding format, you can perform the decode process completely transparently:

Imageproducer image = Jimi. getimageproducer (source );

The actual decoding only requires line of code, which is so simple.

The simplest coding process requires only a small amount of code:

Jimiwriter writer = Jimi. createjimiwriter (DEST );
Writer. setsource (image );
// Add attribute settings, not required
///*
Writer. setoptions (options );
//*/
Writer. putimage (DEST );

The above code completes the entire process of converting the image code to the output to the OS generation and conversion file.

Writer in the code above. setoptions (options) is used to set the attributes of the output file. The attributes of each format are different. com. sun. jimi. core. options. * provides the getter and setter methods for the attributes of each image encoding format, which can be easily set:

The getter and setter methods are provided for the attributes of each image encoding format in COM. Sun. Jimi. Core. Options. * to facilitate settings:

Jpgoptions Options = new jpgoptions ();
Options. setquality (quality );

Format conversion is that simple.

Solution to the encode format not supported by Jimi

/**
*
* @ Param Source
* @ Param dest
* @ Throws jimiexception
*/
Public void togif (string source, string DEST) throws jimiexception {
If (DEST = NULL | DeST. Trim (). Equals (""))
DeST = source;
// 1: Convert to JPG
If (! DeST. tolowercase (). Trim (). endswith ("jpg ")){
DeST + = ". jpg ";
}
Tojpg (source, DEST, 75 );

Bufferedimage file_in = NULL;
File file = new file (DEST );
Try {
File_in = javax. ImageIO. ImageIO. Read (File );
} Catch (ioexception e ){
E. printstacktrace ();
}

Int end = DeST. lastindexof (".");
File. deleteonexit ();
// Output *. gif
File. renameto (new file (DEST. substring (0, end) + ". GIF "));
// JPG to gif
Animatedgifencoder E = new animatedgifencoder ();
E. Start (DEST );
E. addframe (file_in );
E. Finish ();
 
}
The animatedgifencoder class is used here, which I found on the Internet and provides an implementation for GIF encoding. Although it remains to be improved, it is enough to convert the format of a single file :)

Animatedgifencoder E = new animatedgifencoder ();
E. Start (DEST );
E. addframe (file_in );
E. Finish ();

 

Note that animatedgifencoder cannot correctly recognize images in all formats. Therefore, convert other formats to JPG (the simplest method is to use ImageIO)

Bufferedimage file_in = NULL;
File file = new file (DEST );
Try {
File_in = javax. ImageIO. ImageIO. Read (File );
} Catch (ioexception e ){
E. printstacktrace ();
}

In this way, it is OK to directly put it in bufferedimage.

E. addframe (file_in );

The actual encoding process is completed in the preceding sentence.

Int end = DeST. lastindexof (".");
File. deleteonexit ();
// Output *. gif
File. renameto (new file (DEST. substring (0, end) + ". GIF "));

Finally, do not forget to use the above statements to eliminate the evidence before completion :)

Of course, this method is actually not good. The most thorough method is to modify the animatedgifencoder, however, renhe needs to be honest, and he is suffering from the troubles. Therefore, qiaosi relies on the blowing of the thumb of zhunzun, And the row of the scorpion, the bud shoots, and the yunhaolong? BR>
After the format conversion is completed, the scaling function is not a problem. The following code simultaneously implements format rotation and linear scaling:

/**
*
* @ Param img
* @ Param dest
* @ Throws jimiexception
*/
Public void totif (image IMG, string DEST) throws jimiexception {
If (! DeST. tolowercase (). Trim (). endswith ("TIF ")){
DeST + = ". tif ";
System. Out. println ("overriding to Tif, output file:" + DEST );
}
DeST = DeST. substring (0, dest. lastindexof (".") + ". jpg ";
Jimiwriter writer = Jimi. createjimiwriter (DEST );
Writer. setsource (IMG );
DeST = DeST. substring (0, dest. lastindexof (".") + ". tif ";
Writer. putimage (DEST );
}

/**
* Linearly changing the image size (the image format can be changed at the same time)
*
* @ Param Source
* Full path of the source file
* @ Param DESC
* Complete path of the target file
* @ Param INS
* Zoom in/out ratio
* @ Throws jimiexception
* @ Throws ioexception
*/
Public void changedimension (string source, string DESC, double INS)
Throws jimiexception, ioexception {
String temp = DESC;
File _ file = NULL;
If (DESC = NULL | DESC. Trim (). Equals (""))
Desc = source;
If (! Desc. tolowercase (). Trim (). endswith ("jpg ")){
Temp = DESC. Trim () + ". jpg ";
}
This. tojpg (source, temp, 75 );
_ File = new file (temp); // read the file

Image src = javax. ImageIO. ImageIO. Read (_ file); // construct an image object
Double Wideth = (double) SRC. getwidth (null); // obtain the source image width.
Double Height = (double) SRC. getheight (null); // obtain the source image length.
Int iwideth = (INT) (Wideth * INS );
Int iheight = (INT) (height * INS );
Bufferedimage tag = new bufferedimage (iwideth, iheight,
Bufferedimage. type_int_rgb );
Tag. getgraphics (). drawimage (SRC, 0, 0, iwideth, iheight, null); // draw the reduced Graph

If (! Temp. Trim (). Equals (DESC ))
_ File. deleteonexit ();

If (DESC. tolowercase (). Trim (). endswith ("GIF ")){
Animatedgifencoder E = new animatedgifencoder ();
E. Start (DESC );
E. addframe (TAG );
E. Finish ();
} Else if (DESC. tolowercase (). Trim (). endswith ("TIF") | DESC. tolowercase (). Trim (). endswith ("tiff ")){
This. totif (TAG, DESC );
} Else {
Jimiwriter writer = Jimi. createjimiwriter (DESC );
Writer. setsource (TAG );
Writer. putimage (DESC );
}
}

The parameter type in the bufferedimage constructor is int, so the above Code has a slight deviation when changing the image size, but it is okay to simply demonstrate it.

In Jimi's example, you can find a lot of demos for single image processing functions. If you have time to study it, there will be a lot of GAINS.

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.