Java - 图片序列化
来源:互联网 发布:游戏门户网站源码php 编辑:程序博客网 时间:2024/04/30 12:54
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 {// 文件名的listList list = new ArrayList();// 先从本地读取图片,转成StringString 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转为jsonObjectMapper 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;// 返回读取多少个bytewhile ((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();}// uuidString 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 0
- Java - 图片序列化
- java dubbo 图片序列化传输
- 【c】序列化图片资源
- Ubuntu序列化重命名图片
- Java图片的序列化和反序列化以及格式转换如tif等
- 将图片序列化和饭序列化
- Serializable java序列化
- Java对象序列化
- java序列化-Serializable
- Serializable java序列化
- Serializable java序列化
- Java对象序列化
- Java对象序列化
- Java对象序列化
- Serializable java序列化
- JAVA序列化Serializable
- java对象序列化
- Java 对象序列化
- IOS 启动页面制作
- UILabel字体加粗
- 八大排序算法
- iPhone入门 - UIAlertView用法
- UIAlertView使用全解
- Java - 图片序列化
- Python错误和异常
- IOS中的沙盒机制(SandBox)
- mongo windows各版本下载地址
- ios开发中归档的使用
- jQuery - 删除元素
- Lable加载 类似QQ表情 …
- AFNetwork作用和用法详解
- ios开发中的基本设计模式