-
Notifications
You must be signed in to change notification settings - Fork 3
/
ImageExample.java
98 lines (87 loc) · 3.16 KB
/
ImageExample.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
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
/**
* ImageExample.java
* Jeff Ondich, 28 October 2015
*
* How to directly modify pixels in an image.
*/
import java.io.IOException;
import java.io.File;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import java.awt.FlowLayout;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
public class ImageExample {
public static void main(String[] args) {
// Parse command line
boolean showImages = false;
String imageFilePath = null;
String outputImageFilePath = null;
String outputFormatName = ""; // e.g. "png" or "jpg"
for (String arg: args) {
if (arg.equals("--show")) {
showImages = true;
} else if (imageFilePath == null) {
imageFilePath = arg;
} else if (outputImageFilePath == null) {
outputImageFilePath = arg;
int index = outputImageFilePath.lastIndexOf('.');
if (index >= 0) {
outputFormatName = outputImageFilePath.substring(index + 1);
}
}
}
if (imageFilePath == null && outputImageFilePath == null) {
System.err.println("Usage: java ImageExample inputImage outputImage [--show]");
return;
}
// Open the input image
BufferedImage image;
try {
image = ImageIO.read(new File(imageFilePath));
} catch(IOException e) {
System.err.println("Can't open " + imageFilePath);
return;
}
// Get and save the grayscale image
BufferedImage grayImage = toGray(image);
try {
File outputfile = new File(outputImageFilePath);
ImageIO.write(grayImage, outputFormatName, outputfile);
} catch (IOException e) {
System.err.println("Trouble saving " + outputImageFilePath);
return;
}
// Show the before and after images
if (showImages) {
showImage(image);
showImage(grayImage);
}
}
private static BufferedImage toGray(BufferedImage image) {
int width = image.getWidth();
int height = image.getHeight();
BufferedImage newImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int color = image.getRGB(x, y);
int alpha = (color & 0xff000000) >> 24;
int red = (color & 0x00ff0000) >> 16;
int green = (color & 0x0000ff00) >> 8;
int blue = (color & 0x000000ff);
int gray = (red + green + blue) / 3;
int newColor = (alpha << 24) | (gray << 16) | (gray << 8) | gray;
newImage.setRGB(x, y, newColor);
}
}
return newImage;
}
private static void showImage(BufferedImage image) {
JFrame frame = new JFrame();
frame.getContentPane().setLayout(new FlowLayout());
frame.getContentPane().add(new JLabel(new ImageIcon(image)));
frame.pack();
frame.setVisible(true);
}
}