com.dahuatech.icc.multiinone.utils.ImageUtils Maven / Gradle / Ivy
The newest version!
package com.dahuatech.icc.multiinone.utils;
import com.dahuatech.hutool.log.Log;
import com.dahuatech.hutool.log.LogFactory;
import com.dahuatech.icc.util.Base64Util;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
public class ImageUtils {
public static final String BASE64_IMG_PREFIX = "data:image/jpeg;base64,";
private static final Log logger = LogFactory.get();
public static String convertToBase64Img(InputStream inputStream) {
try{
byte[] bytes = new byte[inputStream.available()];
inputStream.read(bytes);
inputStream.close();
return Base64Util.encode(bytes);
}catch (Exception e){
logger.error("图片转base64异常,{}",e,e.getMessage());
return null;
}
}
public static String convertToBase64Img(File file) {
try{
InputStream inputStream = new FileInputStream(file);
return convertToBase64Img(inputStream);
}catch (Exception e){
logger.error("图片转base64异常,{}",e,e.getMessage());
return null;
}
}
}