集册 Java实例教程 从文件、InputStream或URL读取图像

从文件、InputStream或URL读取图像

欢马劈雪     最近更新时间:2020-01-02 10:19:05

433
从文件,InputStream或URL读取图像

import java.awt.BorderLayout;

import java.awt.Image;
/** from 
时 代      J a v a   公   众 号 - nowjava.com**/

import java.io.BufferedInputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStream;

import java.net.URL;


import javax.imageio.ImageIO;

import javax.swing.ImageIcon;

import javax.swing.JFrame;

import javax.swing.JLabel;

/*
 from 时代Java公众号 
*/

public class Main {

  public static void main(String[] args) throws Exception {

    Image image = null;

    try {

      // Read from a file

      File file = new File("image.gif");

      image = ImageIO.read(file);


      // Read from an input stream

      InputStream is = new BufferedInputStream(new FileInputStream("image.gif"));

      image = ImageIO.read(is);


      // Read from a URL

      URL url = new URL("http://hostname.com/image.gif");

      image = ImageIO.read(url);

    } catch (IOException e) {

    }


    
展开阅读全文