/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* SHSMain.java
*
* Created on 12 Jan, 2011, 9:38:29 AM
*/
package webcam;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import javax.media.control.FrameGrabbingControl;
import javax.media.*;
import javax.media.protocol.*;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.util.Date;
import webcam.ImageCompare;
import javax.media.control.FormatControl;
import javax.media.format.VideoFormat;
import javax.media.util.BufferToImage;
/**
*
* @author MOHIT
*/
public class SHSMain extends javax.swing.JFrame {
private FrameGrabbingControl frameGrabber;
private BufferedImage buffImg;
private int count;
/** Creates new form SHSMain */
public SHSMain() {
initComponents();
this.setLocation(10,60);
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
javax.swing.JOptionPane.showMessageDialog(rootPane, e);
}
getCam();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
desktoppane = new javax.swing.JDesktopPane();
menuBar = new javax.swing.JMenuBar();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
desktoppane.setBackground(new java.awt.Color(204, 204, 204));
desktoppane.setAlignmentX(20.0F);
desktoppane.setAlignmentY(20.0F);
setJMenuBar(menuBar);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(desktoppane, javax.swing.GroupLayout.PREFERRED_SIZE, 331, javax.swing.GroupLayout.PREFERRED_SIZE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(desktoppane, javax.swing.GroupLayout.PREFERRED_SIZE, 301, javax.swing.GroupLayout.PREFERRED_SIZE)
);
pack();
}// </editor-fold>//GEN-END:initComponents
public void getCam() {
try {
/* Grab the default web cam*/
MediaLocator ml = new MediaLocator("vfw://0");
/* Create my data source */
DataSource ds = Manager.createDataSource(ml);
requestFormatResolution(ds);
/* Create & start my player */
Player p = Manager.createRealizedPlayer(ds);
p.start();
Thread.currentThread().sleep(1000);
/* code for creating a JFrame and adding the visual component to it */
if (p.getVisualComponent() != null) {
Component comp = p.getVisualComponent();
comp.setSize(desktoppane.getWidth(), desktoppane.getHeight());
this.desktoppane.add(comp);
}
if (p.getControlPanelComponent() != null) {
this.desktoppane.add(p.getControlPanelComponent(), BorderLayout.SOUTH);
}
this.pack();
this.setVisible(true);
frameGrabber = (FrameGrabbingControl) p.getControl(FrameGrabbingControl.class.getName());
int i = 1,j=1;
ImageCompare ic = new ImageCompare();
Thread.sleep(5000);
/*while (true) {
grab(i);
if (i >= 2) {
String f1 = "E:\\" + (i - 1) + ".jpg";
String f2 = "E:\\" + i + ".jpg";
ic.ImageCompare_stage_1(f1, f2);
ic.setParameters(10, 10, 10, 10);
if (!ic.match()) {
count++;
if (count >= 2) {
ic.saveJP(ic.getChangeIndicator(), "e:\\changes"+i+".jpg");
//File file = new File("C:\\DeadRing.wav");
//SimpleAudioPlayer sap = new SimpleAudioPlayer(file)
}
}
}
i = i + 1;
}*/
} catch (Exception e) {
e.printStackTrace();
}
}
private void grab(int i) {
System.out.println(frameGrabber);
Buffer buf = frameGrabber.grabFrame();
// Convert frame to an buffered image so it can be processed and saved
Image img = (new BufferToImage((VideoFormat) buf.getFormat()).createImage(buf));
buffImg = new BufferedImage(img.getWidth(this.desktoppane), img.getHeight(this.desktoppane), BufferedImage.TYPE_INT_RGB);
Graphics2D g = buffImg.createGraphics();
g.drawImage(img, null, null);
g.setColor(Color.darkGray);
g.setFont(new Font("Tahoma", Font.PLAIN, 12).deriveFont(AffineTransform.getRotateInstance(1.57)));
g.drawString((new Date()).toString(), 5, 5);
FileOutputStream out = null;
try {
out = new FileOutputStream("E:\\" + i + ".jpg");
} catch (java.io.FileNotFoundException io) {
System.out.println("File Not Found");
}
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(buffImg);
param.setQuality(0.5f, false);
encoder.setJPEGEncodeParam(param);
try {
encoder.encode(buffImg);
out.close();
} catch (java.io.IOException io) {
System.out.println("IOException");
}
//ImageIO.write(buffImg,"jpg",of);
}
public boolean requestFormatResolution(DataSource ds) {
if (ds instanceof CaptureDevice) {
FormatControl[] fcs = ((CaptureDevice) ds).getFormatControls();
for (FormatControl fc : fcs) {
Format[] formats = ((FormatControl) fc).getSupportedFormats();
for (Format format : formats) {
if ((format instanceof VideoFormat)
&& (((VideoFormat) format).getSize().getHeight() <= 480)
&& (((VideoFormat) format).getSize().getWidth() <= 640)) {
((FormatControl) fc).setFormat(format);
return true;
}
}
}
}
return false;
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new SHSMain().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JDesktopPane desktoppane;
private javax.swing.JMenuBar menuBar;
// End of variables declaration//GEN-END:variables
}