图片BASE64工具 互转

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ck3345143/article/details/88355078

项目笔记:图片BASE64工具 互转

直接上代码:

package com.baigao.springboot.util;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Base64;

/**
 * <p>
 * <p>
 * Description: [图片BASE64工具]
 * </p>
 * 
 * @author Administrator
 * @version $Revision$ 2019年3月8日
 * @author (lastest modification by $Author$)
 * @since 20100901
 */
public class ImgBase64TransUtil {
	/**
	 * <p>
	 * Description:[从图片文件中读取内容]
	 * </p>
	 * 
	 * Created by [Administrator] [2019年3月8日]
	 * Midified by [修改人] [修改时间]
	 * @param path 图片文件的路径
	 * @return 二进制图片内容的byte数组
	 */
	public static byte[] readFile(Path path) {
		byte[] imageContents = null;
		try {
			imageContents = Files.readAllBytes(path);
		} catch (IOException e) {
			System.out.println("读取文件出错了...");
		}
		return imageContents;
	}

	/**
	 * <p>
	 * Description:[编码图片文件,编码内容输出为{@code String}格式]
	 * </p>
	 * 
	 * Created by [Administrator] [2019年3月8日]
	 * Midified by [修改人] [修改时间]
	 * @param imageContents 二进制图片内容的byte数组
	 * @return {@code String}格式的编码内容。
	 */
	public static String base64Encoding(byte[] imageContents) {
		if (imageContents != null)
			return Base64.getEncoder().encodeToString(imageContents);
		else
			return null;
	}

	/**
	 * 解码图片文件。 
	 *@param imageContents 待解码的图片文件的字符串格式。 
	 *@return 解码后图片文件的二进制内容。
	 */
	public static byte[] base64Decoding(String imageContents) {
		if (imageContents != null)
			return Base64.getDecoder().decode(imageContents);
		else
			return null;
	}

	/**
	 * 将解码后的二进制内容写入文件中。 
	 * @param path 写入的路径。 
	 * @param imageContents 解码后的二进制内容。
	 */
	public static void writeFile(Path path, byte[] imageContents) {
		if (imageContents != null)
			try {
				Files.write(path, imageContents, StandardOpenOption.CREATE);
			} catch (IOException e) {
				System.out.println("写入文件出错了...");
			}
	}
	
	/**
	 * <p>
	 * Description:[方法功能中文描述]
	 * </p>
	 * 
	 * Created by [Administrator] [2019年3月8日]
	 * Midified by [修改人] [修改时间]
	 * @param imgBase64Strn 图片base64码
	 * @param imgFilePath   目标图片。如:转换后存放为: d:\\wangyc.jpg
	 * @return
	 */
	@SuppressWarnings("restriction")
	public static boolean setBase64Str2Image(String imgBase64Str, String imgFilePath) {
		// 对字节数组字符串进行Base64解码并生成图片    
		if (imgBase64Str == null) // 图像数据为空     
			return false;    
		sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();    
		try {      
			// Base64解码      
			byte[] bytes = decoder.decodeBuffer(imgBase64Str);      
			for (int i = 0; i < bytes.length; ++i) {        
				if (bytes[i] < 0) {// 调整异常数据         
					bytes[i] += 256;        
					}      
				}      
			// 生成图片      
			OutputStream out = new FileOutputStream(imgFilePath);      
			out.write(bytes);      
			out.flush();      
			out.close();      
			return true;    
		} catch (Exception e) {
			System.err.println(">>>Base64解码 生成图片异常");
			return false;    
			
		}
	}

	public static void main(String[] args) {
		ImgBase64TransUtil bt = new ImgBase64TransUtil();
		String encodingString = bt.base64Encoding(bt.readFile(Paths.get("D:/temp/mm.png")));
		System.out.println("二进制图片文件Base64码:" + encodingString);
		bt.writeFile(Paths.get("D:/temp/mm2.png"), bt.base64Decoding(encodingString));
		System.out.println("任务结束...");
	}
}

猜你喜欢

转载自blog.csdn.net/ck3345143/article/details/88355078