Thread: Image Compressing in java

Forum : Java I/O   30-11-2017 12:05:13 PM
User Image
AdityaT

Newbie

Joined: Apr 20, 2017

Points: 490

Threads: 1

Posts: 1

Image Compressing in java

| Quote Date : Nov 30, 2017    Views:98    

I have a requirement to compress am image using java code,
For ex.


the above image should be compress as follows:


Comments

User Image
ArifKhan

Newbie

Joined : Jun 25, 2017

Points : 175

Threads: 109

Posts: 92

Re: Image Compressing in java

Reply Date : Nov 30, 2017

package com.java.miscellaneous;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

/**
 * This program demonstrates how to resize an image.
 *
 * @author Arif khan
 *
 */
public class ImageResizer {

/**
* Resizes an image to a absolute width and height (the image may not be
* proportional)
*
* @param inputImagePath
*           Path of the original image
* @param outputImagePath
*           Path to save the resized image
* @param scaledWidth
*           absolute width in pixels
* @param scaledHeight
*           absolute height in pixels
* @throws IOException
*/
public static void resize(String inputImagePath, String outputImagePath, int scaledWidth, int scaledHeight)
throws IOException {
// reads input image
File inputFile = new File(inputImagePath);
BufferedImage inputImage = ImageIO.read(inputFile);

// creates output image
BufferedImage outputImage = new BufferedImage(scaledWidth, scaledHeight, inputImage.getType());

// scales the input image to the output image
Graphics2D g2d = outputImage.createGraphics();
g2d.drawImage(inputImage, 0, 0, scaledWidth, scaledHeight, null);
g2d.dispose();

// extracts extension of output file
String formatName = outputImagePath.substring(outputImagePath.lastIndexOf(".") + 1);

// writes to output file
ImageIO.write(outputImage, formatName, new File(outputImagePath));
}

/**
* Resizes an image by a percentage of original size (proportional).
*
* @param inputImagePath
*           Path of the original image
* @param outputImagePath
*           Path to save the resized image
* @param percent
*           a double number specifies percentage of the output image over the
*           input image.
* @throws IOException
*/
public static void resize(String inputImagePath, String outputImagePath, double percent) throws IOException {
File inputFile = new File(inputImagePath);
BufferedImage inputImage = ImageIO.read(inputFile);
int scaledWidth = (int) (inputImage.getWidth() * percent);
int scaledHeight = (int) (inputImage.getHeight() * percent);
resize(inputImagePath, outputImagePath, scaledWidth, scaledHeight);
}

/**
* Test resizing images
*/
public static void main(String[] args) {
String inputImagePath = "F:\\khan.jpeg";
String outputImagePath1 = "F:\\khan.jpeg";
String outputImagePath2 = "F:\\khan.jpeg";
String outputImagePath3 = "F:\\khan.jpeg";

try {
// resize to a fixed width (not proportional)
int scaledWidth = 1024;
int scaledHeight = 768;
ImageResizer.resize(inputImagePath, outputImagePath1, scaledWidth, scaledHeight);

// resize smaller by 50%
double percent = 0.5;
ImageResizer.resize(inputImagePath, outputImagePath2, percent);

// resize bigger by 50%
percent = 1.5;
ImageResizer.resize(inputImagePath, outputImagePath3, percent);

} catch (IOException ex) {
System.out.println("Error resizing the image.");
ex.printStackTrace();
}
}

}


May this will help you..!!

Load More

No More Comments