Bonjour,

Bonjour j'ai effectué un lecteur de code barre classique qui peut lire les ean13 par exemple mais j'aurais aussi besoin qu'il puisse lire et décoder les data matrix (2d) mais le problème c'est que je n'arrive pas a modifier ceci .

Ce que je fais

mon js

// déclaration d'un objet barcode on lui passe 3 paramètre
var Barcode = function (context, width, height) {
    this.context = context;
    this.width = width;
    this.height = height;
    this.debug = false;
};

Barcode.MAX_VARIANCE_SEPARATOR = 0.9;
Barcode.MAX_VARIANCE = 0.27;

Barcode.prototype.scan = function () {

    var horizontal = 2;

    while (horizontal--) {

        var length = horizontal ? this.width : this.height;
        var scan = 10;
        var step = length / scan;

        while (scan--) {

            var xy = (step * scan * 1.5) % length;

            if (horizontal) {
                var x = 0;
                var y = xy;
                var width = length;
                var height = 1;
            } else {
                var x = xy;
                var y = 0;
                var width = 1;
                var height = length;
            }

            var data = this.context.getImageData(x, y, width, height).data;
            var grey = Barcode.grey(data);

            for (var contrast = 10; contrast <= 750; contrast += 10) {

                var bits = Barcode.convert(grey, contrast);

                var reverse = 2;
                while (reverse--) {

                    var line = new Barcode.Line(bits, x, y, width, height, horizontal);

                    if (line.parse()) {
                        return line;
                    }

                    bits.reverse();
                }
            }
        }
    }

    return false;
};

Barcode.prototype.print = function (line) {

    for (var i = 0; i < line.bits.length; i++) {

        this.context.fillStyle = 'rgb(' + line.bits[i] + ', ' + line.bits[i] + ', ' + line.bits[i] + ')';

        if (line.horizontal) {
            this.context.fillRect(i, line.y, 1, 100);
        } else {
            this.context.fillRect(line.x, i, 100, 1);
        }
    }

    this.context.fillStyle = 'rgba(255, 0, 0, 0.5)';
    if (line.horizontal) {
        this.context.fillRect(line.x, line.y, line.width, 5);
    } else {
        this.context.fillRect(line.x, line.y, 5, line.height);
    }
};

Barcode.grey = function (data) {

    var grey = [];

    for (var i = 0, n = data.length; i < n; i += 4) {
        grey[grey.length] = data[i] + data[i + 1] + data[i + 2];
    }

    return grey;
};

Barcode.convert = function (grey, contrast) {

    var bits = [];

    for (var i = 0, n = grey.length; i < n; i++) {
        bits[i] = grey[i] < contrast ? 0 : 255;
    }

    return bits;
};

Barcode.runlength = function (bits) {

    var lines = [];
    var current = bits[0];
    var count = 0;
    for (var col = 0; col < bits.length; col++) {
        if (bits[col] == current) {
            count++;
        } else {
            lines.push(count);
            count = 1;
            current = bits[col];
        }
    }
    lines.push(count);

    return lines;
};

Barcode.Line = function (bits, x, y, width, height, horizontal) {
    this.bits = bits;
    this.x = x;
    this.y = y;
    this.width = width;
    this.height = height;
    this.horizontal = horizontal;

    this.start = 0;
    this.bar = 0;
    this.digits = [];

    this.isbn = '';
};

Barcode.Line.prototype.parse = function () {

    var lines = Barcode.runlength(this.bits);

    // find start
    var bar = 0, start = 0, end = 0;
    for (var i = 0; i < (lines.length - 3); i++) {

        var all = lines.slice(i, i + 59);

        var total = 0;
        for (var j = 0; j < all.length; j++) {
            total += all[j];
        }
        var bar = total / 95;

        var variance = (lines[i] / bar) * (lines[i + 1] / bar) * (lines[i + 2] / bar);

        if (Math.abs(1 - variance) < Barcode.MAX_VARIANCE_SEPARATOR) {

            // check middle
            var variance = (lines[i + 27] / bar) * (lines[i + 28] / bar) * (lines[i + 29] / bar) * (lines[i + 30] / bar) * (lines[i + 31] / bar);

            if (Math.abs(1 - variance) < Barcode.MAX_VARIANCE_SEPARATOR) {

                // check end
                var variance = (lines[i + 56] / bar) * (lines[i + 57] / bar) * (lines[i + 58] / bar);

                if (Math.abs(1 - variance) < Barcode.MAX_VARIANCE_SEPARATOR) {

                    end = i + 59;
                    start = i + 3;

                    // start, middle and end found
                    break;
                }
            }
        }
    }

    if (end == 0) {

        // no end found
        return false;
    }

    // decode barcode
    var GROUP = 6;

    var isbn = '';
    var sum = '';

    var bars = lines.slice(start, start + 4 * GROUP);
    bars = bars.concat(lines.slice(start + 4 * GROUP + 5, start + 4 * 2 * GROUP + 5));
    for (var i = 0; i < 2 * GROUP; i++) {

        var digits = [
            bars[i * 4],
            bars[i * 4 + 1],
            bars[i * 4 + 2],
            bars[i * 4 + 3]
        ];
        this.digits.push(digits);

        var pattern = Barcode.EAN13.match(digits, bar);
        if (pattern) {
            sum += 'L';
        } else {
            sum += 'G';
            pattern = Barcode.EAN13.match(digits.reverse(), bar);
        }

        if (!pattern) {
            // no pattern match found
            return false;
        }

        isbn += pattern;
    }

    var first = Barcode.EAN13.FIRST_DIGITS[sum.substr(0, 6)] || false;

    if (!first) {
        // no first pattern found
        return false;
    }

    this.isbn = first + isbn;

    return Barcode.EAN13.checksum(this.isbn);
};

