Java provides few inbuilt classes for view, edit and manage images. This is very simple program to detect an image is taken day or night.
Video Tutorial
Screenshots
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
/*
Coded By Ajith Kp (c) http://TerminalCoders.BlogSpot.in (c)
*/
public class DayNight extends JFrame {
JButton openB = new JButton("Open");
JPanel imagePanel = new JPanel();
JPanel menuPanel = new JPanel();
DayNight()
{
super("DayNight Detector Java By Ajith Kp [ ajithklp560 ");
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
setMaximizedBounds(env.getMaximumWindowBounds());
setExtendedState(getExtendedState() | MAXIMIZED_BOTH);
openB.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser fc = new JFileChooser();
int returnVal = fc.showOpenDialog(DayNight.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
try {
File imgf = fc.getSelectedFile();
imagePanel.removeAll();
setPic(imgf);
} catch (IOException ex) {
}
}
}
});
menuPanel.add(openB);
add(menuPanel, BorderLayout.NORTH);
add(imagePanel, BorderLayout.CENTER);
}
public void setPic(File imgf) throws IOException
{
BufferedImage myImg = ImageIO.read(imgf);
int w = myImg.getWidth();
int h = myImg.getHeight();
JLabel picLabel = new JLabel(new ImageIcon(myImg));
imagePanel.add(picLabel);
imagePanel.revalidate();
imagePanel.repaint();
double sr = 0, sg = 0, sb = 0;
double R, G, B;
int len = w*h;
for(int i=0;i<w;i++)
{
for(int j=0;j<h;j++)
{
Color c = new Color(myImg.getRGB(i, j));
sr+=c.getRed();
sg+=c.getGreen();
sb+=c.getBlue();
}
}
sr = (sr/len)/255.0;
sg = (sg/len)/255.0;
sb = (sg/len)/255.0;
if(sr <= 0.03928)
{
R = sr/12.92;
}
else
{
R = Math.pow((sr+0.055)/1.055, 2.4);
}
if(sg <= 0.03928)
{
G = sg/12.92;
}
else
{
G = Math.pow((sg+0.055)/1.055, 2.4);
}
if(sb <= 0.03928)
{
B = sb/12.92;
}
else
{
B = Math.pow((sb+0.055)/1.055, 2.4);
}
double y = 0.2126*R+0.7152*G+0.0722*B;
if(y<0.07)
{
JOptionPane.showMessageDialog(null, "Night Photo", "Day Or Night", JOptionPane.INFORMATION_MESSAGE);
}
else
{
JOptionPane.showMessageDialog(null, "Day Photo", "Day Or Night", JOptionPane.INFORMATION_MESSAGE);
}
}
public static void main(String[] args) {
// TODO code application logic here
DayNight dn = new DayNight();
dn.setSize(700, 500);
dn.setVisible(true);
dn.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

