Wednesday, February 17, 2010

Image DPI setting

Here is the important topic of this blog.  How to create TIFF image using java code?  There are various options (APIs) available for tiff image creation using java technology.  You can do this with using different API’s available in java technology.

At the same time when we create the TIFF image, we need to take care of some image operation and information (i.e. image metadata).  We need to maintain the DPI of given image, horizontal and vertical scaling (height and width), maintain the metadata etc.  For this I divided the source code into different parts.  Later you can see the specific topics related to image operation, source code and its explanation.

Image DPI:

In this post I am giving an explanation of DPI setting of an image.  Whenever we do work with image processing one of the biggest things which we need to do is maintaining the DPI of that image or setting the DPI of image.  After converting image into BufferedImage, by default its density of dots per inches will set to 96 dpi.  Cause of this we can not maintain the quality of image.  Less DPI means reduce the density of dot per inches and it directly affect on image quality.  For this we need to set X and Y resolution in an image metadata.

JPEG image DPI setting:

Need to Import Packages:
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

(SUN Microsystems provided API details of above mentioned classes.  Use Google to search related API on SUN site for detailed information.)

Source Code:
  
Static Variables:
// static variable for value of X resolution
private static int X_DPI = 200;
// static variable for value of Y resolution
private static int Y_DPI = 200;


Method:
void setDPIOfAnInputJPGImage(File inputFile, File outputFile) {
        try {
            // create input buffered image
            BufferedImage image = ImageIO.read(inputFile);
            // create jpegEncode for output image
            JPEGImageEncoder jpegEncoder = JPEGCodec
                    .createJPEGEncoder(new FileOutputStream(outputFile));
            // create jpeg encoder from getting defaul value from input buffered
            // image
            JPEGEncodeParam jpegEncodeParam = jpegEncoder
                    .getDefaultJPEGEncodeParam(image);
            // setting up density unit paramter
            jpegEncodeParam
                    .setDensityUnit(JPEGEncodeParam.DENSITY_UNIT_DOTS_INCH);
            // setting up jpeg encode parameter
            jpegEncoder.setJPEGEncodeParam(jpegEncodeParam);
            // set quality parameter
            jpegEncodeParam.setQuality(0.75f, false);
            // set X-resolution
            jpegEncodeParam.setXDensity(X_DPI);
            // set Y-resolution
            jpegEncodeParam.setYDensity(Y_DPI);
            // encode output image
            jpegEncoder.encode(image, jpegEncodeParam);
            // flush the buffer image
            image.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

What more you need to do:
- Create a java class
- Declare the static variable for resolution (DPI is totally depends on the value which provided by user i.e. value of X_DPI and Y_DPI)
- Copy and paste above method inside java class (setDPIOfAnInputJPGImage)
- Write a main method which will pass two File objects to the DPI setting method (first for input image and second for output image)
- Compile and Run the code

References:
Java Media API: http://java.sun.com/javase/technologies/desktop/media/

Note:  For detailed information visit SUN Microsystems.  Any query and suggestions from your side will always welcome.