码迷,mamicode.com
首页 > 编程语言 > 详细

Base64 和 图片互转java工具类

时间:2015-08-11 10:21:41      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:

package com.fengyunhe.helper.image;

import java.io.*;

/**
 * 图片base64互转
 * Created by yangyan on 2015/8/11.
 */
public class ImageBase64Utils {


    public static String bytesToBase64(byte[] bytes) {
        return org.apache.commons.codec.binary.Base64.encodeBase64String(bytes);// 返回Base64编码过的字节数组字符串
    }

    /**
     * 将图片文件转化为字节数组字符串,并对其进行Base64编码处理
     *
     * @param path 图片路径
     * @return base64字符串
     */
    public static String imageToBase64(String path) throws IOException {// 将图片文件转化为字节数组字符串,并对其进行Base64编码处理
        byte[] data = null;
        // 读取图片字节数组
        InputStream in = null;
        try {
            in = new FileInputStream(path);
            data = new byte[in.available()];
            in.read(data);
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return org.apache.commons.codec.binary.Base64.encodeBase64String(data);// 返回Base64编码过的字节数组字符串
    }

    /**
     * 处理Base64解码并写图片到指定位置
     *
     * @param base64 图片Base64数据
     * @param path   图片保存路径
     * @return
     */
    public static boolean base64ToImageFile(String base64, String path) throws IOException {// 对字节数组字符串进行Base64解码并生成图片
        // 生成jpeg图片
        try {
            OutputStream out = new FileOutputStream(path);
            return base64ToImageOutput(base64, out);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 处理Base64解码并输出流
     *
     * @param base64
     * @param out
     * @return
     */
    public static boolean base64ToImageOutput(String base64, OutputStream out) throws IOException {
        if (base64 == null) { // 图像数据为空
            return false;
        }
        try {
            // Base64解码
            byte[] bytes = org.apache.commons.codec.binary.Base64.decodeBase64(base64);
            for (int i = 0; i < bytes.length; ++i) {
                if (bytes[i] < 0) {// 调整异常数据
                    bytes[i] += 256;
                }
            }
            // 生成jpeg图片
            out.write(bytes);
            out.flush();
            return true;
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

测试代码:

InputStream bytesInput = HttpClientHelper.INSTANCE.get("http://ww4.sinaimg.cn/bmiddle/66640ec4jw1euxo80dk39j20c80c83z6.jpg", null, null).getEntity().getContent();
byte[] bytes = IOUtils.toByteArray(bytesInput);
String s = ImageBase64Utils.bytesToBase64(bytes);
ImageBase64Utils.base64ToImageFile(s, "d:\\avatar\\test.jpg");


如果你觉得内容不错,赏点小钱

技术分享

Base64 和 图片互转java工具类

标签:

原文地址:http://my.oschina.net/yangyan/blog/490518

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!