-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPixelImage.java
More file actions
61 lines (50 loc) · 1.22 KB
/
PixelImage.java
File metadata and controls
61 lines (50 loc) · 1.22 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
import java.io.File;
import java.io.IOException;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
public class PixelImage{
public static void main(String args[])throws IOException{
BufferedImage img = null;
File f = null;
//read image
try{
f = new File("INSERTIMAGE");
img = ImageIO.read(f);
}catch(IOException e){
System.out.println(e);
}
/**
* Since, Sample.jpg is a single pixel image so, we will
* not be using the width and height variable in this
* project.
*/
//get pixel value
int p = img.getRGB(100,100);
//get alpha
int a = (p>>24) & 0xff;
//get red
int r = (p>>16) & 0xff;
//get green
int g = (p>>8) & 0xff;
//get blue
int b = p & 0xff;
/**
* to keep the project simple we will set the ARGB
* value to 255, 100, 150 and 200 respectively.
*/
a = 255;
r = 100;
g = 150;
b = 200;
//set the pixel value
p = (a<<24) | (r<<16) | (g<<8) | b;
img.setRGB(0, 0, p);
//write image
try{
f = new File("Output.jpg");
ImageIO.write(img, "jpg", f);
}catch(IOException e){
System.out.println(e);
}
}//main() ends here
}//class ends here