IT源码网

用JAVA读取图片的三种方式

sanshao 2021年04月03日 程序员 373 0
import java.net.*;//for URL 
import java.io.*;//for catch (IOException e),File,InputStream, BufferedInputStream,and FileInputStream ect 
public class HelloJava{ 
   public static void main (String[] args){ 
   Image image = null; 
    try { 
        // Read from a file 
        File sourceimage = new File("source.gif");  //source.gif图片要与HelloJava.java同在一目录下 
        image = ImageIO.read(sourceimage); 
     
        // Read from an input stream 
        InputStream is = new BufferedInputStream( 
           new FileInputStream("mid.jpg"));  //mid.jpg图片要与HelloJava.java同在一目录下 
        image = ImageIO.read(is); 
     
        // Read from a URL 
        URL url = new URL("http://www.javaworld.com/images/012407-tipsbox.jpg"); 
        image = ImageIO.read(url); 
    } catch (IOException e) { 
    } 
     
} 

 

评论关闭
IT源码网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!