图片与Base64转换

作者 zhan-bin 日期 2018-06-22
图片与Base64转换

图片与Base64相互转换

包含7个class文件:

1.将图片解码成Base64编码文件 ——-“Encode”

2.将Base64编码转换成图片文件 ——-“Decode”

3.将文件夹里面的文件按顺序重命名(从1开始顺序递增) —–”Rename”

4.主类 ———“Execution”

5.编码接口类 ———“EncodeImpl”

6.解码接口类 ———“DecodeImpl”

7.重命名接口类 ———“RenameImpl”

代码:

Execution.class

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

package base64;



import java.io.IOException;



public class Execution {

public static void main(String args[]) throws IOException{

String Imgurl="C:/Users/Administrator/Desktop/1/1.png";

String url="C:/Users/Administrator/Desktop/1.txt";

String Folderurl="C:/Users/Administrator/Desktop/1";



Rename r=new Rename(Folderurl); //将文件按数字重命名

Decoder decoder=new Decoder(url,Imgurl); //TXT——>图片

Encoder encoder=new Encoder(url,Imgurl); //图片——>TXT



//Decoder decoder=new Decoder(Folderurl); //TXT——>图片(文件夹批量转)

//Encoder encoder=new Encoder(Folderurl); //图片——>TXT(文件夹批量转)



}





}

Decode.class

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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164

package base64;

import java.io.FileInputStream;



import sun.misc.BASE64Decoder;



import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;



import sun.misc.BASE64Encoder;

public class Decoder implements DecoderImpl{



//构造方法(文件夹操作)

public Decoder(String Folderurl) throws IOException{

String exurl=Folderurl;

String Imgexurl=Folderurl.replaceAll("\\(txt\\)","");

File folder=new File(Folderurl);

this.FolderDecode(folder, Imgexurl);

}

//构造方法(单个操作)

public Decoder(String url,String Imgurl) throws IOException{

this.Exe(url, Imgurl);

}







//对已经获取的base64解码

public byte[] Decode(byte[] base64code) throws IOException{

BASE64Decoder decode=new BASE64Decoder();

byte[] photocode= decode.decodeBuffer(new String(base64code));

for(int i=0;i<photocode.length;i++){

if(photocode[i]<0)photocode[i]+=256;

}

return photocode;

}



//取得文本里面的base64编码

public byte[] GetTxt(String url) throws IOException{

InputStream in =new FileInputStream(url);

byte[] base64code=new byte[in.available()];

in.read(base64code);

in.close();

return base64code;

}



//生成图片

public void Makephoto(byte[] photocode,String Imgurl) throws IOException{

OutputStream out=new FileOutputStream(Imgurl);

out.write(photocode);

out.flush();

out.close();

}



//执行转换的整个过程

public void Exe(String url,String Imgurl) throws IOException{

byte[] base64code=this.GetTxt(url);

String s=new String(base64code);

byte[] photocode=this.Decode(base64code);

this.Makephoto(photocode, Imgurl);

}



//对整个文件夹的base64编码文本进行转换

public void FolderDecode(File folder,String exurl) throws IOException{

if(folder.isDirectory()){

File file[]=folder.listFiles();

if(file!=null){

for(int i=0;i<file.length;i++){

this.FolderDecode(file[i],exurl);

}

}



}

else{

this.Exe(folder.getPath(), exurl+"/"+folder.getName().replaceAll("[.][^.]+$", "")+".jpg");

}

}





}

DecodeImpl.class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

package base64;



import java.io.File;

import java.io.FileNotFoundException;

import java.io.IOException;

public interface DecoderImpl {

public byte[] Decode(byte[] base64code) throws IOException;

public byte[] GetTxt(String url) throws IOException;

public void Makephoto(byte[] photocode,String Imgurl) throws FileNotFoundException, IOException;

public void Exe(String url,String Imgurl) throws IOException;

public void FolderDecode(File folder,String exurl) throws IOException;

}

Encode.class

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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162

package base64;



import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;



import sun.misc.BASE64Encoder;



public class Encoder implements EncodeImpl{

//单个图片转换构造方法

public Encoder(String url,String Imgurl) throws IOException{

Exe(url,Imgurl);

}



//整个文件夹图片转换

public Encoder(String exurl) throws IOException{

File first=new File(exurl);

this.FolderEncode(first, exurl);

}



//将已经编码成功的base64编码写进txt

public void WriterTxt(String url,byte[] base64code) throws IOException{

OutputStream out=new FileOutputStream(url);

out.write(base64code);

out.close();

}



//读取图片编码

public byte[] ReadPhoto(String Imgurl) throws IOException{

InputStream in = null;

try {

in = new FileInputStream(Imgurl);

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

byte[] photocode=new byte[in.available()];

in.read(photocode);

in.close();

return photocode;

}



//将图片进行base64编码

public byte[] Encode(byte[] photocode){



BASE64Encoder encoder=new BASE64Encoder();

byte[] base64code=encoder.encodeBuffer(photocode).getBytes();

return base64code;

}



//对整个文件夹的图片进行转换

public void FolderEncode(File folder, String exurl) throws IOException {

if(folder.isDirectory()){

File[] list=folder.listFiles();

if(list!=null){

for(int i=0;i<list.length;i++){

FolderEncode(list[i],exurl);

}

}}

else{

String filename=folder.getName().replaceAll("[.][^.]+$", "");

Exe(exurl+"(txt)"+"/"+filename+".txt",folder.getPath());

}

}



//执行转换(单张图片)

public void Exe(String url, String Imgurl) throws IOException {

byte[] photocode=this.ReadPhoto(Imgurl);

byte[] base64code =this.Encode(photocode);

this.WriterTxt(url,base64code);



}









}

EncodeImpl.class

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

package base64;



import java.io.File;

import java.io.FileNotFoundException;

import java.io.IOException;



public interface EncodeImpl {

public void WriterTxt(String url,byte[] base64code) throws FileNotFoundException, IOException;

public byte[] ReadPhoto(String Imgurl) throws IOException;

public byte[] Encode(byte[] photocode);

public void FolderEncode(File folder,String exurl) throws IOException;

public void Exe(String url,String Imgurl) throws IOException;

}

Rename.class

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

package base64;



import java.io.File;



public class Rename implements RenameImpl {

static int n=0;





public Rename(String Folderurl){

File f=new File(Folderurl);

this.rename(f, Folderurl);

}





public void rename(File f,String exurl){

if(f.isDirectory()){

File[] list=f.listFiles();

if(list!=null){

for(int i=0;i<list.length;i++){

rename(list[i],exurl);

}}

}

else{

String fileName=f.getName();

String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);

File re=new File(exurl+"/"+n+"."+suffix);

f.renameTo(re);

n++;

}



}



}

RenameImpl.class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

package base64;



import java.io.File;



public interface RenameImpl {

public void rename(File f,String exurl);



}