如果你对图像处理感兴趣,而且需要使用gif、jpeg和png以外的其它图像格式,或者希望改善jpeg图像处理的性能但不知道到哪里寻找适当的方法,或者需要通过几何运算(包括非线性变换)来处理图像,不必再为此苦恼了,答案就在这里――来自sun公司的java高级图像处理api和jai图像i/o api 1.0 rc。
jai api是java media api的一部分,与之相伴的还包括java 2d api、java 3d api、java speech api和其他一些api。java高级图像处理api是作为java规范请求(jsp)34的一部分而开发的,是对j2se version 1.3+版的扩展,主要用于处理图像。最初发布的版本是1.0,jdc(java developer connection)提供了一个预览版1.1.2 beta。(最新进展情况请查阅readme.html文件。)与awt和java 2d相比,jai api提供了更丰富的图像处理,包括对许多通用图像操作的内在支持。
不过本文的目的不是讨论jai api,而是伴随这些api但分离到它自己的可安装库中的一组图像读写器(codec)类,即java高级图像处理图像i/o工具1.0 rc。该rc提供了可以插接到j2se 1.4的图像i/o框架上的一些功能。作为jsr-15一部分而开发的图像i/o api提供了一个支持不同图像格式的可插拔框架。标准j2se 1.4版本身支持gif、jpeg和png图像格式,而jai图像i/o rc则提供了更多主流图像格式的编码解码器。只要加上针对操作平台的适当版本,以前开发的应用程序就可以处理这些新的图像格式。
要理解jai图像i/o工具的使用,需要首先了解图像i/o库。在安装和介绍图像i/o工具包之前,我们先看一看图像i/o库。
图像i/o库
图像i/o库是j2se 1.4的标准api,放在javax.imageio包内。虽然这个包提供了两个接口和9个类,整个api实际上就是imageio类。通过这个类可以弄清读写所支持的图像格式并对这些图像进行读写,实际上这也就是整个api的全部内容。
由于图像i/o库是一个可插拔的框架,所支持的图像格式集不是固定不变的。尽管随j2se 1.4发布了一些标准格式,但任何人都可以增加新的支持格式。要查看有哪些格式可用,可以使用下面的代码:
import javax.imageio.*;import java.util.arrays;
public class getformats { public static void main(string args[]) { string readformats[] = imageio.getreadermimetypes(); string writeformats[] = imageio.getwritermimetypes(); system.out.println("readers: " + arrays.aslist(readformats)); system.out.println("writers: " + arrays.aslist(writeformats)); }}
运行该程序,你会发现这个库支持读取gif、jpeg和png图像,也支持写jpeg和png图像,但是不支持写gif文件。
除了与像image/jpeg这样的mime类型协同工作外,imageio类还允许通过getreaderformatnames和getwriterformatnames方法使用jpeg这样的非正式名称。此外,通过getimagereadersbysuffix和getimagewritersbysuffix还可以了解是否存在针对特定文件扩展名的reader/writer存在。
利用imageio类,你所要做的事情不过是读javax.imageio.stream.imageinputstream、java.io.inputstream、java.io.file或者java.net.url,结果会得到一个java.awt.image.bufferedimage。一旦拥有了bufferedimage,你就可以指定需要的格式名把图像写回去。(不仅仅是bufferimage,任何实现renderedimage接口的类都可以写。)新的格式既可以与读取的格式相同,也可以是不同的格式以便进行格式转换。如果指定的格式没有可用的writer,那么write方法就返回false,否则如果找到了相应的writer就返回true。
string inputfilename = ...;bufferedimage image = imageio.read(inputfilename);...string formatname = "jpg"; // desired formatstring outputfilename = ...;file outputfile = new file(outputfilename);boolean writerexists = imageio.write(image,formatname, outputfile);
为了说明图像i/o库的用法,下面的例子使用jfilechooser提示输入图像文件名。选中文件后再选择目标输出格式,然后按下“save(保存)”按钮。保存完成后,将重新读取图像并在一个新窗口内显示。
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import java.io.*;
import java.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后,还可以重新运行该程序,你将会看到更多的存储格式。读取其它格式的图像基本上无需改变代码也能工作,用户只要选择不同的文件类型就可以了。
闽公网安备 35060202000074号