-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReadword.java
More file actions
115 lines (89 loc) · 3.28 KB
/
Copy pathReadword.java
File metadata and controls
115 lines (89 loc) · 3.28 KB
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
package getInfoFromImage;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Readword {
public static void main(String[] args){
//File f = new File("C://Users//BZP//Desktop//aaaa//d0og0.png");
getImageGRB("C://Users//BZP//Desktop//aaaa//d0og0.png");
}
public void getInfo(String filePath){
/*byte[] bbb = new byte[10240];
try {
InputStream a = new FileInputStream(f);
a.read(bbb);
//System.out.println(bbb[0]);
for (int i = 0; i < bbb.length; i++) {
System.out.println(Integer.toBinaryString(bbb[i]));
}
}*/
File f = new File("C://Users//BZP//Desktop//aaaa//d0og0.png");
FileInputStream fis;
try
{
fis = new FileInputStream(f);
byte[] b;
b = new byte[fis.available()];
StringBuilder str = new StringBuilder();// 不建议用String
fis.read(b);
for (byte bs : b)
{
System.out.println(Integer.toBinaryString(bs));
str.append(Integer.toBinaryString(bs));// 转换为二进制
}
System.out.println("二进制为"+str);
File apple = new File("C://Users//BZP//Desktop//cccc//1.png");// 把字节数组的图片写到另一个地方
FileOutputStream fos = new FileOutputStream(apple);
fos.write(b);
fos.flush();
fos.close();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/*BufferedImage bi;
try {
bi = ImageIO.read(f);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bi, "png", baos); //经测试转换的图片是格式这里就什么格式,否则会失真
byte[] bytes = baos.toByteArray();
for (int i = 0; i < bytes.length; i++) {
System.out.print(bytes[i]);
}
// bi.getData();
//ByteArrayOutputStream baos = new ByteArrayOutputStream();
//System.out.println("二进制位"+bi.getType());
} catch (IOException e) {
e.printStackTrace();
} */
}
public static int[][] getImageGRB(String filePath) {
File file = new File(filePath);
int[][] result = null;
if (!file.exists()) {
return result;
}
try {
BufferedImage bufImg = ImageIO.read(file);
int height = bufImg.getHeight();
int width = bufImg.getWidth();
result = new int[width][height];
System.out.println("长"+width+"宽"+height);
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
//bufImg.get
result[i][j] = bufImg.getRGB(i, j) & 0xFFFFFF;
System.out.println(bufImg.getRGB(i, j) & 0xFFFFFF);
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
}