用Java显示图片(包括BMP(位图)等)

    事实上,对JAVA6.0,它支持得图象格式有[color=red][BMP, bmp, jpg, JPG, wbmp, jpeg, png, PNG, JPEG, WBMP, GIF, gif][/color]
下面是一段列出JAVA支持的图片格式的代码:

import javax.imageio.ImageIO;
import java.util.Arrays;
/**
*列出Java支持的图片格式
*需要JRE1.5或以后
*/
public class SupportedImage{
    public static void main(String[] args){
        System.out.println("支持写的图片格式:" +Arrays.toString(ImageIO.getWriterFormatNames()));
        System.out.println("支持读的图片格式:" +Arrays.toString(ImageIO.getReaderFormatNames()));
    }
}


下面是一个加载现示bmp图象的代码:
import javax.imageio.ImageIO;
import java.awt.*;
import java.io.*;
import java.util.Arrays;
import javax.swing.*;
/**
*加载现示BMP图象,需要JDK1.5或以上
*@author Eatsun
*/
public class ShowBmp extends JFrame{
    public ShowBmp(String bmpFile){
        super("ShowBmp");
        Image image =null;
        try{
            image=ImageIO.read(new File(bmpFile));
        }catch(IOException ex){
        }
        JLabel label =new JLabel(new ImageIcon(image));
        add(label);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        pack();
    }
    public static void main(String[] args){
        final String fileName ="/home/guo/Desktop/02.bmp"; //把这个改成你自己的bmp图片的路径
       
        SwingUtilities.invokeLater(new Runnable(){
            public void run(){
                new ShowBmp(fileName).setVisible(true);
            }
        });
    }
}

你可能感兴趣的:(java,常用源码,java,import,image,string,class,jdk)