Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1501575
  • 博文数量: 3500
  • 博客积分: 6000
  • 博客等级: 准将
  • 技术积分: 43870
  • 用 户 组: 普通用户
  • 注册时间: 2008-05-03 20:31
文章分类

全部博文(3500)

文章存档

2008年(3500)

我的朋友

分类:

2008-05-04 19:23:09

一起学习
下面的这个例子示范怎样使用javax.imageio包来从一个文件、输入流或URL获取图象资源,同时也示范了如何来把Image显示到屏幕上. javax.imageio包默认支持GIF,PNG和JPEG格式的图片,这个例子只能工作在J2SE1.4或以上版本
 

    Image image = null;

    try {

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

        image = ImageIO.read(file);

 

        InputStream is = new BufferedInputStream(

            new FileInputStream("image.gif"));

        image = ImageIO.read(is);

    

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

        image = ImageIO.read(url);

    } catch (IOException e) {

    }

    

    JFrame frame = new JFrame();

    JLabel label = new JLabel(new ImageIcon(image));

    frame.getContentPane().add(label, BorderLayout.CENTER);

    frame.pack();

    frame.setVisible(true);

包javax.imageio默认可以读和写一个GIF,PNG和JPEG图片,你可以调用ImageIO.getReaderFormatNames() 和ImageIO.getWriterFormatNames()来列出全部可读和可写的图片格式
 

    String[] formatNames = ImageIO.getReaderFormatNames();

    formatNames = unique(formatNames);

    

    formatNames = ImageIO.getWriterFormatNames();

    formatNames = unique(formatNames);

    // png jpeg jpg

    



    formatNames = ImageIO.getReaderMIMETypes();

    formatNames = unique(formatNames);

    // image/jpeg image/png image/x-png image/gif

    

    formatNames = ImageIO.getWriterMIMETypes();

    formatNames = unique(formatNames);

    // image/jpeg image/png image/x-png

    



    public static String[] unique(String[] strings) {

        Set set = new HashSet();

        for (int i=0; i
下载本文示例代码


使用J2SE1.4新特性来获取图象资源使用J2SE1.4新特性来获取图象资源使用J2SE1.4新特性来获取图象资源使用J2SE1.4新特性来获取图象资源使用J2SE1.4新特性来获取图象资源使用J2SE1.4新特性来获取图象资源使用J2SE1.4新特性来获取图象资源使用J2SE1.4新特性来获取图象资源使用J2SE1.4新特性来获取图象资源使用J2SE1.4新特性来获取图象资源使用J2SE1.4新特性来获取图象资源使用J2SE1.4新特性来获取图象资源
阅读(143) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~