I am currently doing project related to "image steganography" in Java, for that I want to compress and decompress the secret image. I have done the compression part. But I don't know how to perform the decompression part.
I have compressed the JPEG image using the following code:
public class CompressJPEGFile {
public static void main(String[] args) throws IOException {
File imageFile = new File("C:\\Users\\user\\Desktop\\encryption\\d.jpg");
File compressedImageFile = new File("C:\\Users\\user\\Desktop\\encryption\\compress.jpg");
InputStream is = new FileInputStream(imageFile);
OutputStream os = new FileOutputStream(compressedImageFile);
float quality = 0.5f;
BufferedImage image = ImageIO.read(is);
// get all image writers for JPG format
Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("jpg");
if (!writers.hasNext())
throw new IllegalStateException("No writers found");
ImageWriter writer = (ImageWriter) writers.next();
ImageOutputStream ios = ImageIO.createImageOutputStream(os);
writer.setOutput(ios);
ImageWriteParam param = writer.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality(quality);
//associated stream and image metadata and thumbnails to the output
writer.write(null, new IIOImage(image, null, null), param);
// close all streams
is.close();
os.close();
ios.close();
writer.dispose();
}
}
I have written the code for decompression. Is the code right if I am not considering the quality of image?
public static void decompress() throws FileNotFoundException {
try {
File compressedImageFile = new File("C:\\Users\\user\\Desktop\\encryption\\compressnew.jpg");
File imageFile = new File("C:\\Users\\user\\Desktop\\encryption\\dnew.jpg");
InputStream is = new FileInputStream(compressedImageFile);
OutputStream os = new FileOutputStream(imageFile );
BufferedImage image = ImageIO.read(is);
// get all image writers for JPG format
Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("jpg");
if (!writers.hasNext()) {
throw new IllegalStateException("No writers found");
}
ImageWriter writer = (ImageWriter) writers.next();
ImageOutputStream ios = ImageIO.createImageOutputStream(os);
writer.setOutput(ios);
ImageWriteParam param = writer.getDefaultWriteParam();
writer.write(null, new IIOImage(image, null, null), param);
//associated stream and image metadata and thumbnails to the output
is.close();
os.close();
ios.close();
writer.dispose();
} catch (IOException ex) {
Logger.getLogger(CompressJPEGFile.class.getName()).log(Level.SEVERE, null, ex);
}
}