package com.net.test;
import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.imageio.stream.ImageInputStream;
import com.sun.image.codec.jpeg.ImageFormatException;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class TestImagesDpi {
public static void main(String[] args) throws ImageFormatException, IOException {
FileOutputStream fos = null;
// byte[] rawData = getRawBytesFromFile(""); // some code to read raw bytes from image file
// ImageInputStream iis = ImageIO.createImageInputStream(new ByteArrayInputStream(rawData));
BufferedImage images = ImageIO.read(new File("C:\\Users\\Rajesh\\Desktop\\richagangopadhyay217.jpg"));
// images.se
int type = images.getType() == 0? BufferedImage.TYPE_INT_ARGB : images.getType();
images = resizeImageWithHint(images, type);
// for (int i = 0; i < imageFiles.length; i++) {
fos = new FileOutputStream(new File("C:\\Users\\Rajesh\\Desktop\\test.jpg"));
JPEGImageEncoder jpegEncoder = JPEGCodec.createJPEGEncoder(fos);
JPEGEncodeParam jpegEncodeParam = jpegEncoder.getDefaultJPEGEncodeParam(images);
jpegEncodeParam.setDensityUnit(JPEGEncodeParam.DENSITY_UNIT_DOTS_INCH);
jpegEncodeParam.setXDensity(600);
jpegEncodeParam.setYDensity(600);
jpegEncodeParam.setQuality(1.0f, true);
jpegEncoder.encode(images, jpegEncodeParam);
fos.close();
// }
}
private static BufferedImage resizeImageWithHint(BufferedImage originalImage, int type){
BufferedImage resizedImage = new BufferedImage(256, 256, type);
Graphics2D g = resizedImage.createGraphics();
g.drawImage(originalImage, 0, 0, 256, 256, null);
g.dispose();
g.setComposite(AlphaComposite.Src);
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.setRenderingHint(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
return resizedImage;
}
private static byte[] getRawBytesFromFile(String string) throws FileNotFoundException {
// TODO Auto-generated method stub
File file = new File("C:\\Users\\Rajesh\\Desktop\\richagangopadhyay217.jpg");
FileInputStream fis = new FileInputStream(file);
//create FileInputStream which obtains input bytes from a file in a file system
//FileInputStream is meant for reading streams of raw bytes such as image data. For reading streams of characters, consider using FileReader.
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
try {
for (int readNum; (readNum = fis.read(buf)) != -1;) {
//Writes to this byte array output stream
bos.write(buf, 0, readNum);
System.out.println("read " + readNum + " bytes,");
}
} catch (IOException ex) {
// Logger.getLogger(ConvertImage.class.getName()).log(Level.SEVERE, null, ex);
}
byte[] bytes = bos.toByteArray();
return bytes;
}
}