//------------------------------------------------------------------------------ // webcam.java - Displays a 240x320 image and reloads the image every x seconds. // Supports animated waiting/loading images and an online/offline // status. // Copyright 2002, All rights reserved // Version: 2.1 // Author: Jon McCain // Created: 9/11/2000 // Updated: 2/13/2002 - changed to read image,delay,statusfile from a config // file for security reasons. // 6/6/2002 - added parameters to so as to allow any image size // added parameters to control blink,logo,status imgs //------------------------------------------------------------------------------ // This program is free software; you can redistribute it and/or modify it // under the terms of the GNU General Public License as published by the // Free Software Foundation; either version 2 of the License, or (at your // option) any later version. // // This program is distributed in the hope that it will be useful, but // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License // for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software Foundation, // Inc., 675 Mass Ave, Cambridge, MA 02139, USA. //----------------------------------------------------------------------------- //Parameters: CamNum - cam number. Used to get things from the config file // By putting the real stuff in a config file // this makes things more secure because users can't // see the name of the main image file // or change delay time. // ImgWidth - width of the picture, defaults to 320 // ImgHeight - height of the picture, defaults to 240 // ImgBlinkX - horizontal position of blinking LEDs, defaults to 5 // (wait1.gif,wait2.gif,uploading.gif) // use 0 to hide it // ImgLogoX - horizontal position of the logo, defaults to 75 // use 0 to hide it // ImgStatusX - horizontal position of the online/offline // defaults to 250 // use 0 to hide it // //Other images: online.gif // offline.gif // wait1.gif - "status" image shown when offline // also shown every other second when online // Useful for "blinking" effect // wait2.gif - "status" image shown every other second when online // upload.gif - animated gif shown when image being uploaded // logo.jpg //Configuration file (webcam.conf): //There are three entries for each cam you want to use, up to 9 cams //For extra security, change name and/or location of ConfigFile // //image1=camimg.jpg ;the cam pict to display //delay1=60 ;# of seconds to wait //statusfile1=stat.txt ;status file, just contains 1 or 0 // ;1=cam online,0=cam offline //image2=otherpict.jpg ;settings for cam #2 //delay2=120 //statusfile2=stat2.txt import java.io.*; import java.awt.*; import java.net.*; import java.applet.*; import java.util.*; public class webcam extends java.applet.Applet implements Runnable { Thread webcam = null; MediaTracker tracker; Image OffScreenImg; Graphics OffScreenG; Image ImgCam,ImgOnline,ImgOffline; Image ImgWait1,ImgWait2,ImgUpload,ImgLogo; String s,StatusFile,ImgFile; int delay,i,blink; boolean CamActive,uploading; boolean running; String CamNum; int ImgWidth,ImgHeight,ImgBlinkX,ImgLogoX,ImgStatusX; static String ConfigFile = "webcam.conf"; public String getAppletInfo() { return "webcam 2.1 By Jon McCain, 2002"; } public boolean mouseEnter(java.awt.Event evt, int x, int y) { getAppletContext().showStatus("webcam v2.1"); return true; } //Get settings from the config file void ReadConfig() { DataInputStream dis; String line; //some defaults in case they are not in the config file delay = 120; ImgFile = "camimg.jpg"; StatusFile = "status.txt"; //open the file and read from it try { URL u = new URL(getCodeBase() + ConfigFile); dis = new DataInputStream( u.openConnection().getInputStream() ); if (dis != null) { while ((line = dis.readLine()) != null) { if (line.length() > 5) { if (line.substring(0,5).equalsIgnoreCase("delay") && line.substring(5,6).equals(CamNum)) { delay = Integer.valueOf(line.substring(7,line.length())).intValue(); } if (line.substring(0,5).equalsIgnoreCase("image") && line.substring(5,6).equals(CamNum)) { ImgFile = line.substring(7,line.length()); } if (line.substring(0,10).equalsIgnoreCase("statusfile") && line.substring(10,11).equals(CamNum)) { StatusFile = line.substring(12,line.length()); } } } } dis.close(); dis = null; } catch (Exception e) { System.out.println( e ); } System.out.println(delay); System.out.println(ImgFile); System.out.println(StatusFile); } public void init() { // load the images of the menu tracker = new MediaTracker(this); s = getParameter("CamNum"); CamNum = (s != null) ? s : "1"; String at = getParameter("imgwidth"); ImgWidth = (at != null) ? Integer.valueOf(at).intValue() : 320; at = getParameter("imgheight"); ImgHeight = (at != null) ? Integer.valueOf(at).intValue() : 240; at = getParameter("imgblinkx"); ImgBlinkX = (at != null) ? Integer.valueOf(at).intValue() : 5; at = getParameter("imglogox"); ImgLogoX = (at != null) ? Integer.valueOf(at).intValue() : 75; at = getParameter("imgstatusx"); ImgStatusX = (at != null) ? Integer.valueOf(at).intValue() : 250; ReadConfig(); //to save time, start loading the images ImgCam = getImage(getCodeBase(), ImgFile); tracker.addImage(ImgCam, 0); ImgOnline = getImage(getCodeBase(), "online.gif"); tracker.addImage(ImgOnline, 0); ImgOffline = getImage(getCodeBase(), "offline.gif"); tracker.addImage(ImgOffline, 0); ImgWait1 = getImage(getCodeBase(), "wait1.gif"); tracker.addImage(ImgWait1, 0); ImgWait2 = getImage(getCodeBase(), "wait2.gif"); tracker.addImage(ImgWait2, 0); ImgUpload = getImage(getCodeBase(), "upload.gif"); tracker.addImage(ImgUpload, 0); ImgLogo = getImage(getCodeBase(), "logo.jpg"); tracker.addImage(ImgLogo, 0); OffScreenImg = createImage(size().width, size().height ); OffScreenG = OffScreenImg.getGraphics(); } //look in the status file to see if this cam is online boolean IsOnLine() { DataInputStream dis; String online; //look at the status file online = "0"; try { URL u = new URL(getCodeBase() + StatusFile); dis = new DataInputStream( u.openConnection().getInputStream() ); if (dis != null) { online = dis.readLine(); } dis.close(); dis = null; } catch (Exception e) { System.out.println( e ); } if (online.startsWith("1")) { return true; } else { return false; } } public void run() { Thread.currentThread().setPriority(Thread.MIN_PRIORITY); //if user hits reload in the browser it's possible to get many //instances all going at the same time. This helps to keep only //one active one. if (running) { return; } running = true; //Wait until all of the images get loaded. if (tracker != null) { try { tracker.waitForAll(); } catch (InterruptedException e) {} } tracker = null; while (webcam != null) { CamActive = IsOnLine(); blink = 1; repaint(); getAppletContext().showStatus(""); for (i=delay; i>0; i--) { //only animate things if cam online if (CamActive) { getAppletContext().showStatus(String.valueOf(i)); if (blink == 1) blink = 2; else blink = 1; repaint(); } //wait 1 second, this is for the "waiting/blinking" aminimation try { Thread.sleep(1000); } catch (InterruptedException e) { } } //for (i=delay) //Get a new picture only if the cam is online if (CamActive) { getAppletContext().showStatus("Loading...."); ImgCam.flush(); uploading = true; //so we see the "uploading" image animation repaint(); tracker = new MediaTracker(this); tracker.addImage(ImgCam, 0); try { tracker.waitForAll(); } catch (InterruptedException e) {} uploading = false; //turn off the "uploading" image } } //while(webcam != null) } //run public void update(Graphics g) { paint(OffScreenG); // draws on the buffer g.drawImage(OffScreenImg,0,0, this); // draws the buffer onto applet } public void paint(Graphics g) { Dimension d = size(); //don't refresh the picture if in middle of uploading it if (!uploading) { //first let's make the background black g.setColor(Color.black); g.fillRect(0,0,d.width, d.height); //show the cam image if (ImgCam != null) { g.drawImage(ImgCam,0,0,ImgWidth,ImgHeight,this); } } //blinking LEDs if (ImgBlinkX > 0) { if (uploading) { g.drawImage(ImgUpload,ImgBlinkX,ImgHeight + 2,this); } else { if (blink == 1) { g.drawImage(ImgWait1,ImgBlinkX,ImgHeight + 2,this); } else { g.drawImage(ImgWait2,ImgBlinkX,ImgHeight + 2,this); } } } //lets see the logo if (ImgLogoX > 0) { g.drawImage(ImgLogo,ImgLogoX,ImgHeight + 2,this); } //show cam status if (ImgStatusX > 0) { if (CamActive) { g.drawImage(ImgOnline,ImgStatusX,ImgHeight + 2,this); } else { g.drawImage(ImgOffline,ImgStatusX,ImgHeight + 2,this); } } } //paint public void start() { if (webcam == null) { webcam = new Thread(this); webcam.start(); } } public void stop() { webcam = null; } }