-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageTest.java
40 lines (32 loc) · 1.1 KB
/
ImageTest.java
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
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
public class ImageTest {
public static void makeEncryptedImage(String msg) {
try {
JFileChooser fc = new JFileChooser();
int bs = fc.showOpenDialog(null);
//System.out.println(fc.getName(fc.getSelectedFile()));
File img = fc.getSelectedFile();
byte[] imageInByte;
BufferedImage originalImage = ImageIO.read(img);
// convert BufferedImage to byte array
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(originalImage, "jpg", baos);
baos.flush();
imageInByte = baos.toByteArray();
baos.close();
// convert byte array back to BufferedImage
InputStream in = new ByteArrayInputStream(imageInByte);
BufferedImage bImageFromConvert = ImageIO.read(in);
ImageIO.write(bImageFromConvert, "jpg", new File("Encrypted.jpg"));
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}