Tuesday, 2 July 2013

Resize , Merge Images Using Java

package custom;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class MergeImages {
 private static final int COMBINE_IMG_WIDTH = 180;
 private static final int COMBINE_IMG_HEIGHT = 90;
 
 private static final int POTRAIT_IMG_WIDTH = 70;
 private static final int POTRAIT_IMG_HEIGHT = 90;
 
 
 
public static void main(String[] args) throws IOException {
 
 
    File basePath = new File("f:/");
 // load source images
 BufferedImage background = ImageIO.read(new File(basePath, "back.png"));
 BufferedImage overlay = ImageIO.read(new File(basePath, "land.png"));

 int imageHeight = overlay.getHeight();
 int imageWidth =  overlay.getWidth();
 System.out.println("height: "+imageHeight+" : "+"width: "+imageWidth);
 if(imageHeight > imageWidth)
 {
  System.out.println("Potrate Thumnail");
  BufferedImage resizeImage = resizePotraitImage(overlay, BufferedImage.TYPE_INT_ARGB);
  
  // create the new image, canvas size is the max. of both image sizes
     BufferedImage combined = new BufferedImage(COMBINE_IMG_WIDTH, COMBINE_IMG_HEIGHT, BufferedImage.TYPE_INT_ARGB);

  // paint both images, preserving the alpha channels
  Graphics g = combined.getGraphics();
  g.drawImage(background, 0, 0, null);
  g.drawImage(resizeImage, 55, 0, null);

  // Save as new image
  ImageIO.write(combined, "PNG", new File(basePath, "combined.png"));

 }
 else
 {
  System.out.println("Landescape Thumnail");
  
  BufferedImage resizeImage = resizeLandscapeImage(overlay, BufferedImage.TYPE_INT_ARGB);
  // paint both images, preserving the alpha channels
  Graphics g = resizeImage.getGraphics();
  g.drawImage(resizeImage, 0, 0, null);
  
  // Save as new image
  ImageIO.write(resizeImage, "PNG", new File(basePath, "combined.png"));
 }
 
}



 private static BufferedImage resizePotraitImage(BufferedImage originalImage, int type){
 BufferedImage resizedImage = new BufferedImage(POTRAIT_IMG_WIDTH, POTRAIT_IMG_HEIGHT, BufferedImage.TYPE_INT_ARGB);
 Graphics2D g = resizedImage.createGraphics();
 g.drawImage(originalImage, 0, 0, POTRAIT_IMG_WIDTH, POTRAIT_IMG_HEIGHT, null);
 g.dispose();
 
 return resizedImage;
    }
 
 
 
 private static BufferedImage resizeLandscapeImage(BufferedImage originalImage, int type){
  BufferedImage resizedImage = new BufferedImage(COMBINE_IMG_WIDTH, COMBINE_IMG_HEIGHT, BufferedImage.TYPE_INT_ARGB);
  Graphics2D g = resizedImage.createGraphics();
  g.drawImage(originalImage, 0, 0, COMBINE_IMG_WIDTH, COMBINE_IMG_HEIGHT, null);
  g.dispose();
  
  return resizedImage;
 }  
 
 
}


0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...