import java.awt.*; import java.io.BufferedWriter; import java.io.File; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; import java.util.Arrays; public class ReadPictureAndDecideWhichSide implements Runnable{ Thread thread; BufferedWriter bufOut; Socket povezava; int sleep = 0; int port = 0; int tolerance = 0; Color color; ProcessImage theDoer; Socket socket; ServerSocket serverSocket = null; public ReadPictureAndDecideWhichSide (){ } public void poslji(String ukaz) { try { bufOut.write(ukaz); bufOut.newLine(); bufOut.flush(); } catch (Exception e) { System.out.println("Napaka: " + e); } } public void povezi(int vrata) { try { povezava = new Socket("localhost", vrata); bufOut = new BufferedWriter(new OutputStreamWriter(povezava.getOutputStream())); } catch (Exception e) { System.out.println("Napaka: " + e); } } public void init(){ povezi(port); System.err.println("Connection established"); theDoer = new ProcessImage(); theDoer.TOLERANCE = tolerance; thread = new Thread(this); thread.start(); } /** * @param args */ public static void main(String[] args) { ReadPictureAndDecideWhichSide dd; if(args.length == 6){ dd = new ReadPictureAndDecideWhichSide(); dd.port = Integer.parseInt(args[0]); dd.sleep = Integer.parseInt(args[1]); dd.color = new Color(Integer.parseInt(args[3]), Integer.parseInt(args[4]), Integer.parseInt(args[5])); dd.tolerance = Integer.parseInt(args[2]); dd.init(); } else System.out.println("Usage: java ReadPictureAndDecideWhichSide portNumber sleepTime tolerance rcolor gcolor bcolor"); } public void run() { // TODO Auto-generated method stub File datoteka1 = new File("."); String s[], path = "", path1 = ""; //PrintWriter out = null; System.err.println("Connection established1"); String side = ""; boolean prva = true; while(true){ s = datoteka1.list(); Arrays.sort(s); for(int i = 0; i < s.length; i++) { int h = s[i].indexOf(".png"); //int tmp = Integer.parseInt(s[i].substring(0, h)); if(h > 0){ if(prva){ path = s[i]; prva = false; } else path = path1; path1 = s[i]; } } System.err.println("Uporabil bom sliko: " + path); theDoer.setImage(path); //side = theDoer.findSide(this.color); //prava razdalja med barvami Result bites = theDoer.findBites(this.color, 0); //hack int threshold = 4000; int row = 0; System.out.println("DOBIL: " + bites); if(bites.l > threshold){//l=1 if(bites.diff > threshold){//l=1 m=1 if(bites.r > threshold){ row = 7; } else{ row = 6; } } else{//l=1 m=0 if(bites.r > threshold){ row = 5; } else{ row = 4; } } } else{//l=0 if(bites.diff > threshold){//l=0 m=1 if(bites.r > threshold){ row = 3; } else{ row = 2; } } else{//l=0 m=0 if(bites.r > threshold){ row = 1; } else{ row = 0; } } } poslji("" + row); System.out.println("SIDE " + row + "\n"); try{ thread.sleep(sleep); } catch(Exception e){ System.err.println("Error"); } } } }