图片字符工具类ImageUtils

2018-06-26  本文已影响0人  ggr

最近项目涉及到图片跨域问题,需要将图片转换为Base64进行网络传输,下面是抽离出来的图片字符工具类

package com.weijuju.iag.sf.quiz.util;

import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * 图片字符工具类
 */
public class ImageUtils {

    private static Logger LOGGER = LoggerFactory.getLogger(ImageUtils.class);


    public static void main(String[] args) throws MalformedURLException {
  /*    String urlImageInfo = encodeImgageToBase64(new URL("http://127.0.0.1:9002/image/mobile/origin/yd_02.png"));
        String urlImageInfo = encodeImgageToBase64(new URL("http://127.0.0.1:9002/image/mobile/origin/demo.png"));
  */
        String urlImageInfo = encodeImgageToBase64(new File("D:\\hy.png"));
        System.out.println(urlImageInfo);
        decodeBase64ToImage(urlImageInfo,"D:\\test1\\test\\","1.png");

      /*  String urlImageInfo2 = encodeImgageToBase64(new File("D:\\no35.png"));
        System.out.println(urlImageInfo2);
        decodeBase64ToImage(urlImageInfo2,"D:\\","2.png");*/

     /*   String fileImageInfo = encodeImgageToBase64(new File("D:\\yd_02.png"));
        System.out.println(fileImageInfo);
        decodeBase64ToImage(fileImageInfo,"D:\\","2.png");*/

    }

    /**
     * 将网络图片进行Base64位编码
     * @param imageUrl 图片的url路径,如http://.....xx.png
     * @return
     */
    public static String encodeImgageToBase64(URL imageUrl) {// 将图片文件转化为字节数组字符串,并对其进行Base64编码处理
        ByteArrayOutputStream outputStream = null;
        try {
            BufferedImage bufferedImage = ImageIO.read(imageUrl);
            outputStream = new ByteArrayOutputStream();
            ImageIO.write(bufferedImage, "png", outputStream);
        }catch (Exception e) {
            LOGGER.error("编码异常,imageUrl="+imageUrl,e);
        }
        // 对字节数组Base64编码
        return Base64.encodeBase64String(outputStream.toByteArray());// 返回Base64编码过的字节数组字符串
    }

    /**
     * 将网络图片进行Base64位编码
     * @param url 图片的url路径,如http://.....xx.png
     * @return
     */
    public static String encodeImgageToBase64(String url) {// 将图片文件转化为字节数组字符串,并对其进行Base64编码处理
        ByteArrayOutputStream outputStream = null;
        try {
            BufferedImage bufferedImage = ImageIO.read(new URL(url));
            outputStream = new ByteArrayOutputStream();
            ImageIO.write(bufferedImage, "png", outputStream);
        }catch (Exception e) {
            LOGGER.error("编码异常,url="+url,e);
        }
        // 对字节数组Base64编码
        return Base64.encodeBase64String(outputStream.toByteArray());// 返回Base64编码过的字节数组字符串
    }

    /**
     * 将本地图片进行Base64位编码
     *
     * @param imageFile
     * @return
     */
    public static String encodeImgageToBase64(File imageFile) {// 将图片文件转化为字节数组字符串,并对其进行Base64编码处理
        ByteArrayOutputStream outputStream = null;
        try {
            BufferedImage bufferedImage = ImageIO.read(imageFile);
            outputStream = new ByteArrayOutputStream();
            ImageIO.write(bufferedImage, "png", outputStream);
        } catch (IOException e) {
            LOGGER.error("编码异常,imageFile="+imageFile,e);
        }
        // 对字节数组Base64编码
        return Base64.encodeBase64String(outputStream.toByteArray());// 返回Base64编码过的字节数组字符串
    }

    /**
     * 将Base64位编码的图片进行解码,并保存到指定目录
     *
     * @param base64
     *          base64编码的图片信息
     * @return
     */
    public static void decodeBase64ToImage(String base64, String path,
                                           String imgName) {
        try {
            File pathFile = new File(path);
            if(!pathFile.exists()){
                pathFile.mkdirs();
            }
            FileOutputStream write = new FileOutputStream(new File(path
                    + imgName));
            byte[] decoderBytes = Base64.decodeBase64(base64);
            write.write(decoderBytes);
            write.close();
        } catch (IOException e) {
            LOGGER.error("编码异常,base64="+base64,e);
        }
    }
}
上一篇下一篇

猜你喜欢

热点阅读