Barcode.EAN13 = {
    PATTERNS: [
        [3, 2, 1, 1], // 0
        [2, 2, 2, 1], // 1
        [2, 1, 2, 2], // 2
        [1, 4, 1, 1], // 3
        [1, 1, 3, 2], // 4
        [1, 2, 3, 1], // 5
        [1, 1, 1, 4], // 6
        [1, 3, 1, 2], // 7
        [1, 2, 1, 3], // 8
        [3, 1, 1, 2]  // 9
    ],
    FIRST_DIGITS: {
        'LLLLLL': '0',
        'LLGLGG': '1',
        'LLGGLG': '2',
        'LLGGGL': '3',
        'LGLLGG': '4',
        'LGGLLG': '5',
        'LGGGLL': '6',
        'LGLGLG': '7',
        'LGLGGL': '8',
        'LGGLGL': '9'
    },
    checksum: function (isbn) {

        var length = isbn.length;
        var sum = 0;

        for (var i = 0; i < length; i++) {

            if (i % 2 == 0) {
                sum += parseInt(isbn[i]);
            } else {
                sum += parseInt(isbn[i]) * 3;
            }
        }

        return sum % 10 == 0;
    },
    match: function (digits, bar) {

        var best = Barcode.MAX_VARIANCE;
        var variance = 0;
        var match = false;

        for (var j = 0; j < this.PATTERNS.length; j++) {

            variance = this.variance(digits, this.PATTERNS[j], bar);

            if (variance < best) {
                best = variance;
                match = "" + j;
            }
        }

        return match;
    },
    variance: function (digits, pattern, bar) {

        var sum = digits[0] + digits[1] + digits[2] + digits[3];
        if (isNaN(sum)) {
            return 9999;
        }
        var total = 0;

        total += Math.abs(digits[0] - pattern[0] * bar);
        total += Math.abs(digits[1] - pattern[1] * bar);
        total += Math.abs(digits[2] - pattern[2] * bar);
        total += Math.abs(digits[3] - pattern[3] * bar);

        return total / sum;
    }
};

mon html

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>Giphar</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <link rel="stylesheet" href="../Public/bootstrap.min.css">
        <link rel="stylesheet" href="../Public/style.css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
        <script src="../Public/jquery.min.js" charset="utf-8"></script>
    </head>
    <body>

        <!-- appel le menu -->
        <?php require('../Modele/m_menu.php'); ?><br>

        <form style="text-align:center" action="v_DataMatrix.php" method="post">
            <input type="submit" name="" value="DataMatrix" class="btn btn-info">
        </form>
        <form id="form"style="">
            <div style="border-bottom: 1px solid #ddd;">
                <input type="file" id="file" style="width: 280px; line-height: 18px; padding: 20px;" />
            </div><br>
            <p id="isbn" style="">
                s'il vous plait selectionner une photo d'un code barre (Ean13 ...)
            </p>

            <canvas id="canvas"></canvas>
        </form>
        <script src="../Controleur/c_scan.js"></script>
        <script type="text/javascript" >
            try {
                document.getElementById('file').onchange = function() {
                    document.getElementById('isbn').innerHTML = 'Chargement…';
                    var image = new Image();
                    image.onload = function () {
                        var canvas = document.getElementById('canvas');
                        var width = canvas.width;
                        var height = canvas.height;
                        var context = canvas.getContext('2d');
                        context.drawImage(image, 0, 0, width, height);
                        var barcode = new Barcode(context, width, height);
                        var line = barcode.scan();

                        if (line) {
                            document.getElementById('isbn').innerHTML = "produit trouvé...";
                             barcode.print(line);
                             document.getElementById('codeB').value = line.isbn;

                        } else {
                            document.getElementById('isbn').innerHTML = 'Désolé, ce code barre est introuvable ...';
                        }
                    };
                    image.src = window.webkitURL.createObjectURL(this.files[0]);
                };
            } catch (e) {
                alert(e);
            }
      </script>
      <form action="../Controleur/c_fichePcodeB.php" method="post">
         <input type="text" name="codeB" id="codeB" hidden="">
         <input style='margin-top:30%;margin-left:35%' type="submit" name="btnCodeB" value="fiche produit" class="btn btn-success">
       </form>
    </body>
</html>

merci de votre aide et de votre temps

Aucune réponse