| 高级图像处理图像I/O API RC 1.0 |
|
|
|
|
| 来源: 作者:John Zukowski 添加日期:2006-6-11 8:57:14 点击次数: |
|
String inputFilename = ...; BufferedImage image = ImageIO.read(inputFilename); ... String formatName = "jpg"; // desired format String 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); |
|
| |