All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.dahuatech.icc.multiinone.utils.ImageUtils Maven / Gradle / Ivy

There is a newer version: 1.0.13.7
Show 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;
        }

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy