Java Multi-Resolution Image API: Simplify Image Handling Across Resolutions

Explore the Java Multi-Resolution Image API, introduced in Java 9, designed to manage images in multiple resolution variants seamlessly. This API allows developers to treat different image resolutions as a single multi-resolution image, enhancing flexibility for varied screen sizes and display contexts.



Java - Multiresolution Image API

Java 9 introduced the Multi-resolution Image API to handle images with different resolution variants. This API allows a set of images with different resolutions to be treated as a single multi-resolution image, making it easier to work with image variants for different screen sizes or contexts.

Concept

Consider the following images:

  • mini_logo.png
  • logo.png
  • large_logo.png

These are three images of a logo in different sizes. From Java 9 onward, the Multi-resolution Image API allows you to treat these images as one logical image and retrieve specific variants based on their resolution requirements.

Key Classes

The following classes are essential for working with multi-resolution images in Java:

  • MultiResolutionImage
  • BaseMultiResolutionImage

Both classes are part of the java.awt.image package.

Major Operations

  • Image getResolutionVariant(double destImageWidth, double destImageHeight) − Returns the best image variant for a specified size.
  • List<Image> getResolutionVariants() − Returns a list of all available resolution variants.

Example 1: Get All Variants

In this example, three images are loaded and stored as a MultiResolutionImage. The getResolutionVariants() method is used to print all available image variants.

Get All Variants

package com.tutorialsarena;

import java.awt.Image;
import java.awt.image.BaseMultiResolutionImage;
import java.awt.image.MultiResolutionImage;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;

public class Tester {
public static void main(String[] args) throws IOException {
    // List of URLs of the images
    List imgUrls = List.of(
        "http://www.tutorialsarena.com/java9/images/logo.png",
        "http://www.tutorialsarena.com/java9/images/mini_logo.png",
        "http://www.tutorialsarena.com/java9/images/large_logo.png");

    // Create a list of Image objects
    List images = new ArrayList<>();

    // Load images from URLs
    for (String url : imgUrls) {
        images.add(ImageIO.read(new URL(url)));
    }

    // Create a multi-resolution image from the images
    MultiResolutionImage multiResolutionImage = 
        new BaseMultiResolutionImage(images.toArray(new Image[0]));

    // Get all image variants
    List variants = multiResolutionImage.getResolutionVariants();

    // Print the number of variants and their details
    System.out.println("Total number of images: " + variants.size());
    for (Image img : variants) {
        System.out.println(img);
    }
}
}
Output

Total number of images: 3
BufferedImage@7ce6a65d: type = 6 ColorModel: #pixelBits = 32 numComponents = 4 
color space = java.awt.color.ICC_ColorSpace@548ad73b transparency = 3 
has alpha = true isAlphaPre = false ByteInterleavedRaster: width = 311 
height = 89 #numDataElements 4 dataOff[0] = 3

BufferedImage@4c762604: type = 6 ColorModel: #pixelBits = 32 numComponents = 4 
color space = java.awt.color.ICC_ColorSpace@548ad73b transparency = 3 
has alpha = true isAlphaPre = false ByteInterleavedRaster: width = 156 
height = 45 #numDataElements 4 dataOff[0] = 3

BufferedImage@2641e737: type = 6 ColorModel: #pixelBits = 32 numComponents = 4 
color space = java.awt.color.ICC_ColorSpace@548ad73b transparency = 3 
has alpha = true isAlphaPre = false ByteInterleavedRaster: width = 622 
height = 178 #numDataElements 4 dataOff[0] = 3

Example 2: Get Specific Variant

In this example, three images are loaded and stored in MultiResolutionImage. Using the getResolutionVariant() method, specific image variants are retrieved based on the provided resolution, and the details are printed. If the exact resolution is not found, the closest match is returned.

Get Specific Variant

package com.tutorialsarena;

import java.awt.Image;
import java.awt.image.BaseMultiResolutionImage;
import java.awt.image.MultiResolutionImage;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;

public class Tester {
public static void main(String[] args) throws IOException {
    // List of URLs of the images
    List imgUrls = List.of(
        "http://www.tutorialsarena.com/java9/images/logo.png",
        "http://www.tutorialsarena.com/java9/images/mini_logo.png",
        "http://www.tutorialsarena.com/java9/images/large_logo.png");

    // Create a list of Image objects
    List images = new ArrayList<>();

    // Load images from URLs
    for (String url : imgUrls) {
        images.add(ImageIO.read(new URL(url)));
    }

    // Create a multi-resolution image from the images
    MultiResolutionImage multiResolutionImage = 
        new BaseMultiResolutionImage(images.toArray(new Image[0]));

    // Get and print resolution-specific image variants
    Image variant1 = multiResolutionImage.getResolutionVariant(156, 45);
    System.out.printf("\nImage for destination[%d,%d]: [%d,%d]", 
        156, 45, variant1.getWidth(null), variant1.getHeight(null));

    Image variant2 = multiResolutionImage.getResolutionVariant(311, 89);
    System.out.printf("\nImage for destination[%d,%d]: [%d,%d]", 311, 89, 
        variant2.getWidth(null), variant2.getHeight(null));

    Image variant3 = multiResolutionImage.getResolutionVariant(622, 178);
    System.out.printf("\nImage for destination[%d,%d]: [%d,%d]", 622, 178, 
        variant3.getWidth(null), variant3.getHeight(null));

    Image variant4 = multiResolutionImage.getResolutionVariant(300, 300);
    System.out.printf("\nImage for destination[%d,%d]: [%d,%d]", 300, 300, 
        variant4.getWidth(null), variant4.getHeight(null));
}
}
Output

Total number of images: 3
Image for destination[156,45]: [311,89]
Image for destination[311,89]: [311,89]
Image for destination[622,178]: [622,178]
Image for destination[300,300]: [622,178]

Conclusion

The Multi-resolution Image API simplifies working with images of varying resolutions by treating them as a single logical entity. This approach helps in adapting images for different display sizes without manually managing each resolution variant.