Sunday, May 4, 2014
To day in this article I will tell you how to resize or scale an image in Java. Many times while developing an application you maya need this. Most often you may require to create a thumbnail of an image in most web applications. This tutorial will guide you describing each steps in detail.
resizeImageWithHints() : This is a slightly modified version of the above method. Here rendering hints and alpha composite are added to the destination image as shown below
scaleImage() : This method takes input a source image and the scale factor varying from 0 to 1. The most important feature of this method is that it reserves i.e. maintains the aspect ratio of original image. Here an AffineTransform instance is created and the image is drwan with that instance
DOWNLOAD the source from Mediafire
DOWNLOAD the source from 4shared
- First read an image from file or URL using ImageIO
- Now create a BufferedImage with specified width, height and image type
- Obtain a Graphics2D object from the destination object create in earlier step
- Draw the original image using the Graphics object
- Dispose the Graphics object
- Return or save the final image
Here in our example we will have a class with 3 methods.
Original image (930 x 520) to be resized or scaled |
- resizeImage() : This method takes in a source image, the width and height of destination image. This is most simple method and follows the steps mentioned above
Image resized (100 x 100) without hints
BufferedImage dest = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g2d = dest.createGraphics(); g2d.drawImage(src, 0, 0, width, height, null); g2d.dispose(); return dest;
Image resized (100 x 100) with hints |
g2d.setComposite(AlphaComposite.Src); g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Image scaled = 0.3 (279 x 156) |
AffineTransform at = AffineTransform.getScaleInstance(scale, scale); g2d.drawRenderedImage(src, at);-------------------------------------------------------------------------------------------------------------------------
Java Source Code
-------------------------------------------------------------------------------------------------------------------------import java.awt.AlphaComposite; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.geom.AffineTransform; import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; public class ImageResizer { /** * Resizes an image without rendering hints depending on input width, height * @param src source image to be resized * @param width width of destination image * @param height height of destination image * @return resized image */ public BufferedImage resizeImage(BufferedImage src,int width,int height){ BufferedImage dest = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g2d = dest.createGraphics(); g2d.drawImage(src, 0, 0, width, height, null); g2d.dispose(); return dest; } /** * Resizes an image with rendering hints depending on input width, height * @param src source image to be resized * @param width width of destination image * @param height height of destination image * @return resized image */ public BufferedImage resizeImageWithHints(BufferedImage src,int width,int height){ BufferedImage dest = new BufferedImage(width, height, src.getType()); Graphics2D g2d = dest.createGraphics(); g2d.setComposite(AlphaComposite.Src); g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.drawImage(src, 0, 0, width, height, null); g2d.dispose(); return dest; } /** * Scales an image depending on sclae factor and maintains * the aspect ratio of original image * @param src source image to be scaled * @param scale scale factor xscale = yscale * @return scaled image */ public BufferedImage scaleImage(BufferedImage src, double scale){ int w = (int)(src.getWidth()*scale); int h = (int)(src.getHeight()*scale); BufferedImage dest = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); Graphics2D g2d = dest.createGraphics(); AffineTransform at = AffineTransform.getScaleInstance(scale, scale); g2d.drawRenderedImage(src, at); return dest; } public static void main(String... args) throws Exception { ImageResizer resizer = new ImageResizer(); BufferedImage src = ImageIO.read(new File("original.jpg")); BufferedImage dest = resizer.resizeImage(src, 100, 100); ImageIO.write(dest, "jpeg", new File("dest-resize.jpg")); dest = resizer.resizeImageWithHints(src, 100, 100); ImageIO.write(dest, "jpeg", new File("dest-resize-hints.jpg")); dest = resizer.scaleImage(src, 0.3); ImageIO.write(dest, "jpeg", new File("dest-scale.jpg")); } }-------------------------------------------------------------------------------------------------------------------------
Download Links
-------------------------------------------------------------------------------------------------------------------------DOWNLOAD the source from Mediafire
DOWNLOAD the source from 4shared
Labels:Graphics2D-Images
Subscribe to:
Post Comments
(Atom)
Total Pageviews
Followers
Labels
- Algorithms (7)
- Annotation (3)
- Files (6)
- Generics (3)
- Graphics2D (5)
- Graphics2D-Images (7)
- Inheritance (2)
- J2EE (9)
- Java 8 (4)
- Java FAQs (19)
- JDBC (3)
- Networking (2)
- Packages (1)
- Reflection (4)
- Security (7)
- Sorting (2)
- Swing (3)
- Threads (3)
- Utils (3)
Popular Posts
-
Today I will show you how you can implement Bankers algorithm in Java. The Banker's algorithm is a resource allocation and deadlock a...
-
------------------------- UPDATE ------------------------- I have updated the code on request of some followers so that they can directly...
-
Today I am going to show how to convert a postfix expression to an infix expression using stack in Java. In an earlier post here we ...
-
Today in this article I will tell you how to convert an infix expression to postfix expression using stack. This is an important applicat...
-
--------------------UPDATE------------------- I have updated my post so that now it can detect IE 11. This modification was necessary as t...
-
Today I am going to show you how you can generate and validate captcha. A CAPTCHA (an acronym for "Completely Automated Public Turin...
-
Today I am going to post a program that will be able to produce all the mColorings of a given graph G. What is mColoring : The problem st...
-
Today in this article I will show you how to create or develop a Tower of Hanoi game in Java. The Tower of Hanoi is a famous problem tha...
0 comments:
Post a Comment