Uploaded Image file to a File object compression using ImageWriter:
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 javax.imageio.ImageWriter;
import javax.imageio.ImageWriteParam;
import org.apache.myfaces.trinidad.model.UploadedFile;
public static File compressUploadedFile(UploadedFile file) {
OutputStream newFile1 = null;
File file1 = null;
try{
BufferedImage image = ImageIO.read(file.getInputStream());
file1 = uploadedFileToFileConverter(file);
newFile1 = new FileOutputStream(file1);
Iterator<ImageWriter>writers = ImageIO.getImageWritersByFormatName("jpg"); // Desired compress image type as jpg
ImageWriter writer = (ImageWriter) writers.next();
ImageOutputStream ios = ImageIO.createImageOutputStream(newFile1);
writer.setOutput(ios);
ImageWriteParam param = writer.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality(0.05f); // Compress to 5% of the original size
writer.write(null, new IIOImage(image, null, null), param);
ios.close();
writer.dispose();
} 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.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriter;
import javax.imageio.ImageWriteParam;
import org.apache.myfaces.trinidad.model.UploadedFile;
public static File compressUploadedFile(UploadedFile file) {
OutputStream newFile1 = null;
File file1 = null;
try{
BufferedImage image = ImageIO.read(file.getInputStream());
file1 = uploadedFileToFileConverter(file);
newFile1 = new FileOutputStream(file1);
Iterator<ImageWriter>writers = ImageIO.getImageWritersByFormatName("jpg"); // Desired compress image type as jpg
ImageWriter writer = (ImageWriter) writers.next();
ImageOutputStream ios = ImageIO.createImageOutputStream(newFile1);
writer.setOutput(ios);
ImageWriteParam param = writer.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality(0.05f); // Compress to 5% of the original size
writer.write(null, new IIOImage(image, null, null), param);
ios.close();
writer.dispose();
} 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;
}
No comments:
Post a Comment