Java-based image file format conversion and linear scaling

Source: Internet
Author: User
Tags trim
Picture file format conversion is the core problem is to solve a variety of picture format encoding and decoding, recommended with Jimi package, can download the package from the http://java.sun.com/products/jimi/and its source code and demo.

Note that Jimi provides decoding support for almost all picture formats, but the code api,gif coding problem for GIF and Tif/tiff formats can be found on the internet to find county-related source code, TIFF like jai:http://java.sun.com/ The products/java-media/jai/index.jsp provides its code,
But I did not use the Jai, temporarily uses the JPEG code to replace.

See Jimi Source code, in the Com.sun.jimi.core is the Jimi core processing API.

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 ' 100 '");
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 property settings, not necessary
// /*
Writer.setoptions (options);
// */
Writer.putimage (dest);
catch (jimiexception je) {
System.err.println ("Error:" + je);
}
}
In the format conversion, do not need to care about the format of the original, as long as the image format to be converted within the Jimi decoding format, you can completely transparent decode process:

ImageProducer image = Jimi.getimageproducer (source);

The actual decoding takes only a line of code, as simple as that.

The simplest part of the coding process requires very little code:

Jimiwriter writer = Jimi.createjimiwriter (dest);
Writer.setsource (image);
Add property settings, not necessary
// /*
Writer.setoptions (options);
// */
Writer.putimage (dest);

The above code completes the picture encoding to the output to the OS to generate the converted file the whole process.

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

The getter and setter methods are provided in com.sun.jimi.core.options.* for each image encoding format, and can be easily set:

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

Format conversion is so simple.

Jimi Unsupported encode format resolution

/**
*
* @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 ();
 
}
Here to use the Animatedgifencoder class, is my search on the Internet, the GIF code provides a realization, although still waiting for good places, but the single format has been enough to turn off:

Animatedgifencoder e = new Animatedgifencoder ();
E.start (dest);
E.addframe (file_in);
E.finish ();

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.