TiffFrame
Inheritance: java.lang.Object, com.aspose.imaging.DisposableObject, com.aspose.imaging.DataStreamSupporter, com.aspose.imaging.Image, com.aspose.imaging.RasterImage, com.aspose.imaging.RasterCachedImage
public final class TiffFrame extends RasterCachedImage
TIFF 帧。
构造函数
| 构造函数 | 描述 |
|---|---|
| TiffFrame(InputStream stream) | 初始化 TiffFrame 类的新实例。 |
| TiffFrame(InputStream stream, TiffOptions options) | 初始化 TiffFrame 类的新实例。 |
| TiffFrame(String path) | 初始化 TiffFrame 类的新实例。 |
| TiffFrame(String path, TiffOptions options) | 初始化 TiffFrame 类的新实例。 |
| TiffFrame(RasterImage image) | 初始化 TiffFrame 类的新实例。 |
| TiffFrame(RasterImage image, TiffOptions options) | 初始化 TiffFrame 类的新实例。 |
| TiffFrame(TiffOptions options, int width, int height) | 初始化 TiffFrame 类的新实例。 |
方法
Example: This example shows how to create a TIFF image from scratch and save it to a file.
String dir = "c:\\temp\\";
com.aspose.imaging.imageoptions.TiffOptions createOptions =
new com.aspose.imaging.imageoptions.TiffOptions(com.aspose.imaging.fileformats.tiff.enums.TiffExpectedFormat.Default);
// 为每个颜色分量设置 8 位。
createOptions.setBitsPerSample(new int[]{8, 8, 8});
// 设置大端字节序(Motorola)
createOptions.setByteOrder(com.aspose.imaging.fileformats.tiff.enums.TiffByteOrder.BigEndian);
// 设置 LZW 压缩。
createOptions.setCompression(com.aspose.imaging.fileformats.tiff.enums.TiffCompressions.Lzw);
// 设置 RGB 颜色模型。
createOptions.setPhotometric(com.aspose.imaging.fileformats.tiff.enums.TiffPhotometrics.Rgb);
// 所有颜色分量将存储在单个平面中。
createOptions.setPlanarConfiguration(com.aspose.imaging.fileformats.tiff.enums.TiffPlanarConfigs.Contiguous);
// 创建一个 100x100 像素的 TIFF 帧。
// 请注意,如果帧已包含在 TiffImage 中,则无需显式释放帧。
// 当容器被释放时,所有帧将自动被释放。
com.aspose.imaging.fileformats.tiff.TiffFrame firstFrame = new com.aspose.imaging.fileformats.tiff.TiffFrame(createOptions, 100, 100);
// 用蓝黄渐变填充整个帧。
com.aspose.imaging.brushes.LinearGradientBrush gradientBrush = new com.aspose.imaging.brushes.LinearGradientBrush(
new com.aspose.imaging.Point(0, 0),
new com.aspose.imaging.Point(firstFrame.getWidth(), firstFrame.getHeight()),
com.aspose.imaging.Color.getBlue(),
com.aspose.imaging.Color.getYellow());
com.aspose.imaging.Graphics graphics = new com.aspose.imaging.Graphics(firstFrame);
graphics.fillRectangle(gradientBrush, firstFrame.getBounds());
// 创建 TIFF 图像。
com.aspose.imaging.fileformats.tiff.TiffImage tiffImage = new com.aspose.imaging.fileformats.tiff.TiffImage(firstFrame);
try {
tiffImage.save(dir + "output.tif");
} finally {
tiffImage.dispose();
}
TiffFrame(InputStream stream)
public TiffFrame(InputStream stream)
初始化 TiffFrame 类的新实例。
Parameters:
| 参数 | 类型 | 描述 |
|---|---|---|
| 流 | java.io.InputStream | 用于加载图像并初始化帧像素和调色板数据的流。 |
TiffFrame(InputStream stream, TiffOptions options)
public TiffFrame(InputStream stream, TiffOptions options)
初始化 TiffFrame 类的新实例。
Parameters:
| 参数 | 类型 | 描述 |
|---|---|---|
| 流 | java.io.InputStream | 用于加载图像并初始化帧像素和调色板数据的流。 |
| options | TiffOptions | 用于新创建帧的选项。 |
TiffFrame(String path)
public TiffFrame(String path)
初始化 TiffFrame 类的新实例。
Parameters:
| 参数 | 类型 | 描述 |
|---|---|---|
| 路径 | java.lang.String | 用于加载图像并初始化帧像素和调色板数据的路径。 |
TiffFrame(String path, TiffOptions options)
public TiffFrame(String path, TiffOptions options)
初始化 TiffFrame 类的新实例。
Parameters:
| 参数 | 类型 | 描述 |
|---|---|---|
| 路径 | java.lang.String | 用于加载图像并初始化帧像素和调色板数据的路径。 |
| options | TiffOptions | 用于新创建帧的选项。 |
TiffFrame(RasterImage image)
public TiffFrame(RasterImage image)
初始化 TiffFrame 类的新实例。
Parameters:
| 参数 | 类型 | 描述 |
|---|---|---|
| image | RasterImage | 用于初始化帧像素和调色板数据的图像。 |
TiffFrame(RasterImage image, TiffOptions options)
public TiffFrame(RasterImage image, TiffOptions options)
初始化 TiffFrame 类的新实例。
Parameters:
| 参数 | 类型 | 描述 |
|---|---|---|
| image | RasterImage | 用于初始化帧像素和调色板数据的图像。 |
| options | TiffOptions | 用于新创建帧的选项。 |
TiffFrame(TiffOptions options, int width, int height)
public TiffFrame(TiffOptions options, int width, int height)
初始化 TiffFrame 类的新实例。
Parameters:
| 参数 | 类型 | 描述 |
|---|---|---|
| options | TiffOptions | 帧选项。 |
| 宽度 | int | 宽度。 |
| 高度 | int | 高度。 |
getBackgroundColor()
public Color getBackgroundColor()
获取背景颜色的值。
Returns: Color
setBackgroundColor(Color value)
public void setBackgroundColor(Color value)
设置背景颜色的值。
Parameters:
| 参数 | 类型 | 描述 |
|---|---|---|
| value | Color |
hasAlpha()
public boolean hasAlpha()
获取一个值,指示此实例是否具有 alpha 通道。
Returns:
boolean - 如果此实例具有 alpha,则为 true;否则为 false。
Example: The following example loads a TIFF image and prints information about raw data format and alpha channel.
String dir = "c:\\temp\\";
String fileName = dir + "sample.tif";
com.aspose.imaging.Image image = com.aspose.imaging.Image.load(fileName);
try {
com.aspose.imaging.fileformats.tiff.TiffImage tiffImage = (com.aspose.imaging.fileformats.tiff.TiffImage) image;
// 如果活动 TIFF 帧具有 alpha 通道,则整个 TIFF 图像被视为具有 alpha 通道。
System.out.printf("ImageFile=%s, FileFormat=%s, HasAlpha=%s\r\n", fileName, tiffImage.getRawDataFormat(), tiffImage.hasAlpha());
int i = 0;
for (com.aspose.imaging.fileformats.tiff.TiffFrame frame : tiffImage.getFrames()) {
System.out.printf("Frame=%s, FileFormat=%s, HasAlpha=%s\r\n", ++i, frame.getRawDataFormat(), frame.hasAlpha());
}
} finally {
image.dispose();
}
// 输出可能如下所示:
// ImageFile=c:\temp\sample.tif, FileFormat=RgbIndexed1Bpp, used channels: 1, HasAlpha=False
// Frame=1, FileFormat=RgbIndexed1Bpp, used channels: 1, HasAlpha=False
// Frame=2, FileFormat=RgbIndexed1Bpp, used channels: 1, HasAlpha=False
getBitsPerPixel()
public int getBitsPerPixel()
获取图像每像素位数计数。
Returns: int - 图像每像素位数。
getFrameOptions()
public TiffOptions getFrameOptions()
获取帧创建选项。
Returns: TiffOptions
getHeight()
public int getHeight()
获取图像高度。
Returns: int - 图像高度。
getWidth()
public int getWidth()
获取图像宽度。
Returns: int - 图像宽度。
getHorizontalResolution()
public double getHorizontalResolution()
获取此 RasterImage 的水平分辨率(每英寸像素数)。
Returns: double - 水平分辨率。
Example: The following example shows how to set horizontal/vertical resolution of a separate TIFF frame.
String dir = "c:\\temp\\";
// 从文件加载 TIFF 图像。
com.aspose.imaging.Image image = com.aspose.imaging.Image.load(dir + "sample.tif");
try {
com.aspose.imaging.fileformats.tiff.TiffImage tiffImage = (com.aspose.imaging.fileformats.tiff.TiffImage) image;
int i = 0;
for (com.aspose.imaging.fileformats.tiff.TiffFrame frame : tiffImage.getFrames()) {
// 获取 TiffFrame 的水平和垂直分辨率。
double horizontalResolution = frame.getHorizontalResolution();
double verticalResolution = frame.getVerticalResolution();
System.out.printf("The horizontal resolution of frame %s, pixels per inch: %s\r\n", i, horizontalResolution);
System.out.printf("The vertical resolution, of frame %s, pixels per inch: %s\r\n", i, verticalResolution);
if (horizontalResolution != 96.0 || verticalResolution != 96.0) {
// 使用 SetResolution 方法在一次调用中更新两个分辨率值。
System.out.println("Set resolution values to 96 dpi");
frame.setResolution(96.0, 96.0);
System.out.printf("The horizontal resolution of frame %s, pixels per inch: %s\r\n", i, horizontalResolution);
System.out.printf("The vertical resolution, of frame %s, pixels per inch: %s\r\n", i, verticalResolution);
}
++i;
}
} finally {
image.dispose();
}
setHorizontalResolution(double value)
public void setHorizontalResolution(double value)
设置此 RasterImage 的水平分辨率(每英寸像素数)。
Parameters:
| 参数 | 类型 | 描述 |
|---|---|---|
| value | double | 水平分辨率。 |
getVerticalResolution()
public double getVerticalResolution()
获取此 RasterImage 的垂直分辨率(每英寸像素数)。
Returns: double - 垂直分辨率。
Example: The following example shows how to set horizontal/vertical resolution of a separate TIFF frame.
String dir = "c:\\temp\\";
// 从文件加载 TIFF 图像。
com.aspose.imaging.Image image = com.aspose.imaging.Image.load(dir + "sample.tif");
try {
com.aspose.imaging.fileformats.tiff.TiffImage tiffImage = (com.aspose.imaging.fileformats.tiff.TiffImage) image;
int i = 0;
for (com.aspose.imaging.fileformats.tiff.TiffFrame frame : tiffImage.getFrames()) {
// 获取 TiffFrame 的水平和垂直分辨率。
double horizontalResolution = frame.getHorizontalResolution();
double verticalResolution = frame.getVerticalResolution();
System.out.printf("The horizontal resolution of frame %s, pixels per inch: %s\r\n", i, horizontalResolution);
System.out.printf("The vertical resolution, of frame %s, pixels per inch: %s\r\n", i, verticalResolution);
if (horizontalResolution != 96.0 || verticalResolution != 96.0) {
// 使用 SetResolution 方法在一次调用中更新两个分辨率值。
System.out.println("Set resolution values to 96 dpi");
frame.setResolution(96.0, 96.0);
System.out.printf("The horizontal resolution of frame %s, pixels per inch: %s\r\n", i, horizontalResolution);
System.out.printf("The vertical resolution, of frame %s, pixels per inch: %s\r\n", i, verticalResolution);
}
++i;
}
} finally {
image.dispose();
}
setVerticalResolution(double value)
public void setVerticalResolution(double value)
设置此 RasterImage 的垂直分辨率(每英寸像素数)。
Parameters:
| 参数 | 类型 | 描述 |
|---|---|---|
| value | double | 垂直分辨率。 |
getPathResources()
public List<PathResource> getPathResources()
获取路径资源。
值:路径资源。
Returns: java.util.List<com.aspose.imaging.fileformats.tiff.pathresources.PathResource> - 路径资源。
Example: The following example shows how to retrieve paths from TIFF image and display their names in the console.
try (TiffImage image = (TiffImage) Image.load("Sample.tif"))
{
for (PathResource path : image.getActiveFrame().getPathResources())
{
System.out.println(path.getName());
}
}
Example: The following example shows how to modify already existing Clipping Paths. 以下示例展示了如何修改已存在的剪裁路径。例如,您可以在图像中仅保留一个剪裁路径。
try (TiffImage image = (TiffImage) Image.load("Sample.tif"))
{
List<PathResource> paths = image.getActiveFrame().getPathResources();
image.getActiveFrame().setPathResources(Collections.singletonList(paths.get(0)));
image.save();
}
Example: Transfer Clipping Paths during export from TIFF to PSD image.
try (Image image = Image.load("Sample.tif"))
{
image.save("SampleWithPaths.psd", new PsdOptions());
}
Example: Create Clipping Path manually.
static void main()
{
try (TiffImage image = (TiffImage)Image.load("Sample.tif"))
{
PathResource res = new PathResource();
res.setBlockId((short) 2000); // Block Id according to Photoshop specification
res.setName("My Clipping Path"); // Path name
res.setRecords(createRecords(0.2f, 0.2f, 0.8f, 0.2f, 0.8f, 0.8f, 0.2f, 0.8f)); // Create path records using coordinates
image.getActiveFrame().setPathResources(Collections.singletonList(res));
image.save("ImageWithPath.tif");
}
}
private static List<VectorPathRecord> createRecords(float ... coordinates)
{
List<VectorPathRecord> records = createBezierRecords(coordinates); // Create Bezier records using coordinates
LengthRecord lr = new LengthRecord(); // LengthRecord required by Photoshop specification
lr.setOpen(false); // Lets create closed path
lr.setRecordCount(records.size()); // Record count in the path
records.add(0, lr);
return records;
}
private static List<VectorPathRecord> createBezierRecords(float[] coordinates)
{
List<VectorPathRecord> l = new LinkedList<VectorPathRecord>();
for (int index = 0; index < coordinates.length - 1; index += 2)
{
PointF pt = new PointF(coordinates[index], coordinates[index + 1]);
BezierKnotRecord br = new BezierKnotRecord();
br.setPathPoints(new PointF[] {pt, pt, pt});
l.add(br);
}
return l;
}
setPathResources(List value)
public void setPathResources(List<PathResource> value)
设置路径资源。
值:路径资源。
Parameters:
| 参数 | 类型 | 描述 |
|---|---|---|
| value | java.util.List<com.aspose.imaging.fileformats.tiff.pathresources.PathResource> | 路径资源。 |
removeMetadata()
public void removeMetadata()
通过将此 IHasXmpData.XmpData(IHasXmpData.getXmpData/IHasXmpData.setXmpData(XmpPacketWrapper)) 和 IHasExifData.ExifData(IHasExifData.getExifData/IHasExifData.setExifData(ExifData) IHasExifData.setExifData)的值设为 null,以删除此图像实例的元数据。
getOriginalOptions()
public ImageOptionsBase getOriginalOptions()
根据原始文件设置获取选项。这有助于保持原始图像的位深度和其他参数不变。例如,如果我们加载一张每像素 1 位的黑白 PNG 图像,然后使用 DataStreamSupporter.save(String) 方法保存,它将生成每像素 8 位的输出 PNG 图像。为避免这种情况并以每像素 1 位保存 PNG 图像,请使用此方法获取相应的保存选项,并将其作为第二个参数传递给 Image.save(String, ImageOptionsBase) 方法。
Returns: ImageOptionsBase - The options based on the original file settings.
alignResolutions()
public void alignResolutions()
帮助方法,使水平和垂直分辨率相等。
copyFrame(TiffFrame tiffFrame)
public static TiffFrame copyFrame(TiffFrame tiffFrame)
复制整个帧(重复)。
Parameters:
| 参数 | 类型 | 描述 |
|---|---|---|
| tiffFrame | TiffFrame | 要复制的 TIFF 帧。 |
Returns: TiffFrame - The newly copied tiff frame.
createFrameFrom(TiffFrame tiffFrame, TiffOptions options)
public static TiffFrame createFrameFrom(TiffFrame tiffFrame, TiffOptions options)
使用指定的 options 从指定的 tiffFrame 创建帧。像素数据被保留,但会转换为所需的格式。
Parameters:
| 参数 | 类型 | 描述 |
|---|---|---|
| tiffFrame | TiffFrame | 要从中创建的 TIFF 帧。 |
| options | TiffOptions | 要使用的新选项。 |
Returns: TiffFrame - The newly created frame.
Example: The following example shows how to create a grayscale copy of an existing frame and add it to a TIFF image.
String dir = "c:\\temp\\";
com.aspose.imaging.imageoptions.TiffOptions createTiffOptions
= new com.aspose.imaging.imageoptions.TiffOptions(com.aspose.imaging.fileformats.tiff.enums.TiffExpectedFormat.Default);
// 创建永久的(非临时的)文件源。
createTiffOptions.setSource(new com.aspose.imaging.sources.FileCreateSource(dir + "multipage.tif", false));
createTiffOptions.setPhotometric(com.aspose.imaging.fileformats.tiff.enums.TiffPhotometrics.Rgb);
createTiffOptions.setBitsPerSample(new int[]{8, 8, 8});
com.aspose.imaging.fileformats.tiff.TiffImage tiffImage = (com.aspose.imaging.fileformats.tiff.TiffImage) com.aspose.imaging.Image.create(createTiffOptions, 100, 100);
try {
// 图像左上角到右下角的线性渐变。
com.aspose.imaging.brushes.LinearGradientBrush brush =
new com.aspose.imaging.brushes.LinearGradientBrush(
new com.aspose.imaging.Point(0, 0),
new com.aspose.imaging.Point(tiffImage.getWidth(), tiffImage.getHeight()),
com.aspose.imaging.Color.getRed(),
com.aspose.imaging.Color.getGreen());
// 使用线性渐变画刷填充活动帧。
com.aspose.imaging.Graphics gr = new com.aspose.imaging.Graphics(tiffImage.getActiveFrame());
gr.fillRectangle(brush, tiffImage.getBounds());
// 灰度选项
com.aspose.imaging.imageoptions.TiffOptions createTiffFrameOptions
= new com.aspose.imaging.imageoptions.TiffOptions(com.aspose.imaging.fileformats.tiff.enums.TiffExpectedFormat.Default);
createTiffFrameOptions.setSource(new com.aspose.imaging.sources.StreamSource(new java.io.ByteArrayInputStream(new byte[0])));
createTiffFrameOptions.setPhotometric(com.aspose.imaging.fileformats.tiff.enums.TiffPhotometrics.MinIsBlack);
createTiffFrameOptions.setBitsPerSample(new int[]{8});
// 创建活动帧的灰度副本。
// 像素数据被保留,但会转换为所需的格式。
com.aspose.imaging.fileformats.tiff.TiffFrame grayscaleFrame
= com.aspose.imaging.fileformats.tiff.TiffFrame.createFrameFrom(tiffImage.getActiveFrame(), createTiffFrameOptions);
// 将新创建的帧添加到 TIFF 图像中。
tiffImage.addFrame(grayscaleFrame);
tiffImage.save();
} finally {
tiffImage.dispose();
}
resize(int newWidth, int newHeight, int resizeType)
public void resize(int newWidth, int newHeight, int resizeType)
调整图像大小。
Parameters:
| 参数 | 类型 | 描述 |
|---|---|---|
| newWidth | int | 新的宽度。 |
| newHeight | int | 新的高度。 |
| resizeType | int | 调整大小类型。 |
rotateFlip(int rotateFlipType)
public void rotateFlip(int rotateFlipType)
旋转、翻转,或同时旋转和翻转图像。
Parameters:
| 参数 | 类型 | 描述 |
|---|---|---|
| rotateFlipType | int | 旋转翻转类型。 |
rotate(float angle, boolean resizeProportionally, Color backgroundColor)
public void rotate(float angle, boolean resizeProportionally, Color backgroundColor)
围绕中心旋转图像。
Parameters:
| 参数 | 类型 | 描述 |
|---|---|---|
| angle | float | 旋转角度,以度为单位。正值将顺时针旋转。 |
| resizeProportionally | boolean | 如果设置为 true,图像尺寸将根据旋转矩形(角点)投影进行更改;否则保持尺寸不变,仅旋转内部图像内容。 |
| backgroundColor | Color | 背景的颜色。 |
crop(Rectangle rectangle)
public void crop(Rectangle rectangle)
裁剪图像。
Parameters:
| 参数 | 类型 | 描述 |
|---|---|---|
| rectangle | Rectangle | 矩形。 |