English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Java flow control

Java array

Java object-oriented (I)

Java object-oriented (II)

Java object-oriented (III)

Java Exception Handling

Java List

Java Queue (queue)

Java Map collection

Java Set collection

Java input/output (I/O)

Java Reader/Writer

Java other topics

Java 9 Multi-resolution image API

Java 9 New Features

Java 9 Define the multi-resolution image API, developers can easily operate and display images of different resolutions.

The following are the main operation methods of multi-resolution images:

  • Image getResolutionVariant(double destImageWidth, double destImageHeight) − Get a specific resolution image variant-Represents a logical image of a specific size with known resolution units of DPI and is the best variant.

  • List<Image> getResolutionVariants() − Returns a list of readable resolution image variants.

import java.io.IOException;
import java.net.URL;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;
import java.awt.Image;
import java.awt.image.MultiResolutionImage;
import java.awt.image.BaseMultiResolutionImage;
 
import javax.imageio.ImageIO;
 
public class Tester {
   public static void main(String[] args) throws IOException, MalformedURLException {
 
      List<String> imgUrls = List.of("http://www.oldtoolbag.com/wp-content/themes/w3codebox/assets/img/[email protected]",
         "http://www.oldtoolbag.com/wp-content/themes/w3codebox/assets/img/w3codebox-logo.png",
         "http://www.oldtoolbag.com/wp-content/themes/w3codebox/assets/images/qrcode.png"
 
      List<Image> images = new ArrayList<Image>();
 
      for (String url : imgUrls) {
         images.add(ImageIO.read(new URL(url)));
      }
 
      // Read all images
      MultiResolutionImage multiResolutionImage = 
         new BaseMultiResolutionImage(images.toArray(new Image[0]));
 
      // Get all the resolutions of the image
      List<Image> variants = multiResolutionImage.getResolutionVariants();
 
      System.out.println("Total number of images: ", + variants.size());
 
      for (Image img : variants) {
         System.out.println(img);
      }
 
      // Get the corresponding image resolution for different sizes
      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));
   }  
}

Java 9 New Features