www.JohnJustin.tk
TECH A BREAK 2010 PHOTOS
friendsclub academic project
librarymanagementsystem
AirwayReservationsystem
Payroll Calculation Project
Online Banking project
DOWNLOAD 125 JAVA PROJECTS FREE
FREE DOWNLOAD KAROAKE
FOR ADVERTISE HERE
CONTACT 917667566149

Saturday, February 5, 2011

how to resize an image using java

resize jpeg image using java source code
resize bmp,jpg images using java program


/**
* @author JohnJustin
www.johnjustin.tk
*
*/


package ImageResizing;
import java.awt.Graphics2D;
import java.awt.RenderingHints;

import java.awt.AlphaComposite;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.awt.image.PixelGrabber;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.Vector;

import javax.imageio.ImageIO;

public class ImageResize{

/*public static BufferedImage theOriginalImage = null;
public static BufferedImage theNewImage = null;
public static int theOriginalImageWidth = 0,theOriginalImageHeight = 0;
public static int theNewImageWidth = 0, theNewImageHeight = 0;*/

public static boolean stretched = false;

public static void setStretched()
{
stretched = true;
}

public static boolean resizeImage(String source_file,String dest_file,int width,int height)
{
boolean blnSuccess = false;
File infile = null;
File outfile = null;
BufferedImage originalImage = null;
BufferedImage theNewImage = null;
int theOriginalImageWidth = 0, theOriginalImageHeight = 0;
int theNewImageWidth = 0, theNewImageHeight = 0;

try{
infile = new File(source_file);
outfile = new File(dest_file);
originalImage = ImageIO.read(infile);
theOriginalImageWidth = originalImage.getWidth();
theOriginalImageHeight = originalImage.getHeight();
System.out.println("Height >> " + theOriginalImageWidth + " Width >> " + theOriginalImageHeight);
theNewImageWidth = width;
//theNewImageHeight = height;
if(theOriginalImageWidth < theNewImageWidth || theOriginalImageHeight < theNewImageHeight){
setStretched();
}
theNewImageHeight = (theOriginalImageHeight*theNewImageWidth)/theOriginalImageWidth;
System.out.println("The New Image Width >> " + theNewImageWidth + " The New Image Height >> " + theNewImageHeight);
theNewImage = new BufferedImage(theNewImageWidth,theNewImageHeight,BufferedImage.TYPE_INT_RGB);
Graphics2D loGraphics = theNewImage.createGraphics();
loGraphics.setComposite(AlphaComposite.Src);
loGraphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
loGraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
loGraphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
loGraphics.drawImage(originalImage,0,0,theNewImageWidth,theNewImageHeight,null);
loGraphics.dispose();
ImageIO.write(theNewImage,"jpg",outfile);
blnSuccess = true;
}catch(Exception e){
System.out.println("Error While Executing resizeImage function >> " + e.getMessage());
return(blnSuccess);
}

return(blnSuccess);
}

public static void main(String args[])
{
ImageResize resize = new ImageResize();
String source_file = "E:\\john\\img.jpg";
String dest_file = "E:\\john\\imgresize.jpg";
int width = 300;
int height = 300;
boolean flag = resize.resizeImage(source_file,dest_file,width,height);
if(flag){
System.out.println("Resized " + source_file + " to " + dest_file);
}
if(stretched){
System.out.println("The Image Resized Will be in Stretched Format");
}
}

}

0 comments:

JohnJustin
software engineer,blogger,writer,prolife worker and an active JesusYouth
johnjustin@in.com
TECH A BREAK 2010 PHOTOS
friendsclub academic project
librarymanagementsystem
AirwayReservationsystem
Payroll Calculation Project
Online Banking project
DOWNLOAD 125 JAVA PROJECTS FREE
FREE DOWNLOAD KAROAKE
FOR ADVERTISE HERE
CONTACT 917667566149

.
.
.

Subscribe feeds via e-mail
Subscribe in your preferred RSS reader

Subscribe feeds rss Recent Entries

Advertise on this site Sponsored links

Categories

Sponsored Links

.
TECH A BREAK 2010 PHOTOS
friendsclub academic project
librarymanagementsystem
AirwayReservationsystem
Payroll Calculation Project
Online Banking project
DOWNLOAD 125 JAVA PROJECTS FREE
FREE DOWNLOAD KAROAKE
FOR ADVERTISE HERE
CONTACT 917667566149

.

My Photos on flickr

Subscribe feeds rss Recent Comments

Technorati

Technorati
My authority on technorati
Add this blog to your faves