当前位置:网站首页>Convert local pictures to Base64 encoding

Convert local pictures to Base64 encoding

2022-04-23 22:06:00 Juicy and flavorful

Import required packages :

import java.io.*;
import java.util.Base64;

Use Java Convert to according to the address where the picture is saved locally Base64 code  

    /**
     *  Convert picture to Base64 code 
     * @param imgFile  Images to be processed 
     * @return
     */
    public static String ImgToBase64(String imgFile){
        // Convert image file to byte array string , And carry out Base64 Encoding processing 
        InputStream in = null;
        byte[] data = null;
        // Read image byte array 
        try
        {
            in = new FileInputStream(imgFile);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        return new String(Base64.getEncoder().encode(data));
    }

版权声明
本文为[Juicy and flavorful]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/113/202204232159112679.html