-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem4.java
More file actions
75 lines (59 loc) · 2.38 KB
/
Problem4.java
File metadata and controls
75 lines (59 loc) · 2.38 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*********************************************************************************************************
*
* Author: Maksim Markov, e-mail: maksim.markov.bg@gmail.com , mobile 088 6 839 991
* Date: 18.02.2015
*
**********************************************************************************************************/
package com.hackbulgaria.tasks;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Problem4 {
public void convertToGreyscale(String imgPath) {
int red, blue, green, newpixel;
try {
File input = new File(imgPath);
String[] strFilenameAndExt = input.getName().toString().split("\\.");
String strOutFileName = strFilenameAndExt[0]+"_grey"+"."+strFilenameAndExt[1];
String strFilenameGrey = input.getParent()+"\\"+strOutFileName;
File output = new File(strFilenameGrey);
BufferedImage buffImg = ImageIO.read(input);
if(!input.exists()){
System.out.println("Input image does not exist.");
System.exit(0);
}
int width = buffImg.getWidth();
int height = buffImg.getHeight();
BufferedImage imgGrey = new BufferedImage(width,height,buffImg.getType());
for (int i = 0; i < width; i++)
for (int j = 0; j < height; j++) {
red = new Color(buffImg.getRGB(i, j)).getRed();
blue = new Color(buffImg.getRGB(i, j)).getBlue();
green = new Color(buffImg.getRGB(i, j)).getGreen();
newpixel = (int)(red*0.299) + (int)(blue*0.587) + (int)(green*0.114);
Color newColor = new Color(newpixel, newpixel, newpixel);
imgGrey.setRGB(i, j, newColor.getRGB());
}
if(ImageIO.write(imgGrey, strFilenameAndExt[1], output))
System.out.println("Done! "+strFilenameGrey+" was created.");
else
System.out.println("Error: Task was not completed.");
} catch (IllegalArgumentException iex) {
System.out.println("Error : " + iex);
iex.printStackTrace();
} catch (IOException e) {
System.out.println("Error : " + e);
e.printStackTrace();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Problem4 prob4 = new Problem4();
//prob4.convertToGreyscale("C:/File/Fields.png");
//prob4.convertToGreyscale("C:/File/FolderX/Andrews1.png");
prob4.convertToGreyscale("C:/File/IMG_7471.jpg");
prob4.convertToGreyscale("C:/File/FolderX/setup.bmp");
}
}