I used the following code to write an image to a file using BI_RLE8 compression. How can I read back the image saved in that file?
BufferedImage source = new BufferedImage(200, 100, BufferedImage.TYPE_BYTE_GRAY);
ImageWriter writer = ImageIO.getImageWritersByFormatName("bmp").next();
ImageWriteParam param = writer.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionType("BI_RLE8");
writer.setOutput(new FileImageOutputStream(new File("c:\\RLE.bmp")));
writer.write(null, new IIOImage(source, null, null), param);
writer.dispose();
Related
We have generated a JPG file by using
org.openqa.selenium.TakesScreenshot class
File src = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
File dest = new File("C:\\Users\\admin\\Desktop\\Images\\29_18-03-20-07-11-47.jpg");
FileHandler.copy(src, dest);
As the generated image is having file size of 1.5MB to 2.9MB size. We want to reduce the file size to kbs.
we tried ImageIO but the generated image is not having original file color space/color profile.
File input = new File("C:\\Users\\admin\\Desktop\\Images\\29_18-03-20-07-11-47.jpg");
File compressedImageFile = new File("C:\\Users\\admin\\Desktop\\Images\\compressed_image7.jpg");
BufferedImage image = ImageIO.read(input);
OutputStream os = new FileOutputStream(compressedImageFile);
Iterator<ImageWriter writers = ImageIO.getImageWritersByFormatName("jpg");
ImageWriter writer = (ImageWriter) writers.next();
ImageOutputStream ios = ImageIO.createImageOutputStream(os);
writer.setOutput(ios);
ImageWriteParam param = writer.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality(0.05f);
writer.write(null, new IIOImage(image, null, null), param);
Could not share the image or screen shot
Could someone let me know how can I reduce the filesize
Try using lossless JPEG
ImageWriter writer= (ImageWriter) ImageIO.getImageWritersByFormatName("jpeg").next();
ImageWriteParam param= writer.getDefaultWriteParam();
param.setCompressionMode(param.MODE_EXPLICIT);
param.setCompressionType("JPEG-LS");
writer.setOutput(ImageIO.createImageOutputStream(new File(path)));
writer.write(null, new IIOImage(image, null, null), param);
I am using below code for reducing image size. It reduces size from 1MB to 250 KB. Its fine but it changes image colour.
File input = new File("ImageTOCompress.jpg");
BufferedImage image = ImageIO.read(input);
File compressedImageFile = new File("CompressedImage.jpg");
OutputStream os =new FileOutputStream(compressedImageFile);
Iterator<ImageWriter>writers = ImageIO.getImageWritersByFormatName("jpg");
ImageWriter writer = (ImageWriter) writers.next();
ImageOutputStream ios = ImageIO.createImageOutputStream(os);
writer.setOutput(ios);
ImageWriteParam param = writer.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality(0.05f);
writer.write(null, new IIOImage(image, null, null), param);
os.close();
ios.close();
writer.dispose();
Please help me to maintain image colour as it is...Thanks!!!
I am using this method to change image size:
private File resize(double scale, File file) throws IOException {
double scaledSize = targetSize * scale;
BufferedImage scaledImage = Scalr.resize(sourceImage, (int) scaledSize);
Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName(fileExt);
ImageWriter writer = iter.next();
ImageWriteParam iwp = writer.getDefaultWriteParam();
iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
iwp.setCompressionQuality(1);
FileImageOutputStream output = new FileImageOutputStream(file);
writer.setOutput(output);
IIOImage image = new IIOImage(scaledImage, null, null);
writer.write(null, image, iwp);
writer.dispose();
return file;
}
It seems images lose color depth. How can I set same color depth as source image?
I have a piece of code that compress an jpg image with a certain quality, but when the image is png type, they all turn black. Any idea why and how to fix it? here is my code.
public void compressImage(String filename, ServletContext servletContext) {
//You first need to enumerate the image writers that are available to jpg
Iterator iter = ImageIO.getImageWritersByFormatName("jpg");
//Then, choose the first image writer available
ImageWriter writer = (ImageWriter) iter.next();
//instantiate an ImageWriteParam object with default compression options
ImageWriteParam iwp = writer.getDefaultWriteParam();
//Set the compression quality
iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
iwp.setCompressionQuality(0.5f);
try {
BufferedImage img = ImageIO.read(new File(filename));
String destPath = "/Users/KingdomHeart/resources/scholar/compress/compress.jpg";
File file = new File(destPath);
FileImageOutputStream output = new FileImageOutputStream(file);
writer.setOutput(output);
IIOImage image = new IIOImage(img, null, null);
writer.write(null, image, iwp);
writer.dispose();
}catch(IOException e){
}
}
This might have the answers you're looking for: Converting transparent gif / png to jpeg using java
The issue is likely that you're working with a PNG that has some transparency in it.
I am using java and the following code. Is there a way to use RenderingHints to accomplish this?
try {
sourceImage = ImageIO.read(new File("images.jpg"));
BufferedImage dstImage = null;
ColorSpace colorSpace = ColorSpace.getInstance(ColorSpace.CS_GRAY);
ColorConvertOp op = new ColorConvertOp(colorSpace, null);
dstImage = op.filter(sourceImage, null);
ImageIO.write(dstImage, "jpeg", new File("output.jpg"));
System.out.println("processing complete");
} catch (IOException e) {
e.printStackTrace();
}
I got this working using ImageWriteParam.setCompressionQuality
Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName("jpeg");
ImageWriter writer = (ImageWriter)iter.next();
// instantiate an ImageWriteParam object with default compression options
ImageWriteParam iwp = writer.getDefaultWriteParam();
iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
iwp.setCompressionQuality(0.5f); // an integer between 0 and 1
// 1 specifies minimum compression and maximum quality
File file = new File("output.jpg");
FileImageOutputStream output = new FileImageOutputStream(file);
writer.setOutput(output);
IIOImage image = new IIOImage(dstImage, null, null);
writer.write(null, image, iwp);
writer.dispose();