国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

高級圖像處理圖像I/OAPIRC1.0

2019-11-18 11:13:22
字體:
來源:轉載
供稿:網友

  假如你對圖像處理感愛好,而且需要使用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.  運行該程序,你會發現這個庫支持讀取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");    setDefaultClo
SEOperation(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后,還可以重新運行該程序,你將會看到更多的存儲格式。讀取其它格式的圖像基本上無需改變代碼也能工作,用戶只要選擇不同的文件類型就可以了。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 镇安县| 锦州市| 佛坪县| 民勤县| 芦山县| 那坡县| 沽源县| 中西区| 容城县| 上思县| 莱州市| 怀远县| 南汇区| 绥宁县| 会理县| 博湖县| 丹寨县| 米泉市| 江陵县| 淄博市| 齐河县| 新干县| 怀安县| 汾阳市| 温宿县| 德阳市| 白沙| 双江| 二连浩特市| 遂溪县| 镇巴县| 沙洋县| 东源县| 上思县| 凌源市| 西藏| 集安市| 安乡县| 巴青县| 珲春市| 社旗县|