2

Hello i'm building an application where i want to dynamically change the source of an image in order to force reload it . The problem is that in order of this i only get a broken image on the browser. Instead , if a run the function manually by a button it runs perfect .

HTML document

<!DOCTYPE html>
<html lang="en" ng-app='cameraApp'>
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Node JS Camera</title>
        <!-- Latest compiled and minified CSS -->
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">

        <!-- Optional theme -->
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">
        <script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
        <!-- Latest compiled and minified JavaScript -->
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular.min.js"></script>
        <script src='https://code.angularjs.org/1.4.4/angular-sanitize.min.js'></script>
        <script src="cameraApp.js"></script>
    </head>
    <body>
        <div class="container-fluid">
            <div class="jumbotron">
                <h1>Welcome to NodeJS Camera v1</h1>
            </div>
            <div ng-controller="HomeController">
                <div class="cameraControl col-md-5">
                    <p>Here is the camera control</p>
                    <button class="btn btn-default" ng-click="getSnapshot()">Snapshot</button>
                    <button class="btn btn-info" ng-click="intervalFunction()">Start Feed</button>    
                </div>
                <div class="lifeFeed col-md-7">
                    <p>Here is the live feed</p>
                    <p><button class="btn btn-default" ng-click="readSnapshot()">Snapshot Read</button></p>
                    <img width='600' height='600' ng-src="{{snapshot}}" alt="SnapShot taken">
                </div>
            </div>
        </div>
    </body>
</html>

cameraApp.js

var cameraApp = angular.module('cameraApp',[]);

cameraApp.controller('HomeController', function($scope,$http,$timeout) {
    function updateImage() {
        var img = 'snapshots/camera.jpg'+  '?decache=' + Math.random();
        console.log('Snapshot Loaded');
        $scope.snapshot = img;  
    };

    $scope.readSnapshot = updateImage;

    $scope.getSnapshot = function() {
        $http.get('/api/getSnapshot')
        .then(function(response) {
            // this callback will be called asynchronously
            // when the response is available
            console.log('Snapshot captured');
            $scope.readSnapshot();
        }, function(response) {
            console.log('Error in capturing...');
        }); 
    }

    $scope.intervalFunction = function() {
        $timeout(function() {
            $scope.getSnapshot();
            $scope.intervalFunction();
        }, 2000);
    };

    // Kick off the interval
    $scope.intervalFunction();
});
1
  • What are you getting in console? Commented Aug 15, 2015 at 17:50

1 Answer 1

1

There are two solutions I've used for this in the past.

1) Use an ng-if/ng-show on your img tag. This will prevent the broken image from displaying.

<img ng-if='snapshot'>

2) Set a default image that will load and then be replaced once the other images load.

$scope.snapshot = 'snapshots/default.png';
Sign up to request clarification or add additional context in comments.

4 Comments

If the function updateImage() is triggered by the button the image load ok , but if it is triggered by $scope.getSnapshot then i get the broken image
Right below your image tag put {{snapshot}} to see if snapshot is being populated.
It's being populated just fine , i've managed to get it working by putting the updateImage in a delay shorter than the intervalFunction , but now i get a blinking image as i try to simulate live feed . Is it better to use html canvas?
Yeah I was going to point out that your other problem is this code is asynchronous. You really need to wait until the image has loaded to call interval again. Like <img onload=getSnapshot> type of deal.

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.