ImageIO

說明

對一組檔案套用一連串的影像操作。

使用 AWT 和 ImageIO;取代 Java 9+ 的 image 工作。此工作也可以用於 Java 8,請參閱參數表格以了解限制。

注意:此工作會盡量維持與 image 工作的語法和語意一致。不過,它使用 format 屬性,而不是 encoding 屬性,因為後者是一個錯誤的名稱:幾乎所有工作都使用類似的屬性來設定檔案、檔名或其他字串的字元編碼。此外,當未指定 format 時,其值會由第一個處理的影像檔案格式來定義。

使用資料類型的概觀
ImageOperation class diagram A diagram of Ant DataType classes used by ImageIO task. org.apache.tools.ant.types.DataType ImageOperation instructions : List addDraw(Draw : instr) addRotate(Rotate : instr) addScale(Scale : instr) The setType() method forces type to one of the values of java.awt.geom.Arc2D: open = Arc2D.OPEN pie = Arc2D.PIE chord = Arc2D.CHORD Parameter is not case-sensitive. BasicShape height : int = 0 width : int = 0 strokeWidth : int = 0 stroke : String = "black" fill : String = "transparent" TransformOperation executeTransformOperation(BufferedImage img) : BufferedImage <<interface>> DrawOperation executeDrawOperation() : BufferedImage The implementing class uses ColorMapper to evaluate the color. Only the values defined in ColorMapper are used. ColorMapper COLOR_BLACK : String = "black" COLOR_BLUE : String = "blue" COLOR_CYAN : String = "cyan" COLOR_DARKGRAY : String = "darkgray" COLOR_GRAY : String = "gray" COLOR_LIGHTGRAY : String = "lightgray" COLOR_DARKGREY : String = "darkgrey" COLOR_GREY : String = "grey" COLOR_LIGHTGREY : String = "lightgrey" COLOR_GREEN : String = "green" COLOR_MAGENTA : String = "magenta" COLOR_ORANGE : String = "orange" COLOR_PINK : String = "pink" COLOR_RED : String = "red" COLOR_WHITE : String = "white" COLOR_YELLOW : String = "yellow" Text string : String = "" font : String = "Arial" point : int = 10 bold : boolean = false color : String = "black" italic : boolean = false Rotate angle : float = 0.0F Scale width : String = "100%" height : String = "100%" keepProportions : boolean = false Draw xloc : int = 0 yloc : int = 0 addText(Text : text) addRectangle(Rectangle rect) addEllipse(Ellipse elip) addArc(Arc arc) Rectangle archeight : int = 0 arcwidth : int = 0 Ellipse Arc start : int = 0 stop : int = 0 type : enumerated = open

參數

