Wednesday, December 22, 2010

Rotate and translate images with AffineTransform

Staying on the same subject as my previous posts, the next example will help you rotate and translate any BufferedImage.

public BufferedImage rotateImage(BufferedImage rotateImage) {
AffineTransformOp op = null;

try {

   AffineTransform tx = new AffineTransform();

   //Rotate 90ยบ
   tx.rotate(Math.toRadians(90), rotateImage.getWidth()
              / 2.0, rotateImage.getHeight() / 2.0);

   AffineTransform translationTransform;
   translationTransform = findTranslation(tx, rotateImage);

   tx.preConcatenate(translationTransform);

   op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);

   } catch (Exception e) {
      //Do something here
   }

   return op.filter(rotateImage, null);
}

Thursday, December 16, 2010

How to convert images

javax.imageio.ImageIO let's you convert and save images from one format into another.It works using plug-in modules that handle various formats, including gif, png, bmp, jpg and others. We can use getWriterFormatNames() to find out all supported types.

String[] types = ImageIO.getWriterFormatNames();

  for (String name: types){
    System.out.println(name);
  }


Next, let's load our image into a BufferedImage which is a subclass of Image class:
File inputFile = new File("1.bmp");

  BufferedImage image = ImageIO.read(inputFile);

Finally convert the image:
File outputFile = new File("1.jpg");

  ImageIO.write(image, "jpg", outputFile);

In this example, the image file bmp was converted into a jpg image file.

Wednesday, December 15, 2010

How to resize Buffered Images

When we need to resize or scale images in Java, the Java2D Team has been encouraging developers to move away from Image.getScaledInstance() to more updated APIs. This is exactly what I tried to do. So, if you are in need of a fairly good function that resizes images, look no further :)

private BufferedImage resizedImage(BufferedImage oriImage) {

    int type = oriImage.getType() == 0
            ? BufferedImage.TYPE_INT_ARGB : oriImage.getType();

    BufferedImage scaledImage = new BufferedImage((int) scaleWidth,
            (int) scaleHeight, type);

    // Paint scaled version of image to new image

    Graphics2D graphics2D = scaledImage.createGraphics();
    graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
            RenderingHints.VALUE_INTERPOLATION_BILINEAR);

    graphics2D.drawImage(oriImage, 0, 0, (int) scaleWidth,
            (int) scaleHeight, null);

    // Clean up
    graphics2D.dispose();

//Return scaled image
 return scaledImage;

}


Aside note: Some of my co-workers asked me about AffineTransform.scale() when performing scaled functions. Well, there is nothing wrong in using AffineTransform.Here's an example:
 AffineTransform xform = AffineTransform.getScaleInstance(2, 2);
 graphics2D.drawImage(img, xform, null);

But in a majority of cases, there is a better approach. Simply use the scaling variant of Graphics.drawImage()

 graphics2D.drawImage(oriImage, 0, 0, oriImage.getWidth()*2,
            oriImage.getHeight()*2, null);