Java by API/javax.imageio/ImageIO

Материал из Java эксперт
Перейти к: навигация, поиск

ImageIO: createImageInputStream(Object input)

  
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
public class Main {
  public static void main(String[] argv) throws Exception {
    File file = new File("image.gif");
    System.out.println(getFormatName(file));
    InputStream is = new FileInputStream(file);
    is.close();
    System.out.println(getFormatName(is));
  }
  private static String getFormatName(Object o) {
    try {
      ImageInputStream iis = ImageIO.createImageInputStream(o);
      Iterator iter = ImageIO.getImageReaders(iis);
      if (!iter.hasNext()) {
        return null;
      }
      ImageReader reader = (ImageReader) iter.next();
      iis.close();
      return reader.getFormatName();
    } catch (IOException e) {
    }
    return null;
  }
}





ImageIO: getImageReadersByFormatName(String formatName)

  
import java.io.File;
import java.util.Iterator;
import javax.imageio.ImageIO;
public class Main {
  public static void main(String[] argv) throws Exception {
    boolean b;
    // Check availability using a format name
    b = canReadFormat("foo"); // false
    b = canReadFormat("gif"); // true
    b = canReadFormat("giF"); // true
    }
  // Returns true if the specified format name can be read
  public static boolean canReadFormat(String formatName) {
    Iterator iter = ImageIO.getImageReadersByFormatName(formatName);
    return iter.hasNext();
  }  
}





ImageIO: getImageReadersByMIMEType(String MIMEType)

  
import java.util.Iterator;
import javax.imageio.ImageIO;
public class Main {
  public static void main(String[] argv) throws Exception {
    boolean b;
    b = canReadMimeType("image/jpg"); // false
    b = canReadMimeType("image/jpeg"); // true
  }
  // Returns true if the specified mime type can be read
  public static boolean canReadMimeType(String mimeType) {
    Iterator iter = ImageIO.getImageReadersByMIMEType(mimeType);
    return iter.hasNext();
  }
}





ImageIO: getImageReadersBySuffix(String fileSuffix)

  
import java.io.File;
import java.util.Iterator;
import javax.imageio.ImageIO;
public class Main {
  public static void main(String[] argv) throws Exception {
    boolean b;
    // Check availability using a format name
    b = canReadExtension("foo"); // false
    b = canReadExtension("gif"); // true
    b = canReadExtension("giF"); // true
  }
  // Returns true if the specified file extension can be read
  public static boolean canReadExtension(String fileExt) {
    Iterator iter = ImageIO.getImageReadersBySuffix(fileExt);
    return iter.hasNext();
  }
}





ImageIO: getImageReaders(Object input)

  
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
public class Main {
  public static void main(String[] argv) throws Exception {
    File file = new File("image.gif");
    System.out.println(getFormatName(file));
    InputStream is = new FileInputStream(file);
    is.close();
    System.out.println(getFormatName(is));
  }
  private static String getFormatName(Object o) {
    try {
      ImageInputStream iis = ImageIO.createImageInputStream(o);
      Iterator iter = ImageIO.getImageReaders(iis);
      if (!iter.hasNext()) {
        return null;
      }
      ImageReader reader = (ImageReader) iter.next();
      iis.close();
      return reader.getFormatName();
    } catch (IOException e) {
    }
    return null;
  }
}





ImageIO: getImageWritersByFormatName(String formatName)

  
import java.util.Iterator;
import javax.imageio.ImageIO;
public class Main {
  public static void main(String[] argv) throws Exception {
    boolean b;
    // Check availability using a format name
    b = canWriteFormat("foo"); // false
    b = canWriteFormat("gif"); // true
    b = canWriteFormat("giF"); // true
  }
 
  public static boolean canWriteFormat(String formatName) {
    Iterator iter = ImageIO.getImageWritersByFormatName(formatName);
    return iter.hasNext();
  }
}





ImageIO: getImageWritersByMIMEType(String MIMEType)

  
import java.io.File;
import java.util.Iterator;
import javax.imageio.ImageIO;
public class Main {
  public static void main(String[] argv) throws Exception {
    boolean b;
    b = canWriteMimeType("image/jpg"); // false
    b = canWriteMimeType("image/jpeg"); // true
  }
  // Returns true if the specified mime type can be written
  public static boolean canWriteMimeType(String mimeType) {
    Iterator iter = ImageIO.getImageWritersByMIMEType(mimeType);
    return iter.hasNext();
  }
}





ImageIO: getImageWritersBySuffix(String fileSuffix)

  
import java.util.Iterator;
import javax.imageio.ImageIO;
public class Main {
  public static void main(String[] argv) throws Exception {
    boolean b;
    // Check availability using a format name
    b = canWriteExtension("foo"); // false
    b = canWriteExtension("gif"); // true
    b = canWriteExtension("giF"); // true
  }
  // Returns true if the specified file extension can be written
  public static boolean canWriteExtension(String fileExt) {
    Iterator iter = ImageIO.getImageWritersBySuffix(fileExt);
    return iter.hasNext();
  }
}





ImageIO: getReaderFormatNames()

  

