2017  Kodetalk | Feedback | Privacy Policy | Terms | About

Creation of thumbnail image with fixed dimension using Java

The requirement and the issue,

1. User can send any type of image from the client(browser) to the server (JEE web application)

2. Application has to be smart enough to find the size of the image.

3. If the image size beyond the required image supported by the application then need to be scale the image as a thumbnail with the fixed size.

Currently we are able to find the image size but the issue is to scaling and creating thumbnail from that. Please help us to scaling the image with a fixed size like 100*100 or 250*150 by using Java. 


Its quite easy to scaling the image using java application. If you google will get multiple ways to scaling the image. We have also a similar kind of requirement like you have but only the difference is you need the image from client but we are access the image from AWS server through the given URL and scale an image. 

Here you can find the snippet which I can say the best approach to scaling the image with the fixed length thumbnail view may be helpful for you.

//snippet to scaling the image into fixed size

String imageHTTPPath = "http://localhost:8080/images/1462259987892.jpeg";

InputStream in = new URL(imageHTTPPath).openStream();

BufferedImage image = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);

image.createGraphics().drawImage(ImageIO.read(in).getScaledInstance(100, 100, Image.SCALE_SMOOTH), 0, 0,null);

ByteArrayOutputStream os = new ByteArrayOutputStream();

ImageIO.write(image, "jpeg", os);

//Snippet to encode byte array into Base64 after scaling an image

StringBuilder sb = new StringBuilder();



String base64Image = sb.toString(); // Final step of the image 100*100 encoded into base64

Answer is