在这个简单易用的教程中,您将了解如何使用 Java REST API 将 BMP 转换为 WEBP。 BMP(或位图)是一种存储位图数字图像的图像文件格式。它广泛用于 Windows 操作系统,是标准的图像格式。 BMP 文件通常是未压缩的,包含文件头、位图信息头和原始图像数据。 BMP 文件格式能够存储各种尺寸、分辨率和颜色深度的二维数字图像。但是,WEBP 是由 Google 开发的一种图像文件格式,它使用有损和无损压缩来减小文件大小而不牺牲图像质量。大多数现代网络浏览器都支持它,并用于在网络上显示图像。 WEBP 提供优于其他图像格式(例如 JPEG、PNG 和 GIF)的压缩,从而使文件大小更小,质量损失最小。如果您需要在 Java 低代码 API 中将 BMP 转换为 WEBP,则可以借助此示例代码来完成。
先决条件
- 创建帐户并获取 API 凭据
- 下载 Aspose.Imaging Cloud SDK for Java
- 使用上述 SDK 设置 Java 项目
在 Java REST API 中将 BMP 转换为 WEBP 的步骤
- 为 API 设置客户端 ID 和客户端密码
- 使用客户端凭据创建 ImagingAPI 类的实例
- 指定输入和输出文件
- 读取输入的BMP文件并上传到云存储
- 使用输入和输出文件格式创建 ConvertImageRequest 实例
- 调用 convertImage 方法以使用 REST API 将 BMP 转换为 WEBP
- 将输出的 WEBP 文件保存在本地磁盘上
Java Low Code API 中 BMP 到 WEBP 转换的代码
String ClientID = Client.getID(); //replace Client.getID() with your own client ID here | |
String ClientSecret = Client.getSecret(); //replace3 Client.getSecret() with your own client secret here | |
String APIBaseUrl="https://api.aspose.cloud"; | |
String Local_Path = "C:/Temp/"; | |
ImagingApi imagingApiSdk = new ImagingApi(ClientSecret, ClientID, APIBaseUrl); | |
// Input & output file names | |
String inputFileName = "BMPtoWEBP.bmp"; | |
String outputFileName = "BMPtoWEBP.webp"; | |
try { | |
// Upload local image to Cloud Storage | |
File inputFile = new File(Local_Path + inputFileName); | |
FileInputStream inputFileStream = new FileInputStream(inputFile); | |
byte[] inputImageData = IOUtils.toByteArray(inputFileStream); | |
UploadFileRequest uploadFileRequest = new UploadFileRequest(inputFileName, inputImageData, null); | |
FilesUploadResult filesUploadResult = imagingApiSdk.uploadFile(uploadFileRequest); | |
String outputFormat = "webp"; | |
String remoteFolder = null; // Input file is saved at the root of the storage | |
String remoteStorage = null; // Cloud Storage name | |
ConvertImageRequest convertImageRequest = new ConvertImageRequest(inputFileName, outputFormat, remoteFolder, remoteStorage); | |
byte[] convertedImageData = imagingApiSdk.convertImage(convertImageRequest); | |
// Save exported image to local storage | |
FileOutputStream fileOutputStream = new FileOutputStream(Local_Path + outputFileName); | |
fileOutputStream.write(convertedImageData); | |
fileOutputStream.close(); | |
} catch (Exception e) { | |
System.out.println(e.getMessage()); | |
} |
上面共享的示例代码可帮助您使用 Java REST API 将 BMP 转换为 WEBP。您只需在 Aspose.Imaging REST API SDK 的帮助下输入 BMP 文件并下载输出 WEBP 文件以将其保存在本地。
此 BMP 到 WEBP 的转换功能可以与任何平台上的任何无代码或低代码应用程序一起使用。
在以下主题中可以找到类似的功能:如何使用 Java REST API 将 BMP 转换为 JPG