|
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import java.util.*;
import java.util.List;
import javax.imageio.*;
import javax.imageio.stream.*;
import javax.swing.*;
public class ImageIOTest{
public static void main(String args[]){
ImageIOTest panel = new ImageIOTest();
}
}
class panel extends JPanel{
public panel(){
JFrame frame = new JFrame();
frame.setTitle("态势显示");
frame.setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
setBounds(100,100,700,600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(this);
frame.setVisible(true);
JMenu fileMenu = new JMenu("文件");
JMenuItem openItem = new JMenuItem("打开");
openItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
openFile();
}
});
fileMenu.add(openItem);
JMenu saveMenu = new JMenu("保存为");
fileMenu.add(saveMenu);
Iterator <String> iter = writerFormats.iterator();
while(iter.hasNext()){
final String formatName = iter.next();
JMenuItem formatItem = new JMenuItem(formatName);
saveMenu.add(formatItem);
formatItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
saveFile(formatName);
}
});
}
JMenuItem exitItem = new JMenuItem("退出");
exitItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
System.exit(0);
}
});
fileMenu.add(exitItem);
JMenuBar menuBar = new JMenuBar();
menuBar.add(fileMenu);
frame.setJMenuBar(menuBar);
}
public void openFile(){
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
chooser.setFileFilter(new javax.swing.filechooser.FileFilter(){
public boolean accept(File f){
if(f.isDirectory()) return true;
String name = f.getName();
int p = name.lastIndexOf('.');
if(p==-1) return false;
String suffix = name.substring(p+1).toLowerCase();
return readerSuffixes.contains(suffix);
}
public String getDescription(){
return "Image files";
}
});
int r= chooser.showOpenDialog(this);
if(r!=JFileChooser.APPROVE_OPTION) return;
File f = chooser.getSelectedFile();
Box box = Box.createVerticalBox();
try{
String name = f.getName();
String suffix = name.substring(name.lastIndexOf('.')+1);
Iterator<ImageReader> iter = ImageIO.getImageReadersBySuffix(suffix);
ImageReader reader = iter.next();
ImageInputStream imageIn = ImageIO.createImageInputStream(f);
reader.setInput(imageIn);
int count = reader.getNumImages(true);
images = new BufferedImage[count];
for(int i = 0;i<count;i++){
images[i] = reader.read(i);
box.add(new JLabel(new ImageIcon(images[i])));
}
}
catch(IOException e){
JOptionPane.showMessageDialog(this,e);
}
setContentPane(new JScrollPane(box));
validate();
}
public void saveFile(final String formatName){
if(images==null) return;
Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName(formatName);
ImageWriter writer = iter.next();
final List<String> writerSuffixes = Arrays.asList(writer.getOriginatingProvider().getFileSuffixes());
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
chooser.setFileFilter(new javax.swing.filechooser.FileFilter(){
public boolean accept(File f){
if(f.isDirectory()) return true;
String name = f.getName();
int p = name.lastIndexOf('.');
if(p==-1) return false;
String suffix = name.substring(p+1).toLowerCase();
return readerSuffixes.contains(suffix);
}
public String getDescription(){
return formatName+"files";
}
});
int r= chooser.showOpenDialog(this);
if(r!=JFileChooser.APPROVE_OPTION) return;
File f = chooser.getSelectedFile();
try{
ImageOutputStream imageOut = ImageIO.createImageOutputStream(f);
writer.setOutput(imageOut);
writer.write(new IIOImage(images[0],null,null));
for(int i=1;i<images.length;i++){
IIOImage iioImage = new IIOImage(images[i],null,null);
if(writer.canInsertImage(i))
writer.writeInsert(i,iioImage,null);
}
}
catch(IOException e){
JOptionPane.showMessageDialog(this,e);
}
}
public static Set<String> getReaderSuffixes(){
TreeSet<String> readerSuffixes = new TreeSet<String>();
for(String name:ImageIO.getReaderFormatNames()){
Iterator<ImageReader> iter = ImageIO.getImageReadersByFormatName(name);
while(iter.hasNext()){
ImageReader reader = iter.next();
String[] s = reader.getOriginatingProvider().getFileSuffixes();
readerSuffixes.addAll(Arrays.asList(s));
}
}
return readerSuffixes;
}
public static Set<String> getWriterFormats(){
TreeSet<String> writerFormats = new TreeSet<String>();
TreeSet<String> formatNames = new TreeSet<String>(Arrays.asList(ImageIO.getWriterFormatNames()));
while(formatNames.size()>0){
String name = formatNames.iterator().next();
Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName(name);
ImageWriter writer = iter.next();
String[] names = writer.getOriginatingProvider().getFormatNames();
writerFormats.add(names[0]);
formatNames.removeAll(Arrays.asList(names));
}
return writerFormats;
}
private BufferedImage[] images;
private static final int DEFAULT_WIDTH = 1000;
private static final int DEFAULT_HEIGHT = 800;
private static Set<String> readerSuffixes = getReaderSuffixes();
private static Set<String> writerFormats = getWriterFormats();
} |
|