Showing posts with label ImageIO. Show all posts
Showing posts with label ImageIO. Show all posts

Tuesday, 2 July 2013

Resize , Merge Images Using Java

1 comments
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;
 }  
 
 
}


Thursday, 28 February 2013

File Upload & Image Conversion In Spring 3.0 Using Java.ImageIO

0 comments
web.xml


  HelloWorldExampleWithSpring3MVCInEclipse
  
    Spring MVC Dispatcher Servlet
    org.springframework.web.servlet.DispatcherServlet
    
      contextConfigLocation
      
    /WEB-INF/app-config.xml
   
    
    1
  
  
    Spring MVC Dispatcher Servlet
    /
  
  
  
   index.jsp
  




app-config.xml



 
 
 
 
 
 
 
 
  
  
 
 
 
    
        
        
    




UploadFormController.java
package com;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import com.domain.UploadForm;

@Controller
public class UploadFormController implements HandlerExceptionResolver{
 
 @RequestMapping(value="/FileUploadForm", method=RequestMethod.GET)
 public String showForm(ModelMap model){
  UploadForm form = new UploadForm();
  model.addAttribute("FORM", form);
  return "FileUploadForm";
 }

 @RequestMapping(value="/fileAction", method=RequestMethod.POST)
 public String processForm(@ModelAttribute(value="FORM") UploadForm form,BindingResult result){
  if(!result.hasErrors()){
   FileOutputStream outputStream = null;
   String filePath = System.getProperty("java.io.tmpdir") + "/" + form.getFile().getOriginalFilename();
   try {
    outputStream = new FileOutputStream(new File(filePath));
    outputStream.write(form.getFile().getFileItem().get()); 
    outputStream.close();
   } catch (Exception e) {
    System.out.println("Error while saving file");
    return "FileUploadForm";
   }
   return "success";
  }else{
   return "FileUploadForm";
  }  
 }
 

 @Override
 public ModelAndView resolveException(HttpServletRequest arg0,
   HttpServletResponse arg1, Object arg2, Exception exception) {
  Map model = new HashMap();
        if (exception instanceof MaxUploadSizeExceededException)
        {
            model.put("errors", "File size should be less then "+((MaxUploadSizeExceededException)exception).getMaxUploadSize()+" byte.");
        } else
        {
            model.put("errors", "Unexpected error: " + exception.getMessage());
        }
        model.put("FORM", new UploadForm());
        return new ModelAndView("/FileUploadForm", (Map) model);

 }
 
 
 @RequestMapping(value = "/image.htm", method = RequestMethod.POST)
 public ModelAndView getFile(@ModelAttribute(value="FORM") UploadForm form) throws IOException {
     try {
      System.out.println("In Image Processing Action");
      String fileType = form.getFile().getContentType();
      
         // Retrieve image from the classpath.
         InputStream is = form.getFile().getInputStream(); 

         // Prepare buffered image.
         BufferedImage img = ImageIO.read(is);

         // Create a byte array output stream.
         ByteArrayOutputStream bao = new ByteArrayOutputStream();
         
         File output = new File("F://output.tiff");
         
         // Write to output stream
         ImageIO.write(img, "jpg", output);
         
         return new ModelAndView("success");
     } catch (IOException e) 
     {
         throw new RuntimeException(e);
     }
 }

}

UploadForm.java
package com.domain;

import org.springframework.web.multipart.commons.CommonsMultipartFile;

public class UploadForm {
 
 private String name = null;
 private CommonsMultipartFile file = null;
 
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public CommonsMultipartFile getFile() {
  return file;
 }
 public void setFile(CommonsMultipartFile file) {
  this.file = file;
  this.name = file.getOriginalFilename();
 }
 
 
}


FileUploadForm.jsp

<%@ page session="true" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>

 
  File Upload with Spring 3 MVC
  
 
 
  

File Upload Form


${errors}
Name :

success.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>




File Upload Confirmation Page


 

File has been uploaded successfully.


File Name : ${FORM.name}.



Related Posts Plugin for WordPress, Blogger...