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

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

具体内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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<String,String> zipSerializer(String zipPath) throws Exception{
File file = new File(zipPath);
ZipFile zip = new ZipFile(file);
Enumeration<ZipEntry> entries = (Enumeration<ZipEntry>)zip.entries();
Map<String,String> map = new HashMap<String,String>();
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();
}

}

本作品采用知识共享署名 4.0 中国大陆许可协议进行许可,欢迎转载,但转载请注明来自御前提笔小书童,并保持转载后文章内容的完整。本人保留所有版权相关权利。

本文链接:https://royalscholar.cn/2017/04/17/Java图片的序列化和反序列化以及格式转换如tif等/

# JAVA, TIF

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×