这是一个使用 Java 将图片转换为 Base64 编码的示例代码:

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Base64;

public class ImageToBase64 {
    public static void main(String[] args) {
        String imagePath = "path/to/image.jpg"; // 图片的路径

        try {
            // 读取图片文件
            InputStream inputStream = new FileInputStream(imagePath);
            byte[] imageBytes = new byte[inputStream.available()];
            inputStream.read(imageBytes);
            inputStream.close();

            // 将图片字节流转换为 Base64 编码
            String base64Image = Base64.getEncoder().encodeToString(imageBytes);

            System.out.println(base64Image);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

请将代码中的 path/to/image.jpg 替换为你实际的图片路径。运行代码后,将会输出图片的 Base64 编码字符串。

Java 图片转 Base64 编码:简单示例与代码 - 轻松实现图片数据转换

原文地址: https://www.cveoy.top/t/topic/pJBC 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录