/*
reader BMP
reader bmp
reader jpeg
reader wbmp
reader gif
reader png
reader JPG
reader jpg
reader WBMP
reader JPEG
writer BMP
writer bmp
writer jpeg
writer wbmp
writer png
writer JPG
writer PNG
writer jpg
writer WBMP
writer JPEG
*/
import javax.imageio.ImageIO;
public class Main {
  static public void main(String args[]) throws Exception {
    String names[] = ImageIO.getReaderFormatNames();
    for (int i = 0; i < names.length; ++i) {
      System.out.println("reader " + names[i]);
    }
    names = ImageIO.getWriterFormatNames();
    for (int i = 0; i < names.length; ++i) {
      System.out.println("writer " + names[i]);
    }
  }
}





ImageIO: getReaderMIMETypes()

  
import javax.imageio.ImageIO;
public class Main {
  public static void main(String[] args) {
    String[] readers, writers;
    System.out.println("For Reading:");
    readers = ImageIO.getReaderFormatNames();
    System.out.println("\tBy format:");
    for (int i = 0; i < readers.length; i++)
      System.out.println("\t\t" + readers[i]);
    readers = ImageIO.getReaderMIMETypes();
    System.out.println("\tBy MIME Types:");
    for (int i = 0; i < readers.length; i++)
      System.out.println("\t\t" + readers[i]);
    System.out.println("For Writing:");
    writers = ImageIO.getWriterFormatNames();
    System.out.println("\tBy format:");
    for (int i = 0; i < writers.length; i++)
      System.out.println("\t\t" + writers[i]);
    writers = ImageIO.getWriterMIMETypes();
    System.out.println("\tBy MIME Types:");
    for (int i = 0; i < writers.length; i++)
      System.out.println("\t\t" + writers[i]);
  }
}





ImageIO: getWriterFormatNames()

  

/*
reader BMP
reader bmp
reader jpeg
reader wbmp
reader gif
reader png
reader JPG
reader jpg
reader WBMP
reader JPEG
writer BMP
writer bmp
writer jpeg
writer wbmp
writer png
writer JPG
writer PNG
writer jpg
writer WBMP
writer JPEG
*/
import javax.imageio.ImageIO;
public class Main {
  static public void main(String args[]) throws Exception {
    String names[] = ImageIO.getReaderFormatNames();
    for (int i = 0; i < names.length; ++i) {
      System.out.println("reader " + names[i]);
    }
    names = ImageIO.getWriterFormatNames();
    for (int i = 0; i < names.length; ++i) {
      System.out.println("writer " + names[i]);
    }
  }
}





ImageIO: getWriterMIMETypes()

  
import javax.imageio.ImageIO;
public class Main {
  public static void main(String[] args) {
    String[] readers, writers;
    System.out.println("For Reading:");
    readers = ImageIO.getReaderFormatNames();
    System.out.println("\tBy format:");
    for (int i = 0; i < readers.length; i++)
      System.out.println("\t\t" + readers[i]);
    readers = ImageIO.getReaderMIMETypes();
    System.out.println("\tBy MIME Types:");
    for (int i = 0; i < readers.length; i++)
      System.out.println("\t\t" + readers[i]);
    System.out.println("For Writing:");
    writers = ImageIO.getWriterFormatNames();
    System.out.println("\tBy format:");
    for (int i = 0; i < writers.length; i++)
      System.out.println("\t\t" + writers[i]);
    writers = ImageIO.getWriterMIMETypes();
    System.out.println("\tBy MIME Types:");
    for (int i = 0; i < writers.length; i++)
      System.out.println("\t\t" + writers[i]);
  }
}





ImageIO: read(File input)

  
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
public class Main extends JFrame {
  private BufferedImage bi;
  public static void main(String[] args) {
      new Main ().setVisible(true);
  }
  public Main () {
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    try {
      bi = ImageIO.read(new File("your.jpg"));
    } catch (Exception e) {
      e.printStackTrace();
    }
    setSize(bi.getWidth(), bi.getHeight());
  }
  public void paint(Graphics g) {
    g.drawImage(bi, 0, 0, getWidth(), getHeight(), this);
  }
}





ImageIO: read(URL input)

  

import java.awt.BorderLayout;
import java.awt.Image;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Main {
  public static void main(String[] argv) throws Exception {
    // Read from a URL
    URL url = new URL("http://java.org/source.gif");
    Image image = ImageIO.read(url);
    JFrame frame = new JFrame();
    JLabel label = new JLabel(new ImageIcon(image));
    frame.getContentPane().add(label, BorderLayout.CENTER);
    frame.pack();
    frame.setVisible(true);
  }
}





ImageIO: write(RenderedImage im, String formatName, File output)

  

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
public class Main {
  final static int WIDTH = 50;
  final static int HEIGHT = 50;
  final static int NUM_ITER = 1500;
  public static void main(String[] args) throws Exception {
    BufferedImage bi;
    bi = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
    Graphics g = bi.getGraphics();
    for (int i = 0; i < NUM_ITER; i++) {
      g.setColor(Color.RED);
      g.drawLine(1, 2, i, i + 1);
    }
    g.dispose();
    ImageIO.write(bi, "gif", new File("image.gif"));
  }
}