java处理图片与base64编码互相转换

最近做项目遇到,需要在signature签名元素中添加签名的base64编码,网上找了js处理,出结果了,但编码不正确。之后用java代码处理(与在线转换http://imgbase64.duoshitong.com/结果一样),直接上代码,如下:


package ydysz.tools;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.commons.codec.binary.Base64;

import sun.misc.BASE64Decoder;

/**
 * 将图片转换为Base64
 * 将base64编码字符串解码成img图片
 * @创建时间 2018-10-16 10:20
 */
public class Img2Base64Util {

    public static void main(String[] args) {
        String imgFile = "C:\\Users\\dellpc\\Desktop\\0094.png";// 待处理的图片
        String imgbese = getImgStr(imgFile);
        System.out.println(imgbese.length());
        System.out.println(imgbese);
        String imgFilePath = "C:\\Users\\dellpc\\Desktop\\0094.jpg";// 新生成的图片
        generateImage(imgbese, imgFilePath);
    }

    /**
     * 将图片转换成Base64编码
     * @param imgFile 待处理图片
     * @return
     */
    public static String getImgStr(String imgFile) {
        // 将图片文件转化为字节数组字符串,并对其进行Base64编码处理

        InputStream in = null;
        byte[] data = null;
        // 读取图片字节数组
        try {
            in = new FileInputStream(imgFile);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new String(Base64.encodeBase64(data));
    }

    /**
     * 对字节数组字符串进行Base64解码并生成图片
     * @param imgStr 图片数据
     * @param imgFilePath 保存图片全路径地址
     * @return
     */
    public static boolean generateImage(String imgStr, String imgFilePath) {
        //
        if (imgStr == null) // 图像数据为空
            return false;
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            // Base64解码
            byte[] b = decoder.decodeBuffer(imgStr);
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {// 调整异常数据
                    b[i] += 256;
                }
            }
            // 生成jpg图片
            OutputStream out = new FileOutputStream(imgFilePath);
            out.write(b);
            out.flush();
            out.close();
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}
base64转图片如图(本地桌面截图):


图片转base64效果如下:

Qk06EAAAAAAAADYAAAAoAAAANgAAABkAAAABABgAAAAAAAQQAAAAAAAAAAAAAAAAAAAAAAAAAAAARERE/////v7+/////f39/////////////////////////////////////////////f39////////////4+PjdnZ2ampqu7u7/Pz8/////v7+/////////f39/////////////////////////////////////////////////////////////////////v7+6+vrcXFxqamp/////v7+AAAKCgoKCgrOzs7+/v7////+/v7////////////////5+fmWlpZ7e3u6urr////+/v7+/v7///////////////+srKwCAgIAAAABAQEyMjJycnLGxsb////9/f3////////////7+/v////8/Pz9/f3////7+/u5ubnl5eX+/v7////////+/v7////7+/v////6+vpeXl4BAQEiIiL6+vr///8AAGRkZAUFBWxsbP///////8vLy9zc3P////39/f39/X9/fwEBAQAAAAYGBpWVlf////////z8/GJiYlJSUvb29vf396CgoFRUVB8fHwAAAAAAAEVFRf39/f////39/f7+/v////////7+/v///////////6ampgICAkFBQfz8/Pv7+/////////z8/P////7+/oSEhAAAAAAAABoaGu7u7vz8/AAA19fXCAgIGxsb8/Pz/v7+KCgoHR0d39/f////39/fAwMDERERYWFhBAQEBAQEo6Oj+/v7////XFxcAgICqamp////zc3NUFBQvb29u7u7EBAQFRUV39/f/////////////f39/////////f39////////OTk5AAAABAQE0tLS////8vLyysrK4ODg/v7+1NTUBgYGJSUlFBQUAAAA1dXV////AAD+/v5BQUEAAACwsLD7+/tjY2MAAACTk5P////Hx8cAAABJSUn///93d3cCAgIcHBzk5OT+/v7b29sREREiIiLx8fGampoAAAAyMjLQ0NA1NTUAAACjo6P////+/v7///////////////+ZmZlQUFC1tbUEBAQCAgIBAQGNjY3+/v5paWkAAAATExN+fn5EREQDAwMlJSUVFRUAAADT09P///8AAP///2VlZQAAADY2Np2dnS0tLQAAAENDQ/n5+cXFxQMDA0ZGRv////z8/EVFRQAAAHNzc/7+/v///3R0dAEBAa6ursXFxQMDAwAAAAEBAQUFBQQEBIaGhv////39/f7+/v////////z8/GlpaQICAjQ0NAwMDE1NTQAAAFNTU////zg4OAAAAAMDAwICAgICAjAwMAAAAAAAAAEBAb29vf///wAA+vr6jY2NBQUFAgICMTExAQEBAAAAAQEBt7e319fXAAAALCws////////sbGxBAQEMTEx/f39////1tbWBgYGMDAw2traGxsbAQEBAAAACQkJAAAAhYWF/////////////f39////////yMjICgoKAAAAFxcXrq6uBAQELi4u7+/vTExMBAQEAAAAAAAAAgICCgoKCwsLHR0dAQEBkJCQ/f39AAD///+3t7cAAAASEhIFBQUAAAAKCgoAAABoaGjk5OQFBQUODg7Ly8v////r6+sKCgoVFRXw8PD9/f3///9qamoAAACMjIxcXFwAAAAICAgODg4CAgKQkJD9/f3////8/Pz////6+vr////8/Pw2NjYCAgIbGxvOzs4UFBQPDw9QUFADAwMAAAAICAhra2sjIyMAAAA3NzdoaGgAAAB5eXn9/f0AAP///+Dg4AEBAUhISE1NTQ8PDwAAAAEBAQoKClNTUw0NDQAAADIyMurq6v///zAwMAEBAZ2dnY2NjY+Pj3h4eAEBAUJCQmVlZQICAgAAAAEBAQEBAYuLi6enp+Xl5f////7+/v////39/f///3t7ewAAAA0NDePj40JCQgAAAAAAAAMDAwMDAwAAADExMScnJwAAAB4eHnl5eQAAAD09Pf///wAA////7u7uCwsLOjo6////b29vAAAABAQEAAAAAgICAAAAAwMDAAAAVlZW+/v7VVVVAQEBLi4uAgICAwMDAAAAAwMDAAAAFBQUBAQENDQ0IiIiCQkJPDw8AAAAmZmZ/////f39////////////x8fHBQUFAAAA0tLSjY2NAAAAAAAAeXl5pqamCAgIAAAAAQEBAwMDc3Nz3d3dCAgIHx8f8/PzAAD///////8sLCwsLCz7+/vAwMABAQEBAQECAgIAAAAoKCipqalaWloCAgKIiIinp6cAAABmZmaLi4tWVlYmJiYCAgIBAQEBAQEaGhqpqal+fn4ICAgCAgIcHBzb29v+/v7////////9/f3////19fUyMjIDAwOVlZWEhIQEBAQODg7j4+P9/f2GhoYDAwMAAAAAAAAbGxt1dXUjIyMAAADNzc0AAPv7+////ysrKykpKf////Dw8BgYGAAAAAAAAAcHB2lpafz8/OPj4w0NDRoaGrm5uQQEBDk5Ofz8/P////Hx8b29vTw8PAAAAAAAAAsLCwQEBAAAAEVFRdXV1f39/f////7+/v////////////7+/oSEhAICAhMTEw4ODgAAAEBAQP////////v7+zExMQAAAAQEBAAAAAAAAAAAAAAAAJ6engAA////+vr6QUFBOTk5////+/v7QkJCAgICAAAAAAAAIiIi7+/v////g4ODAAAAd3d3X19fAAAAy8vL/Pz8////////uLi4AQEBBwcHGxsbAAAAAQEBZWVl+Pj4/f39/////////////f39/f39////cHBwAAAAAgICERERCQkJLCwse3t7cnJy8PDwvb29BgYGAAAAGBgYPz8/AgICAQEBeHh4AAD9/f3////09PTl5eX+/v7///9LS0sAAABfX18CAgIAAACFhYX////r6+sFBQUgICDJyckSEhJlZWX9/f3////6+vqKiooEBAQHBwdVVVU0NDQJCQkAAACbm5v////9/f3////7+/v////////5+flYWFgBAQEAAABAQEAdHR0AAAAAAAACAgI6OjrW1tZZWVkDAwMJCQnY2NhiYmIAAABTU1MAAP////39/fz8/P///7W1tYSEhLW1tbe3t/39/YmJiQYGBhEREcLCwvn5+UNDQwAAAM7OzsjIyLCwsP///////+7u7g0NDQAAAAAAAAICAgQEBCsrK1hYWKGhof39/f////////7+/v7+/v///////+/v75CQkBgYGAEBAQICAgICAjExMQYGBgAAAC0tLWBgYAAAAAYGBldXV5eXlwMDAy0tLQAA/Pz8////////////vb29AAAAo6Oj/v7+/////v7+LS0tAAAADQ0Nm5ubTU1NAAAAr6+v/////////////v7+/f39s7OzcHBwEhISAAAAAQEBJSUl+Pj4/Pz8/////////Pz8/////v7+////+fn5////////2traXV1dDAwMAgICFhYWHR0dAAAAAgICBQUFEBAQAAAAAQEBCgoKAAAAKCgoAAD////9/f3+/v7+/v68vLwBAQF5eXn////8/Pz///9eXl4CAgIAAAADAwMAAAAAAAC/v7/9/f3////////////////p6elvb28REREAAAACAgKNjY3+/v7////////+/v7////+/v7////8/Pz////////9/f3+/v7///9ra2sAAAAKCgoAAAAAAAAAAAABAQEAAAACAgIAAAABAQEBAQEZGRkAAP7+/v////39/cHBwR4eHgQEBLKysv39/f///////5mZmQAAAFRUVFpaWjU1NXJycvT09P////////7+/v////39/bW1tQAAAAICAgAAAAMDAy4uLu/v7////////////////////////////////////////////////3V1dQAAAJqamqqqqqysrBwcHAAAAAQEBAAAAAAAADc3N3t7e56engAA////////4ODgBQUFBAQEZ2dn9vb2////////+/v7u7u7AgICbGxs/v7++/v7/////v7+/v7+/////v7+/f39////8/Pzo6OjX19fAwMDAAAAAQEB2dnZ+/v7/////v7+////////////////////////////////////c3NzAAAAmpqa////+vr6p6enBgYGCgoKGBgYAAAADg4Ozs7O+vr6AAD////7+/vk5OSBgYG8vLz+/v7////9/f39/f3////BwcEAAABeXl7////////8/Pz////8/Pz////////////9/f3////+/v78/PwzMzMCAgJ2dnb7+/v///////////////////////////////////////////////+5ubk3Nzeqqqr///////////9oaGgDAwNNTU0pKSkAAAB0dHT///8AAP////////7+/v////r6+v////39/f7+/v///////+Pj41VVVampqf39/f39/f////////////////39/f////39/f7+/v////7+/mdnZwAAALe3t/7+/v////////z8/P////////////////////////////////7+/v////r6+v////////39/f7+/urq6h4eHhgYGKWlpXh4eNDQ0P///wAA/f39/////Pz8/Pz8////+/v7/f39////+/v7/////////////f39/////Pz8/////f39////////+/v7/////////////v7+////oqKiAgICg4OD+vr6/////////////////////////////////////////////////f39/////Pz8/v7+////////+/v7gYGBAwMDYmJi/////////Pz8AAD////9/f3////////8/Pz////////9/f3////8/Pz////+/v7+/v7////////9/f39/f3////+/v7////////+/v7////+/v7+/v7V1dUAAABISEj////////9/f3////////////////////////////////////+/v7////+/v7////+/v7////9/f3////d3d0ICAgCAgKysrL9/f3///8AAP////////////7+/v////39/f////////v7+/////39/fv7+/////z8/P39/f////////7+/v7+/v////7+/v39/f7+/v////39/e3t7VxcXJ6env////v7+/////7+/v////////////////////////////////7+/v////////v7+/////7+/vz8/P////7+/n9/fwAAAHl5ef///////wAA/v7+/////////Pz8/////////v7+/////////f39/////////////////////////////f39/////v7+/Pz8/////v7+/////v7+////+vr6/v7+/////////v7+/////////////////////////////////////////v7+////////////+/v7/////v7+////+vr6xsbG6enp////////AAA=

你可能感兴趣的:(java)