GithubHelp home page GithubHelp logo

Comments (5)

serratus avatar serratus commented on August 25, 2024

Web workers would indeed make a lot of sense in this context. The main problem to solve when using web workers is how to pass the image-data into the worker. Sure, there is the option to use Transferables, but then I lose access to the already allocated memory and have to re-allocate the buffer for every new frame. And copying the entire buffer to the web worker context does only waste resources. Do you have experience in working with web workers? Do you know how to solve this problem in a memory- and CPU efficient way?

from quaggajs.

pushplay avatar pushplay commented on August 25, 2024

Worker.postMessage works in both directions, including the list of Transferables. So you can get your buffer back. It doesn't make sense to even try to process frames while the web worker is going so just ignore those frames until the web worker returns.

from quaggajs.

SunboX avatar SunboX commented on August 25, 2024

I would do something like this:

var video = document.querySelector('#video'),
    canvas = document.createElement('canvas');

navigator.getMedia(
    {
        video: true,
        audio: false
    },
    function(stream) {

        if (navigator.mozGetUserMedia) {

            video.mozSrcObject = stream;

        } else {

            var vendorURL = window.URL || window.webkitURL;
            video.src = vendorURL ? vendorURL.createObjectURL(stream) : stream;

        }

        video.play();

        decodeBarcode();
    },
    function(err) {
        console.log('An error occured! ' + err);
    }
);

var workerCount = 0,
    ctx = canvas.getContext('2d')

function decodeBarcode() {
    if (workerCount === 0) {
        if (decodeWorker === null) {
            decodeWorker = new Worker('decoder.js');
            decodeWorker.onmessage = receiveMessage;
        }
        workerCount++;

        ctx.drawImage(video, 0, 0, canvas.width, canvas.height);

        decodeWorker.postMessage({
            imageData: ctx.getImageData(0, 0, canvas.width, canvas.height).data,
            width: canvas.width,
            height: canvas.height
        });
    }
}

function receiveMessage(e) {

    workerCount--;

    if (e.data.success === true) {

        alert(e.data.result);
    }

    decodeBarcode();
}

from quaggajs.

serratus avatar serratus commented on August 25, 2024

Finally, after multiple iterations of different approaches, I managed to find a good solution which works for most use-cases. If you are interested, take a look at the issue-1a branch. Feedback is greatly appreciated.

from quaggajs.

serratus avatar serratus commented on August 25, 2024

This issue should be solved with #12

from quaggajs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.