高级图像处理图像I/O API RC 1.0(2)
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import java.io.*;
import java.net.*;[来源www.iocblog.net]
import javax.imageio.*;
public class converting extends jframe { jlabel promptlabel; jtextfield prompt; jbutton promptbutton; jfilechooser filechooser; jcombobox combobox;? jbutton savebutton;? public converting() { super("image conversion"); setdefaultcloseoperation(exit_on_close); container contentpane = getcontentpane(); jpanel inputpanel = new jpanel(); promptlabel = new jlabel("filename:"); inputpanel.add(promptlabel); prompt = new jtextfield(20); inputpanel.add(prompt); promptbutton = new jbutton("browse"); inputpanel.add(promptbutton); contentpane.add(inputpanel, borderlayout.north);
filechooser = new jfilechooser(); promptbutton.addactionlistener( new actionlistener() { public void actionperformed(actionevent e) { int returnvalue = filechooser.showopendialog(null); if (returnvalue == jfilechooser.approve_option) { file selectedfile = filechooser.getselectedfile(); if (selectedfile != null) { prompt.settext(selectedfile.getabsolutepath()); } } } } );
jpanel outputpanel = new jpanel(); string writerformats[] = imageio.getwriterformatnames(); comboboxmodel comboboxmodel = new defaultcomboboxmodel(writerformats); combobox = new jcombobox(comboboxmodel); outputpanel.add(combobox); savebutton = new jbutton("save"); outputpanel.add(savebutton); savebutton.addactionlistener( new actionlistener() { public void actionperformed(actionevent e) { try { string name = prompt.gettext(); file file = new file(name); if (file.exists()) { bufferedimage image = imageio.read(file.tourl()); if (image == null) { system.err.println("invalid input file format"); } else { string selection = (string)combobox.getselecteditem(); string outputfilename = name + "." + selection; file outputfile = new file(outputfilename); boolean found = imageio.write(image, selection, outputfile); if (found) { jdialog window = new jdialog(); container windowcontent = window.getcontentpane(); bufferedimage newimage = imageio.read(outputfile); jlabel label = new jlabel(new imageicon(newimage)); jscrollpane pane = new jscrollpane(label); windowcontent.add(pane, borderlayout.center); window.setsize(300, 300); window.show(); } else { system.err.println("error saving"); } } } else { system.err.println("bad filename"); } } catch (malformedurlexception mur) { system.err.println("bad filename"); } catch (ioexception ioe) { system.err.println("error reading file"); } } } );
contentpane.add(outputpanel, borderlayout.south);
} public static void main(string args[]) { jframe frame = new converting(); frame.pack(); frame.show(); }}
注意,该程序没有硬编码任何文件类型,而是询问图像i/o框架支持哪些文件类型。安装java高级图像处理图像i/o工具rc后,还可以重新运行该程序,你将会看到更多的存储格式。读取其它格式的图像基本上无需改变代码也能工作,用户只要选择不同的文件类型就可以了。
文章整理:iocblog
版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。