1 / 11

Java 語言 語法簡介 2014/9/18

Java 語言 語法簡介 2014/9/18. 1. 灰階影像儲存. 灰階影像 大小為 W*H pixels 寬 W=640 高 H=480 Java 語言語法: 灰階影像像素若為 8 bits/pixel. 640 pixels. 480 pixels. int W=640; int H=480 ; int image[][] = new int [ H ][ W ];. 以原圖大小的40%顯示. 2 . 彩色影像儲存. RGB 彩色影像 大小為 W*H pixels 寬 W=200 高 H=300 維度 dim=3

lydie
Download Presentation

Java 語言 語法簡介 2014/9/18

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java語言 語法簡介 2014/9/18

  2. 1. 灰階影像儲存 • 灰階影像 • 大小為W*H pixels • 寬W=640 • 高 H=480 • Java 語言語法: • 灰階影像像素若為 8 bits/pixel 640 pixels 480 pixels int W=640; int H=480 ; int image[][] = new int[H][W]; 以原圖大小的40%顯示

  3. 2. 彩色影像儲存 • RGB彩色影像 • 大小為W*H pixels • 寬W=200 • 高H=300 • 維度 dim=3 • Java 語言語法: 200 pixels 300 pixels static int W=200; static int H=300 ; static int dim=3; static int image[][] = new int[H][W][dim];

  4. 3. 開啟檔案 • 影像檔案的輸入與輸出必須先『開啟檔案』後,才能進行輸入與輸出的動作 • Java語言語法: • 宣告檔案指標 import java.io.*; • 讀取現有的檔案 • 寫入檔案 FileInputStream fi = new FileInputStream("lenna512.raw"); FileOutputStream fo = new FileOutputStream("out.raw");

  5. 4. 輸入影像 • 輸入大小為W*H的灰階影像 • 像素的儲存順序是依照由左而右,由上而下的順序。也就是一列一列循序儲存。 • Java語言語法: • fi:輸入的檔案指標 for(int i=0;i<H;i++) { for(int j=0;j<W;j++) { image[i][j]= fi.read(); } } 記得是依照 一列一列的 順序來讀入

  6. 4. 輸入影像 • 輸入大小為W*H的彩色影像 int W=512; int H=512; int dim=3; int image[][][] = new int[H][W][dim]; FileInputStream fi = new FileInputStream("lenna512c.raw"); for(int i=0;i<H;i++) { for(int j=0;j<W;j++) { for(int k=0;k<dim;k++) { image[i][j][k]= fi.read(); } } }

  7. 5. 輸出影像 • 輸出大小為W*H的灰階影像 • fo:輸出的檔案指標 • image[i][j]:個別像素值 FileOutputStream fo = new FileOutputStream("out.raw"); for(int i=0;i<H;i++) { for(int j=0;j<W;j++) { fo.write(image[i][j]); } }

  8. 5. 輸出影像 • 輸出大小為W*H的RGB彩色影像 • fo:輸出的檔案指標 • image[i][j][k]:個別像素值 FileOutputStream fo = new FileOutputStream("outc.raw"); for(int i=0;i<H;i++) { for(int j=0;j<W;j++) { for(int k=0;k<dim;k++) { fo.write(image[i][j][k]); } } }

  9. 6. 由鍵盤輸入資料 • 輸入整數值 System.out.println(“請輸入一個整數)”); Scanner sc = new Scanner(System.in); int choice=sc.nextInt();

  10. 6. 由鍵盤輸入資料 • 輸入字串 static String fin_name; System.out.print("\n請輸入灰階影像名稱: "); Scanner sci = new Scanner(System.in); fin_name=sci.next(); FileInputStream fi = new FileInputStream(fin_name);

  11. 7. 格式化輸出 • 輸出固定位數的整數值 • 八位元灰階像素值範圍為0, 1, …, 255 import java.text.DecimalFormat; DecimalFormat decf = new DecimalFormat("000"); for(int i=0;i<H;i++) { for(int j=0;j<W;j++) { System.out.print(" " + decf.format(image[i][j])); } }

More Related