屬性 說明 必要
failonerror 布林值。如果為 false,會將錯誤記錄到輸出,但會繼續執行。 否;預設為 true
srcdir 包含影像的目錄。 是,除非使用巢狀檔案集
format 影像格式。
有效值(不區分大小寫):bmpgifjpegjpgpngtiftiffwbmp(Java 8 VM 不支援 TIFF,可以透過 外部函式庫 來提供支援)。
否;預設為(第一個)原始檔案的格式
overwrite 布林值。設定是否要覆寫檔案(如果發生命名衝突)。 否;預設為 false
gc 布林值。在處理完每張影像後啟用垃圾回收。 否;預設為 false
destdir 儲存結果影像的目錄。 否;預設為 srcdir 的值
includes 必須包含的檔案模式清單(以逗號或空格分隔)。 否;預設為全部 (**)
includesfile 檔案名稱。此檔案的每一行都被視為包含模式
excludes 必須排除的檔案模式清單(以逗號或空格分隔)。 否;預設為預設排除或無(如果 defaultexcludesno
excludesfile 檔案名稱。此檔案的每一行都被視為排除模式
defaultexcludes 表示是否要使用預設排除 (yes|no)。 否;預設為 yes
caseSensitive 布林值。設定檔案系統的大小寫敏感性。 否;預設為 false
followSymlinks 布林值。設定是否要追蹤符號連結。 否;預設為 true

以巢狀元素指定參數

此工作會形成一個隱含的 FileSet,並支援 <fileset> 的大多數屬性,以及巢狀的 <include><exclude><patternset> 元素。

下列 ImageOperation 物件可指定為巢狀元素:RotateScaleDraw

Rotate

將 Rotate ImageOperation 加入鏈結。

參數
屬性 說明 必要
angle 浮點值。設定旋轉角度(以度為單位)。 否;預設為 0.0F

Scale

將 Scale ImageOperation 加入鏈結。

參數
屬性 說明 必要
proportions 設定要控制比例的尺寸。有效值為
  • ignore—獨立處理尺寸。
  • height—根據寬度維持比例。
  • width—根據高度維持比例。
  • cover—維持比例並符合提供的尺寸。
  • fit—維持比例並覆蓋提供的尺寸。
否;預設為 ignore
width 設定影像寬度,可以是整數(畫素)或 %。 否;預設為 100%
height 設定影像高度,可以是整數(畫素)或 %。 否;預設為 100%

Draw

將 Draw ImageOperation 加入鏈結。DrawOperation DataType 物件可巢狀在 Draw 物件內。

參數
屬性 說明 必要
xloc 繪製巢狀影像元素的 X 位置。 否;預設為 0
yloc 繪製巢狀影像元素的 Y 位置。 否;預設為 0
巢狀元素

Text 和 BasicShape 物件都可以巢狀。目前支援的 BasicShape 物件有 Arc、Ellipse 和 Rectangle。

BasicShape 物件的共用參數
屬性 說明 必要
height BasicShape 的高度。 否;預設為 0
width BasicShape 的寬度。 否;預設為 0
strokewidth BasicShape 的線條寬度。 否;預設為 0
color BasicShape 的顏色。 否;預設為 black
fill BasicShape 的填滿。 否;預設為 transparent
Arc 物件的特定參數
屬性 說明 必要
start 圓弧的起始角度(以度為單位)。 否;預設為 0
stop 圓弧的角度範圍(以度為單位)。 否;預設為 0
type 下列其中一種:chordopenpie 否;預設為 open
Ellipse 物件的特定參數

無。

Rectangle 物件的特定參數
屬性 說明 必要
archeight 矩形角落圓弧的垂直直徑。 否;預設為 0
arcwidth 矩形角落圓弧的水平直徑。 否;預設為 0
Text 的參數
屬性 說明 必要
string 文字字串。 否;預設為
font 設定文字字型的字型。 否;預設為 Arial
point 字型的點數大小。 否;預設為 10
粗體 字型是否為粗體。 否;預設為 false
color 文字的顏色。 否;預設為 black
斜體 字型是否為斜體。 否;預設為 false

對應器

你可以透過使用巢狀的 對應器 元素來定義檔案名稱轉換。<image> 所使用的預設對應器為 身分對應器

你也可以使用 filenamemapper 類型來取代 mapper 元素。

範例

建立我的圖片縮圖,並確保它們都符合 160×160 像素的大小,無論圖片是直幅或橫幅。

<image destdir="samples/low" overwrite="yes">
    <fileset dir="samples/full">
        <include name="**/*.jpg"/>
    </fileset>
    <scale width="160" height="160" proportions="fit"/>
</image>

src 中建立所有 PNG 檔案的縮圖,大小為 40 像素,並保持比例,並儲存 src

<image srcdir="src" includes="*.png">
    <scale proportions="width" width="40"/>
</image>

與上述相同,但將結果儲存在 dest 中。

<image srcdir="src" destdir="dest" includes="*.png">
    <scale proportions="width" width="40"/>
</image>

與上述相同,但將結果儲存在檔案中,原始檔名加上前綴 scaled-

<image srcdir="src" destdir="dest" includes="*.png">
    <scale proportions="width" width="40"/>
    <globmapper from="*" to="scaled-*"/>
</image>