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:
Post a Comment