Java - 图片序列化

1.将本地的图片转成byte数组,base64序列化,转成byte数组,保存为jpeg

package p01;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.imageio.stream.FileImageInputStream;
import javax.imageio.stream.FileImageOutputStream;

import com.fasterxml.jackson.databind.ObjectMapper;
/*
 * 1. 图片转为byte数组,byte数组转为,base64 String
 * 2. String 转回 byte数组,在本地保存为图片
 * 3. 将java 对象转化为json 数据
 */
public class ImgToString {
	public static void main(String []args) throws Exception {
		// 文件名的list
		List list = new ArrayList();
		
		// 先从本地读取图片,转成String
		String strF1 = "f:\\115图片20170210174749.png";
		list.add(strF1);
		File f1 = new File("");
		String uuid = (UUID.randomUUID().toString()).replaceAll("-", "");
		String strF2 = "f:\\img\\desk\\" + uuid + ".JPEG";
		list.add(strF2);
		File f2 = new File(strF2);
		File dir = new File("f:\\img\\desk\\");
		// 创建文件夹
		dir.mkdirs();
		Util util = new Util();
		// 将list转为json
		ObjectMapper mapper = new ObjectMapper();
		String json = mapper.writeValueAsString(list);
		System.out.println(json);
		if (f1.exists()) {
			byte []data = null;
			FileImageInputStream input = new FileImageInputStream(f1);
			ByteArrayOutputStream output = new ByteArrayOutputStream();
			byte []buf = new byte[1024];
			int numBytesRead = 0;
			// 返回读取多少个byte
			while ((numBytesRead = input.read(buf)) != -1) {
				output.write(buf, 0, numBytesRead);
			}
			data = output.toByteArray();
			String strImg = util.encodeBase64(data);
//			System.out.println(strImg);
			System.out.println("正在转码");
			input.close();
			output.close();
			// 返序列化
			// 将字符串转化为图片
			byte []dataImg = util.decodeBase64(strImg);
			FileImageOutputStream fios = new FileImageOutputStream(f2);
			fios.write(dataImg, 0, dataImg.length);
			fios.close();
			System.out.println("ok");
		} else {
			System.out.println("null");
		}
		System.out.println("end");
	}
}


2.

接受base64 的string,保存在本地,返回json格式的图片路径

package com.yuejianmian.util;

import java.io.File;
import java.lang.reflect.Method;
import java.util.UUID;

import javax.imageio.stream.FileImageOutputStream;

import com.fasterxml.jackson.databind.ObjectMapper;

public class StringUtil {
	public String saveImgLocal(String basePath, String imgStr) throws Exception {
		// 创建文件夹
		File dir = new File(basePath);
		// 判断是否存在,不存在创建
		if (!dir.exists()) {
			dir.mkdirs();
		}
		// uuid
		String uuid = (UUID.randomUUID().toString()).replaceAll("-", "");
		String imgPath = basePath + uuid + ".JPEG";
		// 创建图片流
		FileImageOutputStream fios = null;
		// 将String 转成byte数组
		byte []dataImg = decodeBase64(imgStr);
		// 写入本地
		File img = new File(imgPath);
		fios = new FileImageOutputStream(img);
		fios.write(dataImg, 0, dataImg.length);
		fios.close();
		return imgPath;
	}
	// jackson插件,将一个对象转成一个json字符串
	public String objToJSONString(Object obj) throws Exception {
		ObjectMapper mapper = new ObjectMapper();
		return mapper.writeValueAsString(obj);
	}
	 /*** 
     * encode by Base64 
     */  
	private  byte[] decodeBase64(String input) throws Exception{  
        Class clazz=Class.forName("com.sun.org.apache.xerces.internal.impl.dv.util.Base64");  
        Method mainMethod= clazz.getMethod("decode", String.class);  
        mainMethod.setAccessible(true);  
         Object retObj=mainMethod.invoke(null, input);  
         return (byte[])retObj;  
    } 
}

 /*** 
     * decode by Base64 
     * 将字符串转化为图片,序列化
     */  
    @SuppressWarnings({ "unchecked", "rawtypes" })
	public static byte[] decodeBase64(String input) throws Exception{  
        Class clazz=Class.forName("com.sun.org.apache.xerces.internal.impl.dv.util.Base64");  
        Method mainMethod= clazz.getMethod("decode", String.class);  
        mainMethod.setAccessible(true);  
         Object retObj=mainMethod.invoke(null, input);  
         return (byte[])retObj;  
    } 


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

PeersLee

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值