Reduce the filesize of a JPG file in java - java

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);

Related

this code works fine , but compression of png image is not happening

Code works fine for other formats of images but for .png compression is negligible or no compression at all
here is a sample how the image is loaded and saved
Or someone suggest me better way of compressing png image
This is myfirst question posted on stackoverflow. please ignore any mistakes
I have taken sample images from local folder
File input = new File("C:/Users/Public/Pictures/Sample Pictures/bz_nela19911.png");
InputStream is = new FileInputStream(input);
BufferedImage image = ImageIO.read(is);
File compressedImageFile = new File("C:/Users/Public/Pictures/Sample Pictures/me1.png");
OutputStream os =new FileOutputStream(compressedImageFile);
Iterator<ImageWriter>writers = ImageIO.getImageWritersByFormatName("png");
// works fine for other formats of images
ImageWriter writer = null;
while (writers.hasNext()) {
ImageWriter candidate = writers.next();
if (candidate.getClass().getSimpleName().equals("CLibPNGImageWriter")) {
writer = candidate; // This is the one we want
break;
}
else if (writer == null) {
writer = candidate;
}
}
ImageOutputStream ios = ImageIO.createImageOutputStream(os);
writer.setOutput(ios);
ImageWriteParam param = writer.getDefaultWriteParam();
if (param.canWriteCompressed()) {
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality(0.1f);
}
writer.write(null, new IIOImage(image, null, null), param);

Java Image Compression : colour changes

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!!!

Reading a bitmap image from a file

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();

How to Convert jpeg image into jpeg2000 lossless compression image?

I am compressing a image using java.
I want to compress the image in jpeg2000 lossless.
Please suggest solution.
File imageFile = new File("C:\\Users\\Public\\Pictures\\Sample Pictures\\Chrysanthemum.jpg");
File compressedImageFile = new File("C:\\Users\\Public\\Pictures\\Sample Pictures\\decompjai.jpg");
InputStream is = new FileInputStream(imageFile);
OutputStream os = new FileOutputStream(compressedImageFile);
BufferedImage image = ImageIO.read(is);
// get all image writers for JPG format
Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("jp2k");
if (!writers.hasNext())
throw new IllegalStateException("No writers found");
ImageWriter writer = (ImageWriter) writers.next();
J2KImageWriter
J2KImageWriteParam jwp = (J2KImageWriteParam) writer.getDefaultWriteParam();
// J2KImageWriteParam jwp = new J2KImageWriteParam();
boolean lossless = true;
jwp.setLossless(lossless);
// jwp.setFilter(J2KImageWriteParam.FILTER_97);
if (!lossless) {
jwp.setEncodingRate(8.0 / 2);
}
// ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
ImageOutputStream ios = ImageIO.createImageOutputStream(os);
writer.setOutput(ios);
writer.write(null, new IIOImage(image, null, null), jwp);
ios.flush();
ios.close();
} catch (IOException e) {
e.printStackTrace();
}
I am trying with the above code.
Check out this JPEG2000 Java Encoder/Decoder project. (you can find the source code here)
I'd first try javax.ImageIO as your system may have a provider for the JPEG2000 format.
Otherwise: http://www.deic.uab.cat/~francesc/ is apparently for java.

png file turn all black when compress to jpeg. Is it because PNG is lossless?

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.

Categories

Resources