-
Notifications
You must be signed in to change notification settings - Fork 1
/
getImagePixel.java
58 lines (53 loc) · 2 KB
/
getImagePixel.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
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class getImagePixel {
public static int Width;
public static int height;
public static int[][] readImage(String path) {
BufferedImage img;
try {
img = ImageIO.read(new File(path));
Width = img.getHeight();
height = img.getWidth();
int[][] imagePixels= new int[Width][height];
for (int x = 0; x < height; x++) {
for (int y = 0; y < Width; y++) {
int pixel = img.getRGB(x, y);
int red = (pixel & 0x00ff0000) >> 16;
int grean = (pixel & 0x0000ff00) >> 8;
int blue = pixel & 0x000000ff;
int alpha = (pixel & 0xff000000) >> 24;
imagePixels[y][x] = red;
}
}
return imagePixels;
} catch (IOException e) {
return null;
}
}
public static BufferedImage getBufferedImage(int[][] imagePixels, int width, int height) {
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int y = 0; y < height; y++) {
int s;
if(y == 199)
s = 13;
for (int x = 0; x < width; x++) {
int value = -1 << 24;
value = 0xff000000 | (imagePixels[y][x] << 16) | (imagePixels[y][x] << 8) | (imagePixels[y][x]);
image.setRGB(x, y, value);
}
}
return image;
}
public static void writeImage(int[][] imagePixels, int width, int height, String outPath) {
BufferedImage image = getBufferedImage(imagePixels, width, height);
File ImageFile = new File(outPath);
try {
ImageIO.write(image, "jpg", ImageFile);
} catch (IOException e) {
e.printStackTrace();
}
}
}