用 Java 将 EPUB 转换为 JPG

EPUB 是最广泛支持的电子书格式。EPUB 文件是一个 ZIP 压缩包,实际上包含一个网站,包括 HTML 文件、图像、CSS 样式表和其他资产。当你想在 PowerPoint 演示文稿中包含 EPUB 文件或通过电子邮件发送 EPUB 文件时,将 EPUB 转换为 JPG 会很有帮助。或者,例如,你想与没有安装 EPUB 阅读器的人共享 EPUB 文件。

Aspose.HTML for Java 库提供多种 EPUB 图像格式转换,如 JPGPNGBMPTIFFGIF

将 EPUB 转换为任何支持的图像格式时,必须遵循以下步骤:

  • 打开 EPUB 文件
  • 创建 SaveOptions 对象。
  • 使用 Converter 类将 EPUB 转换为所选图像格式。

唯一的区别是使用 ImageSaveOptions 类指定输出图像格式。ImageSaveOptions(format) 构造函数用指定的图像格式初始化选项对象。可以将格式设置为 JPG、PNG、BMP、GIF 或 TIFF。默认的 ImageFormat 为 PNG。

在本文中,您将了解如何使用Aspose.HTML for Java将EPUB转换为JPG,以及如何应用 ImageSaveOptions。您可以轻松使用Java示例将EPUB转换为JPG,这里详细介绍了如何将 EPUB转换为PNG、EPUB转换为BMP、EPUB转换为GIF和EPUB转换为TIFF。只需将 ImageFormat设置为 ImageSaveOptions即可!

通过几行 Java 代码将 EPUB 转换为 JPG

Converter 类的静态方法主要用于将 EPUB 文件转换为各种格式的最简单方法。只需几行代码,您就可以在 Java 应用程序中将 EPUB 转换为 JPG 格式!

1// Convert EPUB to JPG using Java
2
3// Open an existing EPUB file for reading
4java.io.FileInputStream fileInputStream = new java.io.FileInputStream("input.epub");
5
6// Invoke the convertEPUB() method to convert EPUB to JPG
7Converter.convertEPUB(fileInputStream, new ImageSaveOptions(ImageFormat.Jpeg), "convert-in-two-lines.jpg");

将 EPUB 转换为 JPG

让我们通过一个简单的EPUB到JPG转换方案的逐步说明:

  1. 打开现有的 EPUB 文件。在示例中,代码初始化了 FileInputStream,以字节流的形式读取 EPUB 文件的内容。
  2. 使用 JPG ImageFormat 创建 ImageSaveOptions 的实例。将使用 ImageFormat.Png 作为默认图像格式。
  3. 使用 Converter 类的 convertEPUB(stream, options, savePath) 方法将 EPUB 保存为 JPG 图像。该方法将 streamoptionssavePath 作为参数,并执行转换。

使用 convertEPUB()方法是将 EPUB 文件转换为各种格式的最常见方法。下面的 Java 代码片段显示了如何将 EPUB 转换为 JPG:

 1// Convert EPUB to JPEG using Aspose.HTML for Java
 2
 3// Open an existing EPUB file for reading
 4java.io.FileInputStream fileInputStream = new java.io.FileInputStream("input.epub");
 5
 6// Create an instance of the ImageSaveOptions class
 7ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Jpeg);
 8
 9// Call the сonvertEPUB() method to convert EPUB to JPG
10Converter.convertEPUB(fileInputStream, options, "input-output.jpg");

您可以从 GitHub 下载完整的示例和数据文件。

ImageSaveOptions 类

Aspose.HTML for Java 可使用 ImageSaveOptions 以编程方式将 EPUB 转换为 JPG,并可完全控制各种转换选项。该功能允许用户通过设置 image format、页面大小、页边距、 compression levelmedia type和其他参数来定制渲染过程。

MethodDescription
setCompression(value)Sets the Tagged Image File Format (TIFF) Compression. By default this property is Compression.LZW.
getCssGets a CssOptions object which is used for configuration of CSS properties processing.
setFormat(value)Sets ImageFormat (JPG, PNG, BMP, TIFF, or GIF). By default this property is ImageFormat.Png.
setBackgroundColor(value)Sets Color which will fill background of every page. Default value is Color.Transparent(Color.getTransparent()).
setPageSetup(value)Gets a page setup object is used for configuration output page-set.
setHorizontalResolution(value)Sets horizontal resolution for output images in pixels per inch. The default value is 300 dpi.
setVerticalResolution(value)Sets vertical resolution for output images in pixels per inch. The default value is 300 dpi.
setSmoothingMode(value)Sets the rendering quality for this image.
getText()Gets a TextOptions object which is used for configuration of text rendering.

有关如何使用 ImageSaveOptions 自定义转换过程的详细信息,请参阅 Fine-Tuning Converters 一文。

通过指定 ImageSaveOptions 将 EPUB 转换为 JPG 格式

Aspose.HTML for Java 允许使用默认或自定义保存选项将 EPUB 转换为 JPG。下面的 Java 示例展示了如何使用 “ImageSaveOptions”(图像保存选项)并创建具有自定义页面大小、页边距、分辨率、渲染质量和背景颜色的 JPG 文件:

  1. 打开现有的 EPUB 文件。初始化 FileInputStream,以字节流的形式读取 EPUB 文件的内容。
  2. 使用 JPG ImageFormat 创建一个新的 ImageSaveOptions 对象,并指定所需的保存选项:
    • Use the setSmoothingMode() method to set the quality of the image smoothing to high.
    • Use the setHorizontalResolution() and setVerticalResolution() methods to set the horizontal and vertical resolution of the image to 400.
    • Use the getBackgroundColor() method to set the background color of the image to AliceBlue.
    • Use the setAnyPage() method to set the size of the image and margins.
  3. 使用 convertEPUB(stream, options, savePath) 方法将 EPUB 文件保存为 JPG 图像。该方法接收 streamoptions、输出文件路径 savePath 并执行转换操作。
 1// Convert EPUB to JPG in Java with custom settings
 2
 3// Open an existing EPUB file for reading
 4java.io.FileInputStream fileInputStream = new java.io.FileInputStream("input.epub");
 5
 6// Initialize ImageSaveOptions
 7ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Jpeg);
 8options.setSmoothingMode(SmoothingMode.HighQuality);
 9options.setHorizontalResolution(Resolution.to_Resolution(400));
10options.setVerticalResolution(Resolution.to_Resolution(400));
11options.setBackgroundColor(Color.getAliceBlue());
12options.getPageSetup().setAnyPage(new Page(new Size(800, 500), new Margin(30, 20, 10, 10)));
13
14// Convert EPUB to JPG
15Converter.convertEPUB(fileInputStream, options, "input-options.jpg");

结论

Aspose.HTML for Java 允许用户轻松地将电子图书转换为常用的 JPG 图像格式。使用 ImageSaveOptions类,您可以自定义输出,从分辨率到背景颜色和页面布局,确保生成的图像符合您的特定要求。所提供的 Java 示例演示了基本和高级转换功能的用法,使开发人员能够实现各种场景和用例。

Aspose.HTML提供免费的在线 EPUB到JPG转换器,可将EPUB转换为高质量的JPG,简单快捷。只需上传、转换您的文件并在几秒钟内得到结果!

文本 “EPUB 到 JPG 转换器”

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.