Saturday, July 8, 2017

Create Image Thumbnails using Java

In this post we will learn how we can utilize java to automatically creates thumbnails for existing images with desired thumbnail scaling.

Programming Language:
Java

Git Location:
https://github.com/csanuragjain/extra/tree/master/ThumbnailGenerator

Tutorial Location:
https://cooltrickshome.blogspot.in/2017/07/create-image-thumbnails-using-java.html

Program:

main method:

1
2
3
4
5
6
7
8
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Scanner s =new Scanner(System.in);
  System.out.println("Enter the path of image whose thumbnail need to be generated");
  String imgPath=s.nextLine();
  File thumnailImg=createThumbnail(new File(imgPath), 400, 400);
  System.out.println("Thumbnail generated at "+thumnailImg.getAbsolutePath());
 }

How it works:
1) A scanner object is created to take user input.
2) Image location for which thumbnail need to be generated is taken using the scanner object
3) We call the createThumbnail function (will create this) passing the image and the required thumbnail width and height.
4) createThumbnail returns the File object pointing to the generated thumbnail which is now shown to user.

createThumbnail method:
 /**
  * Creates a thumnail of provided image
  * @param inputImgFile The input image file
  * @param thumnail_width Desired width of the output thumbnail
  * @param thumbnail_height Desired height of thr output thumnail
  */
 public static File createThumbnail(File inputImgFile, int thumnail_width, int thumbnail_height){
  File outputFile=null;
  try {
  BufferedImage img = new BufferedImage(thumnail_width, thumbnail_height, BufferedImage.TYPE_INT_RGB);
  img.createGraphics().drawImage(ImageIO.read(inputImgFile).getScaledInstance(thumnail_width, thumbnail_height, Image.SCALE_SMOOTH),0,0,null);
  outputFile=new File(inputImgFile.getParentFile()+File.separator+"thumnail_"+inputImgFile.getName());
   ImageIO.write(img, "jpg", outputFile);
   return outputFile;
  } catch (IOException e) {
   System.out.println("Exception while generating thumbnail "+e.getMessage());
   return null;
  }
 }

How it works:
1) We use the ImageIO class to create a scaled version of the input image using the predefined getScaledInstance method passing the desired thumbnail width and height
2) We pass the Image from Step1 into a BufferedImage object
3) Now we simply use the write method of ImageIO class to write the BufferedImage from Step2 into a jpg file and return a File object pointing to same.

Output:
 Enter the path of image whose thumbnail need to be generated  
 C:\images\extra\7.jpg  
 Thumbnail generated at C:\images\extra\thumnail_7.jpg  

Full Program:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.cooltrickshome;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;

import javax.imageio.ImageIO;

public class ThumbnailGenerator {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Scanner s =new Scanner(System.in);
  System.out.println("Enter the path of image whose thumbnail need to be generated");
  String imgPath=s.nextLine();
  File thumnailImg=createThumbnail(new File(imgPath), 400, 400);
  System.out.println("Thumbnail generated at "+thumnailImg.getAbsolutePath());
 }

 /**
  * Creates a thumnail of provided image
  * @param inputImgFile The input image file
  * @param thumnail_width Desired width of the output thumbnail
  * @param thumbnail_height Desired height of thr output thumnail
  */
 public static File createThumbnail(File inputImgFile, int thumnail_width, int thumbnail_height){
  File outputFile=null;
  try {
  BufferedImage img = new BufferedImage(thumnail_width, thumbnail_height, BufferedImage.TYPE_INT_RGB);
  img.createGraphics().drawImage(ImageIO.read(inputImgFile).getScaledInstance(thumnail_width, thumbnail_height, Image.SCALE_SMOOTH),0,0,null);
  outputFile=new File(inputImgFile.getParentFile()+File.separator+"thumnail_"+inputImgFile.getName());
   ImageIO.write(img, "jpg", outputFile);
   return outputFile;
  } catch (IOException e) {
   System.out.println("Exception while generating thumbnail "+e.getMessage());
   return null;
  }
 }
}

Hope it helps :)

No comments:

Post a Comment