package com.bonus.tcp.util;

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

public class ImageToBase64Converter {

    public static void main(String[] args) {

    }

    /**
     * 将给定路径的图像转换为 Base64 编码字符串
     *
     * @param imagePath 图像文件的路径
     * @return Base64 编码的字符串
     * @throws IOException 如果读取文件时发生错误
     */
    public static String convertImageToBase64(String imagePath) throws IOException {
        File imageFile = new File(imagePath);
        byte[] fileContent = new byte[(int) imageFile.length()];

        try (FileInputStream fileInputStream = new FileInputStream(imageFile)) {
            fileInputStream.read(fileContent);
        }

        return "data:image/jpeg;base64,"+Base64.getEncoder().encodeToString(fileContent);
    }
}