////Examine Image
* Examines the image and determines brightness
* @Charlie Barton
* @1.1
*/
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.AffineTransform;
import java.awt.image.*;
import javax.imageio.*;
import javax.swing.*;
import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.swing.ImageIcon;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class ExamineImage{
public static void lum(String ffile) {
BufferedImage img=null;
File myFile = new File(ffile);
// File[] listOfFiles = myFolder.listFiles();
try{
if (myFile.isFile()) {
img = ImageIO.read(myFile);
int width=0;
int height=0;
int count=0;
double avg=0;
double sum = 0;
if(img!=null){
width = img.getWidth();
height = img.getHeight();
}
System.out.println("Width " + width + " " + height);
for(int x=0; x < width; x++){
for (int y=0;y < height;y++){
int pixelCol = img.getRGB(x, y);
int a = (pixelCol >>> 24) & 0xff;
int r = (pixelCol >>> 16) & 0xff;
int g = (pixelCol >>> 8) & 0xff;
int b = pixelCol & 0xff;
double lum= (0.2126*r) + (0.7152*g) + (0.0722*b);
sum+= lum;
count += 1;
}}
double averageLum = sum/count;
System.out.println("The average luminance is " + averageLum);
}
}catch(Exception e){
System.out.println("THERE WAS A PROBLEM " + e);
}
}//end of lumanance method
}//end of class
////TheFileChooser
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;
import javax.swing.filechooser.*;
import java.io.*;
public class ChooseFile extends JPanel implements ActionListener {
JButton go;
String sourceFolder="";
String theFile="";
JFileChooser chooser;
String choosertitle;
public ChooseFile() {
go = new JButton("Choose Folder");
go.addActionListener(this);
add(go);
}
public void actionPerformed(ActionEvent e) {
System.out.println("Button Clicked");
chooser = new JFileChooser();
chooser.setCurrentDirectory(new java.io.File("."));
chooser.setDialogTitle(choosertitle);
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"JPG, PNG & GIF Images", "jpg", "gif", "png");
chooser.setFileFilter(filter);
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
String dirr = "" + chooser.getCurrentDirectory();
File file = chooser.getSelectedFile();
if(dirr.substring(dirr.length()-1,dirr.length()).equals(".")){
dirr = dirr.substring(0,dirr.length()-1);
sourceFolder=""+dirr + "" + file.getName();
}else{
sourceFolder=""+dirr + "/" + file.getName();
}
System.out.println("Folder path: " + dirr + " | File Name: " + file.getName());
System.out.println(sourceFolder);
ExamineImage.lum(sourceFolder);
}else {
System.out.println("No Selection ");
}
}
public Dimension getPreferredSize(){
return new Dimension(200, 200);
}
public static void main(String s[]) {
JFrame frame = new JFrame("");
ChooseFile panel = new ChooseFile();
frame.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
);
frame.getContentPane().add(panel,"Center");
frame.setSize(panel.getPreferredSize());
frame.setVisible(true);
}
}
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.