base64编码操作图片

package com.trsmedia.service;


import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
 
import java.util.Base64.Encoder;
import java.util.Base64;
import java.util.Base64.Decoder;
 
public class Image_base64_test {
	public static void main(String[] args) {
		//这一步很重要很重要很重要,因为base64的数据会有data:base64img,
		String str = "iVBORw0KGgoAAAANSUhEUgAAAXcAAAGQCAMAAABiX9D2AAAAAXNSR0IB2cksfwAAAchQTFRFAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAsLcCbwAAAJh0Uk5TACBfj69/P3D0/8um9jBQ6P2K0u3a2E/U/t4Qvu7P+3fIbvUf7PdYV/gtsOr5a3X8gdw6aYxsw+A7PPLNVObpv4nF1VN+qM68ve+R25vx5/rwh8LHLo2hwNaLlLlg65yj0K6VkvOX3Z/EhmLi2Y7TkK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EvaBx53ZU56GSbF5od2uyAznR6EYSgOiQE83/2t2YLHE52OO3uykAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA8fk/Tmi9wqi3qsEAAAAASUVORK5CYII=";
		generateImage(str,"F:\\tg\\tupian.png");
		
	}
	 /**
     * @param imgStr base64编码字符串
     * @param path   图片路径-具体到文件
     */
    public static boolean generateImage(String imgStr, String path) {
        if (imgStr == null)
            return false;
        Decoder decoder = Base64.getDecoder();
        try {
// 解密
            byte[] b = decoder.decode(imgStr);
// 处理数据
            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) {
        	
        	System.out.println(e);
            return false;
        }
    }
    //图片转化成base64字符串  
    public static String GetImageStr()  
    {//将图片文件转化为字节数组字符串,并对其进行Base64编码处理  
        String imgFile = "F:\\tupian\\a.jpg";//待处理的图片
                         // 地址也有写成"F:/deskBG/86619-107.jpg"形式的
        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();  
        }  
        //对字节数组Base64编码  
        Encoder encoder = Base64.getEncoder();
        return encoder.encodeToString(data);//返回Base64编码过的字节数组字符串  
    }

}

  

posted @ 2019-08-28 18:16  黑魔法os  阅读(643)  评论(0编辑  收藏  举报