Uploaded Image file to a File object compression using Graphics2D:
import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import javax.imageio.ImageIO;
import org.apache.myfaces.trinidad.model.UploadedFile;
public static File compressUploadedFile(UploadedFile file) {
OutputStream newFile1 = null;
File file1 = null;
try {
BufferedImage img = ImageIO.read(file.getInputStream());
file1 = uploadedFileToFileConverter(file);
newFile1 = new FileOutputStream(file1);
int width = img.getWidth();
int height = img.getHeight();
int imgWidth, imgHeight;
if (height > width) {
imgWidth = (img.getWidth() * 300)/ img.getHeight(); // 300 is the desired max image size
imgHeight = 300;
} else {
imgHeight = (img.getHeight() * 300)/ img.getWidth();
imgWidth = 300;
}
final BufferedImage bufferedImage = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_RGB);
final Graphics2D graphics2D = bufferedImage.createGraphics();
graphics2D.setComposite(AlphaComposite.Src);
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2D.setRenderingHint(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY);
graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
graphics2D.drawImage(img, 0, 0, imgWidth, imgHeight, null);
graphics2D.dispose();
ImageIO.write(bufferedImage, "jpg", newFile1); // Desired compress image type as jpg
} catch (IOException e) {
logger.severe(e);
}
return file1;
}
public static File uploadedFileToFileConverter(UploadedFile uf) {
InputStream inputStream = null;
OutputStream outputStream = null;
//Add you expected file encoding here:
System.setProperty("file.encoding", "UTF-8");
File newFile = new File(uf.getFilename());
try {
inputStream = uf.getInputStream();
outputStream = new FileOutputStream(newFile);
int read = 0;
byte[] bytes = new byte[1024];
while ((read = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
}
} catch (IOException e) {
logger.severe(e);
}
return newFile;
}
import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import javax.imageio.ImageIO;
import org.apache.myfaces.trinidad.model.UploadedFile;
public static File compressUploadedFile(UploadedFile file) {
OutputStream newFile1 = null;
File file1 = null;
try {
BufferedImage img = ImageIO.read(file.getInputStream());
file1 = uploadedFileToFileConverter(file);
newFile1 = new FileOutputStream(file1);
int width = img.getWidth();
int height = img.getHeight();
int imgWidth, imgHeight;
if (height > width) {
imgWidth = (img.getWidth() * 300)/ img.getHeight(); // 300 is the desired max image size
imgHeight = 300;
} else {
imgHeight = (img.getHeight() * 300)/ img.getWidth();
imgWidth = 300;
}
final BufferedImage bufferedImage = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_RGB);
final Graphics2D graphics2D = bufferedImage.createGraphics();
graphics2D.setComposite(AlphaComposite.Src);
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2D.setRenderingHint(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY);
graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
graphics2D.drawImage(img, 0, 0, imgWidth, imgHeight, null);
graphics2D.dispose();
ImageIO.write(bufferedImage, "jpg", newFile1); // Desired compress image type as jpg
} catch (IOException e) {
logger.severe(e);
}
return file1;
}
public static File uploadedFileToFileConverter(UploadedFile uf) {
InputStream inputStream = null;
OutputStream outputStream = null;
//Add you expected file encoding here:
System.setProperty("file.encoding", "UTF-8");
File newFile = new File(uf.getFilename());
try {
inputStream = uf.getInputStream();
outputStream = new FileOutputStream(newFile);
int read = 0;
byte[] bytes = new byte[1024];
while ((read = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
}
} catch (IOException e) {
logger.severe(e);
}
return newFile;
}