import cn.hutool.core.img.ImgUtil; import com.czxy.tanhua.AppServiceApplication; import com.czxy.tanhua.autoconfig.template.AipTemplate; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import javax.annotation.Resource; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; /** * @Description * @create 2023-03-31 9:55 */ @RunWith(SpringRunner.class) @SpringBootTest(classes = AppServiceApplication.class) public class Aiptest { @Test public void Test() throws IOException { //本地图片 String path = "D:\\jihe\\tupian\\sdf.png"; //转换流 BufferedImage read = ImageIO.read(new File(path)); //转换base64 String jpeg = ImgUtil.toBase64(read, "jpeg"); System.out.println(jpeg); } } |
将本地图片转换Base64传输
最新推荐文章于 2024-06-19 11:06:32 发布
该代码示例展示了如何在Java中利用Hutool库将本地路径的PNG图片读取为BufferedImage,然后将其转换为JPEG格式的BASE64字符串。主要涉及的类包括ImgUtil、ImageIO和File。
摘要由CSDN通过智能技术生成