1

I am developing an application where i am capturing images from a camera and streaming them online. To capture images i am using an opencv code. It stores(rewrites) the image on the same image file on server. This image is then streamed online. However if the streaming and storing take place simultaneously then the image gets corrupt. So i have synchronized those processes using system locks. The html page that checks that reloads the image every 2 secs checks if the img file has a system lock. If yes then it doesn't reload the image. So to check whether the file is locked or not i have to put some php code in my html file. I believe that my code is sytactically correct. Yet it is not giving the desired output.

 <html>

 <head>

 <!--to keep requesting the server new page without displaying the image from the cache pragma
  is used -->
 <META HTTP-EQUIV="Pragma" CONTENT="no-cache">

 <title>YOUR SITE TITLE GOES HERE</title>
 <style type="text/css">
    #imgJSselbox{
        position: absolute;
        margin: 0px;
        padding: 0px;
        visibility: hidden;
        width: 0px;
        height: 0px;
        border: 1px solid #006;
        color: #fff;
        background-image: url(selection_area.gif);
        z-index: 20;
    }
 </style>
 <script type="text/javascript" src="globalvar.js"></script>
 <script language="Javascript">

       var x = 2;
       var y = 1;
       var now;

  function startClock() {
        x = x-y;
        document.form1.clock.value = x;
   <?php
       $fp1=fopen("a.jpg","r");
       $fp2=fopen("b.jpeg","r");
       if(flock($fp1,LOCK_EX|LOCK_NB)==0 && flock($fp2,LOCK_EX|LOCK_NB)==0)
     $x=1;
       else
     $x=0;
    ?>

    var ch;
    ch=<?php echo $x; ?>

     if (x < 1 && ch==1) reload();
    <?php
        $fp1=fopen("a.jpg","r");
        $fp2=fopen("b.jpeg","r");
        flock($fp1,LOCK_UN); 
        flock($fp2,LOCK_UN);
    ?>
    timerID = setTimeout("startClock()", 1000);
   }

   function reload()
   {
        now = new Date();
         <!-- to pass the new image to the src -->
        var camImg = "a.jpg" + "?" + now.getTime();
        document.campicture.src = camImg;
        var crpImg = "b.jpeg" + "?" + now.getTime();
        document.croppic.src = crpImg;  

  x = 2;
        document.form1.clock.value = x; 
    }

    </script>
    <script type="text/javascript" src="image_cropper.js">
    document.form1.x-coord.value=x1;
    document.write(x1);


    </script>

    <script>
    function window.open('im','imgg')
    {
           window.open('im','imgg');
    }
    </script>
    </head>
     <body bgcolor="white" onLoad="startClock()">

    <center>

     <font size=-1>

     <h1>AYS WEBCAM PAGE</h1>

     <h2>This page loads images and refreshes them after every given interval of time keeping
     the same name of the image file</h2><p><br />
<div id="imgJSselbox"></div>
</center>
      <img name="campicture" src="a.jpg" id="testimage" border=1 width=640 height=480
       alt="AYS    
         founder's IMAGE" onClick="getImageCropSelectionPoint
        'testimage',event,document.form1);">
             ====>>ROI==>> 

<img name="croppic" src="b.jpeg" width=320 height=240 alt="cropped_image">
<center>
       <form NAME="form1"action="image_cropper.php" method="POST"><b><font size="-1"
       face="Arial">AutoReload in :==> </font>

        <input TYPE="text" NAME="clock" SIZE="2" VALUE="5"> <font size="-1"
        face="Arial">seconds.</font></b><br />
<br><br><input type="button" value="Crop" onclick= setImageCropAreaSubmit
       ("image_cropper.php",'testimage')><br><br>
       </center>
       <!-- <input type="text" name="x-coord" value=""><br>
 <input type="text" name="y-coord" value=""><br>
 <input type="text" name="wid" value="" ><br>
 <input type="text" name="hght" value="" ><br>
       -->
       </form>
       </body>

      </html>

Thank you

7
  • What is the desired output? What is it giving instead? Commented Jun 5, 2012 at 21:33
  • i want it to check for an exclusive lock and if it gets on then reload the image. however that is not happening. when i add my php code it simply doesn't enter the startClock() function and subsequently the reload function. hence what i get is a passive page instead of a dynamic constantly reloading page. Commented Jun 5, 2012 at 21:40
  • Looks like your error is with Javascript then. Try using a script debugger like firebug for FireFox or if you use Chrome, push ctrl-shift-J Commented Jun 5, 2012 at 21:44
  • 1
    You also may want to change <script language="Javascript"> to <script type="text/javascript">. I'm not sure if that would have an effect. Commented Jun 5, 2012 at 21:46
  • It looks like your mixing PHP and JavaScript in an unconventional way. Your fopen/flock calls will only be called on Page Load. I don't see where you reload the page, so that only happens once. Worst case (personal project), you could add a new meta tag (<META HTTP-EQUIV="Refresh" CONTENT="60"> ) that will refresh the current page every 60 seconds... Otherwise, I'd recommend moving the fopen/flock checking calls into its own PHP page, and then using some AJAX (jQuery) to continually check for lock/reload needs. Commented Jun 5, 2012 at 22:00

1 Answer 1

1

Your problem is in the code timerID = setTimeout("startClock()", 1000);.

Use timerID = setTimeout(startClock, 1000); instead.

See window.setTimeout - MDN.

Sign up to request clarification or add additional context in comments.

1 Comment

Oops - it looks like it works fine as it is with timerID = setTimeout("startClock()", 1000); (at least on jsFiddle - jsfiddle.net/EdvGb), maybe the problem is in the PHP?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.