JavaBeat

  • Home
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Privacy

How To Get Supported Image Formats Using Java

April 21, 2014 by Krishna Srinivasan Leave a Comment

This example is for how to list read/write image formats in the Java applications. If you are working with the image processing applications, it is necessary to know the supported formats to inform the user. Lets look at the example on how to list read/write image formats. SupportedImageFormatsExample.java

[code lang=”java”]
package javabeat.net.core;

import java.util.HashSet;
import java.util.Set;

import javax.imageio.ImageIO;

/**
* Supported Types Example
*
* @author Krishna
*
*/
public class SupportedImageFormatsExample {
public static void main(String[] args) {
Set setOfSupportedTypes = new HashSet();

//Get list of all registered readers
String[] formatNamesArray = ImageIO.getReaderFormatNames();

for (int i = 0; i < formatNamesArray.length; i++) {
setOfSupportedTypes.add(formatNamesArray[i].toLowerCase());
}

System.out.println("Read Formats Supported : " + setOfSupportedTypes);
setOfSupportedTypes.clear();

//Get list of all registered writers
formatNamesArray = ImageIO.getWriterFormatNames();

for (int i = 0; i < formatNamesArray.length; i++) {
setOfSupportedTypes.add(formatNamesArray[i].toLowerCase());
}
System.out.println("Write Formats Supported : " + setOfSupportedTypes);
setOfSupportedTypes.clear();

//Get list of all MIME types registered readers
formatNamesArray = ImageIO.getReaderMIMETypes();

for (int i = 0; i < formatNamesArray.length; i++) {
setOfSupportedTypes.add(formatNamesArray[i].toLowerCase());
}
System.out.println("Supported read MIME types: " + setOfSupportedTypes);
setOfSupportedTypes.clear();

//Get list of all MIME types registered writers
formatNamesArray = ImageIO.getWriterMIMETypes();

for (int i = 0; i < formatNamesArray.length; i++) {
setOfSupportedTypes.add(formatNamesArray[i].toLowerCase());
}
System.out.println("Supported write MIME types: " + setOfSupportedTypes);
}
}

[/code]

Output…

[code]
Read Formats Supported : [jpg, bmp, jpeg, wbmp, png, gif]
Write Formats Supported : [bmp, jpg, wbmp, jpeg, png, gif]
Supported read MIME types: [image/jpeg, image/png, image/x-png, image/vnd.wap.wbmp, image/bmp, image/gif]
Supported write MIME types: [image/png, image/jpeg, image/x-png, image/vnd.wap.wbmp, image/bmp, image/gif]
[/code]

Filed Under: Java Tagged With: Java ImageIO

How To Compress Image Using Java

April 21, 2014 by Krishna Srinivasan Leave a Comment

Compression of image file is one of the important task when it comes to save the large number of image files. It saves lot of space if you could compress the images when it is necessary. This example demonstrates how to compress the the JPEG file and reduce the size.

ImageWriteParam class is mainly used for compression the images. It has two methods which is mostly used for changing the compression settings.
setCompressionQuality() method used for setting the quality of the compressed image the value between 0 to 1. A compression quality setting of 0.0 is most generically interpreted as “high compression is important,” while a setting of 1.0 is most generically interpreted as “high image quality is important.” Also we used method setCompressionMode() to set the quality of the compression.

Here is the list of methods that are available in the class ImageWriteParam,

  1. protected boolean canOffsetTiles
  2. protected boolean canWriteCompressed
  3. protected boolean canWriteProgressive
  4. protected boolean canWriteTiles
  5. protected int compressionMode
  6. protected float compressionQuality
  7. protected String compressionType
  8. protected String[] compressionTypes
  9. protected Locale locale
  10. protected Dimension[] preferredTileSizes
  11. protected int progressiveMode
  12. protected int tileGridXOffset
  13. protected int tileGridYOffset
  14. protected int tileHeight
  15. protected boolean tilingSet

Lets look at the example which compress the given image and output the image with small in size. Lets try this example, post your comments.

CompressJPEGFileExample.ajav

[code lang=”java”]
package javabeat.net.core;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Iterator;

import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;

/**
* Compress JPEG File Example
*
* @author Krishna
*
*/
public class CompressJPEGFileExample {
public static void main(String[] args) throws FileNotFoundException, IOException{
File imageFile = new File("Desert.jpg");
File compressedImageFile = new File("compressed_file.jpg");

InputStream inputStream = new FileInputStream(imageFile);
OutputStream outputStream = new FileOutputStream(compressedImageFile);

float imageQuality = 0.3f;

//Create the buffered image
BufferedImage bufferedImage = ImageIO.read(inputStream);

//Get image writers
Iterator<ImageWriter> imageWriters = ImageIO.getImageWritersByFormatName("jpg");

if (!imageWriters.hasNext())
throw new IllegalStateException("Writers Not Found!!");

ImageWriter imageWriter = (ImageWriter) imageWriters.next();
ImageOutputStream imageOutputStream = ImageIO.createImageOutputStream(outputStream);
imageWriter.setOutput(imageOutputStream);

ImageWriteParam imageWriteParam = imageWriter.getDefaultWriteParam();

//Set the compress quality metrics
imageWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
imageWriteParam.setCompressionQuality(imageQuality);

//Created image
imageWriter.write(null, new IIOImage(bufferedImage, null, null), imageWriteParam);

// close all streams
inputStream.close();
outputStream.close();
imageOutputStream.close();
imageWriter.dispose();
}
}
[/code]

I hope this tutorial helped you to understand how to compress a file using Java. There are various techniques to compress the image file, if you have used any other good techniques to compress a image files, please share with us here.

Filed Under: Java Tagged With: Java ImageIO

How To Get Image Format Using Java

April 21, 2014 by Krishna Srinivasan Leave a Comment

Java provides the ImageIO to work with image files. This example demonstrates how to get the type or format of the image file. It is necessary to know the type of the file when you are processing the images. Java has ImageReader which stores the type of image file. One image can have more that one readers associated with that file.

If you look at the below example, I have read a JPEG file and then printing the image format using the ImageIO classes.

ImageFormatExample.java

[code lang=”java”]
package javabeat.net.core;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;

import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;

/**
* Get Image Format Example
*
* @author Krishna
*
*/
public class ImageFormatExample {
public static void main(String[] args) throws IOException {

//Create Image File
File imageFile = new File("Desert.jpg");

//Create ImageInputStream using Image File
ImageInputStream imageInputStream = ImageIO.createImageInputStream(imageFile);

//Get the image readers for that file
Iterator<ImageReader> imageReadersList = ImageIO.getImageReaders(imageInputStream);

if (!imageReadersList.hasNext()) {
throw new RuntimeException("Image Readers Not Found!!!");
}

//Get the image type
ImageReader reader = imageReadersList.next();
System.out.println("Image Format: " + reader.getFormatName());

//Close stream (best practice)
imageInputStream.close();

}
}
[/code]

Output…

[code]
Image Format: JPEG
[/code]

Filed Under: Java Tagged With: Java ImageIO

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved