
package my4;

/**
 *
 * @author Viliam Krizan
 */


import com.github.sarxos.webcam.Webcam;
import com.github.sarxos.webcam.WebcamUtils;
import com.github.sarxos.webcam.util.ImageUtils;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;


public class CaptureWebcam {
    
    //ColorTransform cTrans;
    BufferedImage image1, image2,test;
    Webcam webcam;
    int counter = 0;
    int counter2 = 0;
    int dpc = 0;
    
    File WebCamData = new File("WebCamData.txt");
    File WebCamData2 = new File("WebCamData2.txt");
    
    
    public CaptureWebcam(){
        try {
            webcam = Webcam.getDefault();
        } catch (Exception e) {
            System.exit(0);
        }
        webcam.open();
        
    }
    
    public static void main(String[] args) {
		CaptureWebcam web = new CaptureWebcam();
                web.run();
                
                
                //cTrans = new ColorTransform(image);
                //cTrans.getRGB();
	}
    
    public void run(){
        test  =  webcam.getImage();
        image1 = webcam.getImage();
        try {
            Thread.sleep(20);
        } catch (InterruptedException ex) {
        }
        image2 = webcam.getImage();
        try {
            ImageIO.write(image1, "PNG", new File("test1.png"));
            ImageIO.write(image2, "PNG", new File("test2.png"));
        } catch (IOException ex) {
            
        }
        
        //System.out.println("image height: " + image1.getHeight());
        //System.out.println("image width: " + image1.getWidth());
        
        //BufferedWriter writer = new BufferedWriter(new FileWriter(WebCamData,true));
                
        BufferedWriter writer = null;
        BufferedWriter writer2 = null;
         try {
             writer = new BufferedWriter(new FileWriter(WebCamData,true));
             writer2 = new BufferedWriter(new FileWriter(WebCamData2,true));
        } catch (IOException ex) {
            Logger.getLogger(CaptureWebcam.class.getName()).log(Level.SEVERE, null, ex);
        }
         
        int r,g,b;
        
        for (int i = 0; i < image1.getHeight(); i++) {
            for (int j = 0; j < image1.getWidth(); j++) {
                Color c1,c2;
                c1 = new Color(image1.getRGB(j, i));
                c2 = new Color(image2.getRGB(j, i));
                
                r =  c1.getRed();
                g =  c1.getGreen();
                b =  c1.getBlue();
                saveToText(writer, r, g, b);
                
                r =  c2.getRed();
                g =  c2.getGreen();
                b =  c2.getBlue();
                saveToText2(writer2,r, g, b);
                
                if(!c1.equals(c2)){
                    dpc++;
                }
                
            }
        }
        
        try {
            writer.close();
            writer2.close();
        } catch (IOException ex) {
            Logger.getLogger(CaptureWebcam.class.getName()).log(Level.SEVERE, null, ex);
        }
        //writer2.close();
        System.out.println("Number of different pixels: " + dpc);
    }
    
    public void saveToText(BufferedWriter writer, int r, int g, int b){
        //File WebCamData = new File("WebCamData.txt");
        
        try {
  
                //writer.write(counter + "\t");
            
                String s1 = String.format("%32s", Integer.toBinaryString(r & 0xFF)).replace(' ', '0');
                String s2 = s1.substring(31);
                writer.write(s2);
                
                s1 = String.format("%32s", Integer.toBinaryString(g & 0xFF)).replace(' ', '0');
                s2 = s1.substring(31);
                writer.write(s2);
                
                s1 = String.format("%32s", Integer.toBinaryString(b & 0xFF)).replace(' ', '0');
                s2 = s1.substring(31);
                writer.write(s2 + System.lineSeparator());
                counter++;
                    
                
           
        } catch (IOException ex) {
            System.out.println("cant write");
            
        }
    }
    
    public void saveToText2(BufferedWriter writer, int r, int g, int b){
        //File WebCamData = new File("WebCamData2.txt");
        
        try {
            //BufferedWriter writer = new BufferedWriter(new FileWriter(WebCamData,true));
                
                //writer.write(counter + "\t");
            
                String s1 = String.format("%32s", Integer.toBinaryString(r & 0xFF)).replace(' ', '0');
                String s2 = s1.substring(31);
                writer.write(s2);
                
                s1 = String.format("%32s", Integer.toBinaryString(g & 0xFF)).replace(' ', '0');
                s2 = s1.substring(31);
                writer.write(s2);
                
                s1 = String.format("%32s", Integer.toBinaryString(b & 0xFF)).replace(' ', '0');
                s2 = s1.substring(31);
                writer.write(s2 + System.lineSeparator());
                counter2++;
                    
                
      
        } catch (IOException ex) {
            //System.out.println("cant write");
            
        }
    }
}
