Java之图片与Base64字符之间的相互转换

Base64工具类

package com.juzilicai.framework.common.util;

import java.io.UnsupportedEncodingException;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 * base64 编码 〈一句话功能简述〉 〈功能详细描述〉
 * 
 * @author bigxiongli
 * @version 2017年7月25日
 * @see Base64
 * @since
 */
public class Base64 {
	/**
	 * 加密
	 * 
	 * @param str
	 * @return
	 */
	public static String encode(byte[] b) {
		String s = null;
		if (b != null) {
			s = new BASE64Encoder().encode(b);
		}
		return s;
	}

	/**
	 * 加密
	 * 
	 * @param str
	 * @return
	 */
	public static String encode(String src) {
		return encode(src.getBytes());
	}

	/**
	 * 解密
	 * 
	 * @param s
	 * @return
	 */
	public static byte[] decode(String s) {
		byte[] b = null;
		if (s != null) {
			BASE64Decoder decoder = new BASE64Decoder();
			try {
				b = decoder.decodeBuffer(s);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return b;
	}

	/**
	 * 解密
	 * 
	 * @param s
	 * @return
	 */
	public static String decodeStr(String s) {
		byte[] b = decode(s);
		try {
			return b != null ? (new String(b, "UTF-8")) : null;
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return null;
		}
	}
}
互相转换的实现:
package com.juzilicai.encrypt.impl;

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

import org.apache.commons.io.FileUtils;

import com.juzilicai.framework.common.util.Base64;

import net.coobird.thumbnailator.Thumbnails;

/**
 * 功能描述:
 * 
 * @author deekeydai 2018年5月30日
 */
public class MainTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		File imgFile = new File("d://xxxxx.png");
		Base64 base64 = new Base64();
		InputStream in = null;
		byte[] data = null;
		// 读取图片字节数组
		try {
			in = new FileInputStream(imgFile);
			data = new byte[in.available()];
			in.read(data);
			in.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		// 对字节数组Base64编码
		System.err.println(data);
		System.out.println(base64.encode(data));

		String imgStr = base64.encode(data);
		System.err.println(imgStr.length());
		try {
			byte[] b = base64.decode(imgStr);
			for (int i = 0; i < b.length; ++i) {
				if (b[i] < 0) {// 调整异常数据
					b[i] += 256;
				}
			}
			// 生成jpeg图片
			String imgFilePath = "d://yyyyy.png";
			FileUtils.writeByteArrayToFile(new File(imgFilePath), b);
						
			/**
			 * 生成缩略图
			 * 若图片横比200小,高比300小,不变   
			 * 若图片横比200小,高比300大,高缩小到300,图片比例不变   
			 * 若图片横比200大,高比300小,横缩小到200,图片比例不变   
			 * 若图片横比200大,高比300大,图片按比例缩小,横为200或高为300 
			 */
			Thumbnails.of(new File(imgFilePath)).size(200, 300).toFile("d://yyyyy-slt.png");
//			OutputStream out = new FileOutputStream(imgFilePath);
//			out.write(b);
//			out.flush();
//			out.close();
			System.err.println("OK");
		} catch (Exception e) {
			
			System.out.println("error");
		}
	}

}

注意:

生成缩率图的Thumbnails需引入以下jar包

<dependency>
        <groupId>net.coobird</groupId>
	<artifactId>thumbnailator</artifactId>
        <version>0.4.8</version>
</dependency>

猜你喜欢

转载自blog.csdn.net/daihope/article/details/80512597