Base64 converted to pictures and pictures into Base64 code.

public class base64Change {
    /**
     * @param imgStr base64Coded string*@param path   Image path - specific to file*/
    public static boolean generateImage(String imgStr, String path) {
        if (imgStr == null)
            return false;
        BASE64Decoder decoder = new BASE64Decoder();
        try {
// Decrypt
            byte[] b = decoder.decodeBuffer(imgStr);
// Processing data
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {
                    b[i] += 256;
                }
            }
            OutputStream out = new FileOutputStream(path);
            out.write(b);
            out.flush();
            out.close();
            return true;
        } catch (Exception e) {
            return false;
        }
    //Picture into Base64 string
    public static String GetImageStr()  
    {//The image file is converted to byte array string and processed by Base64 encoding.
        String imgFile = "F:\\tupian\\a.jpg";//Picture to be processed// The address is also written in the form of "F:/deskBG/86619-107.jpg".
        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();  
        }  
        //Byte array Base64 encoding
        BASE64Encoder encoder = new BASE64Encoder();  
        return encoder.encode(data);//Returns Base64 encoded byte array strings.
    }

}
public String ChangeBase64(String base64data,Integer certifiedtype,Integer userid,HttpServletRequest request) {
    //This step is very important, important and important, because the data of Base64 will have data:base64img.//All need to be removed after the transformation, otherwise it is a blank document that can not be opened.
    String base64img = certifieddata.substring(base64data.indexOf(",")+1);

    FileUploadUtils.generateImage(base64img,"F:/deskBG/86619-107.jpg");
    //return null;For testing, actual needs are determined.
    return null;
  }

 

Leave a Reply

Your email address will not be published. Required fields are marked *