
package fortuna.entropySources.webcam;

/**
 *
 * @author Viliam Krizan
 */

import com.github.sarxos.webcam.Webcam;
import fortuna.entropySources.EntropySource;
import java.awt.Color;
import java.awt.image.BufferedImage;



public class CaptureWebcam extends EntropySource{
    
    BufferedImage image;
    Webcam webcam;
    byte[] data = new byte[3];
    Color c;
    
    
    public CaptureWebcam(){
        try {
            //webcam detection
            webcam = Webcam.getDefault();
        } catch (Exception e) {
            System.exit(0);
        }
        webcam.open();
    }
    
    @Override
    public void run(){
        super.run();
        
        try {
            while(running){
                 long t1 = System.currentTimeMillis();
                image = webcam.getImage();
                final int[] data2 = new int[3];
                for (int i = 0; i < image.getHeight(); i++) {
                    for (int j = 0; j < image.getWidth(); j++) {
                    
                        c = new Color(image.getRGB(j, i));

                        data[0] =  (byte)c.getRed();
                        data[1] =  (byte)c.getGreen();
                        data[2] =  (byte)c.getBlue();
                    
                        acumulator.saveToPool(data, actualPool);
                        poolCounterIncrement();
                    }
                }
                
                long t2 = System.currentTimeMillis();
                System.out.println(image.getHeight() + " " + image.getWidth());
            }
        } catch (Exception e) {
            System.out.println("Unidentified webcam error");
        }
        webcam.close();
    }
}
