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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
package aufgaben.imageio;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;
import aufgaben.Aufgabe;
import misc.Utils;
public class Aufgabe1 extends Aufgabe {
private final int width = 500, height = 500;
private boolean done = false;
private String name;
private JPanel panel;
private JTextField inPath, outPath;
private JButton readButton, writeButton;
private BufferedImage bild;
public Aufgabe1() {
name = "Lesen Speichern";
this.setSize(width, height);
this.setTitle(name);
this.addWindowListener(
new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.out.println("close");
done = true;
}
}
);
this.setLayout(null);
}
@Override
public boolean done() {
return done;
}
@Override
public String getName() {
return name;
}
@Override
public void init() {
inPath = new JTextField();
inPath.setBounds(50, 50, 200, 20);
outPath = new JTextField();
outPath.setBounds(50, 80, 200, 20);
readButton = new JButton("Lesen");
readButton.setBounds(260, 50, 100, 20);
readButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
bild = readImage(new File(inPath.getText()));
/*
try {
bild = ImageIO.read(new File("title.png"));
} catch (IOException e) {
e.printStackTrace();
}
*/
panel.repaint();
}
});
writeButton = new JButton("Schreiben");
writeButton.setBounds(260, 80, 100, 20);
writeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
writeImage(new File(outPath.getText()), bild);
}
});
panel = new JPanel() {
public void paint(Graphics g) {
if(bild != null)
g.drawImage(bild, 0, 0, 500, 390, this);
}
};
panel.setBounds(0, 110, 500, 390);
this.add(inPath);
this.add(outPath);
this.add(readButton);
this.add(writeButton);
this.add(panel);
this.repaint();
}
private BufferedImage getGreyImage(BufferedImage i) {
BufferedImage image = new BufferedImage(i.getWidth(), i.getHeight(), i.getType());
for(int x = 0, width = i.getWidth(); x<width; x++) {
for(int y = 0, height = i.getHeight(); y<height; y++) {
Color c = new Color(i.getRGB(x, y));
int h = (int)(((double)(c.getRed()+c.getGreen()+c.getBlue())/765)*255);
image.setRGB(x, y, new Color(h, h, h).getRGB());
}
}
return image;
}
private BufferedImage readImage(File file) {
FileInputStream fis;
BufferedInputStream bis;
try {
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
byte[] b = new byte[4];
bis.read(b);
int width = byteArrayToInt(b);
bis.read(b);
int height = byteArrayToInt(b);
bis.read(b);
int type = byteArrayToInt(b);
BufferedImage image = new BufferedImage(width, height, type);
for(int x = 0; x<width; x++) {
for(int y = 0; y<height; y++) {
bis.read(b);
image.setRGB(x, y, byteArrayToInt(b));
}
}
bis.close();
fis.close();
return image;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private void writeImage(File file, BufferedImage image) {
FileOutputStream fos;
BufferedOutputStream bos;
try {
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
bos.write(intToByteArray(image.getWidth()));
bos.write(intToByteArray(image.getHeight()));
bos.write(intToByteArray(image.getType()));
for(int x = 0, width = image.getWidth(); x<width; x++) {
for(int y = 0, height = image.getHeight(); y<height; y++) {
bos.write(intToByteArray(image.getRGB(x, y)));
}
}
bos.close();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static int byteArrayToInt(byte[] b)
{
return b[3] & 0xFF |
(b[2] & 0xFF) << 8 |
(b[1] & 0xFF) << 16 |
(b[0] & 0xFF) << 24;
}
public static byte[] intToByteArray(int a)
{
return new byte[] {
(byte) ((a >> 24) & 0xFF),
(byte) ((a >> 16) & 0xFF),
(byte) ((a >> 8) & 0xFF),
(byte) (a & 0xFF)
};
}
}
|