import java.awt.Color; import java.awt.Graphics; import java.awt.event.MouseEvent; import java.awt.image.BufferedImage; import java.io.BufferedOutputStream; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.net.URLEncoder; import java.util.Date; import sun.net.TelnetInputStream; import sun.net.TelnetOutputStream; import sun.net.ftp.FtpClient; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGEncodeParam; import com.sun.image.codec.jpeg.JPEGImageEncoder; public class BestPixelTracker extends PixelOperater{ /** * */ private static final long serialVersionUID = 1L; //make a default color that you are chasing int redTarget = 255; int greenTarget = 255; int blueTarget = 255; //keep track of the closestSoFar int closestInColorSoFar = 1000; //keep track of the winning pixel int bestY; int bestX; BestPixelTracker(){ super(); } public void newFrame(){ //reset the closest to be very far away closestInColorSoFar = 1000; setTitle("Best Pixel"); super.newFrame(); } public void performPixelOp(int _x, int _y){ int[] rgb = ps.getPixel(_x,_y); //color space works like cartesian space and you can use dist=sqrt(x2 + y2 + z2) or you could just use add the absolute value of the differences int differenceInColor = (int) Math.sqrt(Math.pow( rgb[1] - redTarget,2) + Math.pow( rgb[2] - greenTarget,2) + Math.pow( rgb[3] - blueTarget,2)); //see if this is closer in color than the previous closest if (differenceInColor < closestInColorSoFar){ //raise the bar for closest closestInColorSoFar = differenceInColor; //register the pixel as the current front runner for winning bestX = _x; bestY = _y; } } public void paint(Graphics g){ super.paint(g); //paint the winner g.setColor(Color.red); g.fillOval(kWidth + bestX-15,bestY-15,30,30); } public String getTimeString() { Date now = new Date(); String date = String.valueOf(now.getYear() + "-" + String.valueOf(now.getMonth() + 1) + "-" + String.valueOf(now.getDate())); long millis = now.getTime(); String time = String.valueOf(millis); return date + time; } public void mousePressed(MouseEvent e){ //allow the user to click on the color they want to chase boolean pressed = true; int[] rgb = ps.getPixel(e.getX(), e.getY()); redTarget = rgb[1]; greenTarget = rgb[2]; blueTarget = rgb[3]; if (pressed = true){ makeJPegFile(freezeFrame, "C:\\", "test.jpg", 1.0f); postFileToITP("C:\\", "test.jpg", "/home/clc219/public_html", "WC" + getTimeString() + ".jpg" ); } } public void postFileToITP(String _localPath, String _localFilename, String _pathname, String _filename) { DataOutputStream dataOut; String urlString = "http://itp.nyu.edu/~clc219/cgi-bin/UpLoad.php";; URLConnection urlConn; URL url; try { url = new URL(urlString); urlConn = url.openConnection(); urlConn.setDoInput(true); urlConn.setDoOutput(true); urlConn.setUseCaches(false); urlConn.setRequestProperty("Content-Type", "multipart/form-data, boundary=AaB03x"); dataOut = new DataOutputStream(urlConn.getOutputStream()); dataOut.writeBytes("--AaB03x\r\n"); dataOut.writeBytes("Content-Disposition: form-data; name=\"image_file\"; filename=\"" + URLEncoder.encode(_pathname + _filename) + "\"\r\n"); dataOut.writeBytes("Content-Type: image/jpeg\r\n"); dataOut.writeBytes("\r\n"); FileInputStream fis = new FileInputStream(_localPath + _localFilename); System.out.println("output from " + _localPath + _localFilename + " to " + _pathname + _filename); byte buffer[] = new byte[1000]; int len; while ((len = fis.read(buffer)) != -1) { dataOut.write(buffer, 0, len); } fis.close(); dataOut.close(); dataOut.writeBytes("\r\n--AaB03x--\r\n"); dataOut.flush(); dataOut.close(); System.out.println("waiting for server"); InputStream in = urlConn.getInputStream(); int input = 0; String inputSt = ""; while (input != -1) { input = in.read(); inputSt = inputSt + ((char) input); } System.out.println("from Server" + inputSt); } catch (MalformedURLException me) { System.err.println("MalformedURLException: " + me); } catch (IOException ioe) { System.err.println("IOException: " + ioe.getMessage()); } } public void ftp_utility(String localPath, String localFilename, String serverLogin, String serverPassword, String serverAddress, String serverPath, String serverFilename) { FtpClient fcMyFtp = new FtpClient(); try { int ch; fcMyFtp.openServer(serverAddress); fcMyFtp.login(serverLogin, serverPassword); TelnetInputStream tisList = fcMyFtp.list(); while ((ch = tisList.read()) != -1) ; fcMyFtp.cd(serverPath); fcMyFtp.binary(); FileInputStream fis = new FileInputStream(localPath + localFilename); TelnetOutputStream tos = fcMyFtp.put(serverFilename); byte buffer[] = new byte[1000]; int len; while ((len = fis.read(buffer)) != -1) { tos.write(buffer, 0, len); } fis.close(); tos.close(); } catch (IOException e) { e.printStackTrace(); } } public void makeJPegFile(BufferedImage bi, String pathname, String filename, float quality) { try { // convert it to an image without an alpha channel for the Jpeg BufferedImage biOut = new BufferedImage(bi.getWidth(), bi.getHeight(), BufferedImage.TYPE_INT_RGB); Graphics biOutG = biOut.getGraphics(); biOutG.drawImage(bi, 0, 0, null); File outputFile = new File(pathname + filename); FileOutputStream fos = new FileOutputStream(outputFile); BufferedOutputStream bufOut = new BufferedOutputStream(fos); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bufOut); JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(biOut); param.setQuality(quality, false); encoder.setJPEGEncodeParam(param); encoder.encode(biOut); System.out.println(" jpeg " + pathname + filename); bufOut.close(); } catch (FileNotFoundException e) { System.out.println("file not found" + pathname + filename); } catch (IOException e) { System.out.println("io execption" + pathname + filename); } } public static void main(String[] oogs){ BestPixelTracker myTracker = new BestPixelTracker(); myTracker.startVideo(); } }