Java BufferedImage from String decodeToImage(String imageString, String pathFile)

Here you can find the source of decodeToImage(String imageString, String pathFile)

Description

Decode string to image

License

Apache License

Parameter

Parameter Description
imageString The string to decode

Exception

Parameter Description
IOException an exception

Return

decoded image.

Declaration

public static BufferedImage decodeToImage(String imageString, String pathFile) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.IOException;
import java.io.ByteArrayInputStream;

import java.io.File;

import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import org.apache.commons.codec.binary.Base64;

public class Main {
    /**//w  w w.  ja v  a2  s  . co  m
     * Decode string to image
     * @param imageString The string to decode
     * @return decoded image.
     * @throws IOException 
     */
    public static BufferedImage decodeToImage(String imageString, String pathFile) throws IOException {
        BufferedImage image = null;
        byte[] imageByte;
        try {
            imageByte = Base64.decodeBase64(imageString);
            ByteArrayInputStream bis = new ByteArrayInputStream(imageByte);
            image = ImageIO.read(bis);
            bis.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (image != null) {
            ImageIO.write(image, "jpg", new File(pathFile));
        }
        return image;
    }
}

Related

  1. base64ToImg(String base64Strings)
  2. decodeImage(String encoded)
  3. decodeImage(String imageString)
  4. decodeToImage(String imageString)
  5. decodeToImage(String imageString)