java图片序列化_Java图片的序列化和反序列化以及格式转换如tif等

import java.io.ByteArrayInputStream;

import java.io.ByteArrayOutputStream;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

import java.util.Enumeration;

import java.util.HashMap;

import java.util.Iterator;

import java.util.Map;

import java.util.zip.ZipEntry;

import java.util.zip.ZipFile;

import javax.media.jai.JAI;

import javax.media.jai.PlanarImage;

import com.sun.media.jai.codec.ByteArraySeekableStream;

import com.sun.media.jai.codec.ImageCodec;

import com.sun.media.jai.codec.ImageEncoder;

import com.sun.media.jai.codec.JPEGEncodeParam;

import com.sun.media.jai.codec.SeekableStream;

/**

* @author humf

*

*/

public class IdepUtil {

public static String serializer(Object ob) throws Exception{

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);

objectOutputStream.writeObject(ob);

String serStr = byteArrayOutputStream.toString("ISO-8859-1");

serStr = java.net.URLEncoder.encode(serStr, "UTF-8");

objectOutputStream.close();

byteArrayOutputStream.close();

return serStr;

}

public static Object unSerializer(String xml) throws Exception{

String redStr = java.net.URLDecoder.decode(xml, "UTF-8");

ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(redStr.getBytes("ISO-8859-1"));

ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);

Object ob = objectInputStream.readObject();

objectInputStream.close();

byteArrayInputStream.close();

return ob;

}

@SuppressWarnings("unchecked")

public static Map zipSerializer(String zipPath) throws Exception{

File file = new File(zipPath);

ZipFile zip = new ZipFile(file);

Enumeration entries = (Enumeration)zip.entries();

Map map = new HashMap();

while(entries.hasMoreElements()){

ZipEntry entry = entries.nextElement();

String entryName = entry.getName();

InputStream inputStream = zip.getInputStream(entry);

ByteArrayOutputStream out=new ByteArrayOutputStream();

byte[] buffer=new byte[1024*4];

int n=0;

while ( (n=inputStream.read(buffer)) !=-1) {

out.write(buffer,0,n);

}

// byte[] b = out.toByteArray();

byte[] b = tifToJpg(out.toByteArray());

//String s = IdepUtil.serializer(b);

String s = byte2hex(b);

map.put(entryName, s);

out.close();

inputStream.close();

//System.out.println(s);

}

return map;

}

public static byte[] tifToJpg(byte[] b) throws IOException{

SeekableStream stream = new ByteArraySeekableStream(b);

PlanarImage in = JAI.create("stream", stream);

// OutputStream os = null;

// os = new FileOutputStream(output);

ByteArrayOutputStream os = new ByteArrayOutputStream();

JPEGEncodeParam param = new JPEGEncodeParam();

byte[] ret = null;

ImageEncoder enc = ImageCodec.createImageEncoder("JPEG", os, param);

try {

enc.encode(in);

os.flush();

ret = os.toByteArray();

os.close();

stream.close();

} catch (IOException e) {

e.printStackTrace();

}

return ret;

}

private static String byte2hex(byte[] b){ // 二进制转字符串

StringBuffer sb = new StringBuffer();

String stmp = "";

for (int n = 0; n < b.length; n++) {

stmp = Integer.toHexString(b[n] & 0XFF);

if (stmp.length() == 1){

sb.append("0" + stmp);

}else{

sb.append(stmp);

}

}

return sb.toString();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值