Skip to content

Instantly share code, notes, and snippets.

@jojobyte
Forked from tbtlr/get_barcode_from_image.js
Created May 28, 2013 20:14
Show Gist options
  • Select an option

  • Save jojobyte/5665737 to your computer and use it in GitHub Desktop.

Select an option

Save jojobyte/5665737 to your computer and use it in GitHub Desktop.

Revisions

  1. @invalid-email-address Anonymous revised this gist Jun 21, 2010. 1 changed file with 8 additions and 7 deletions.
    15 changes: 8 additions & 7 deletions get_barcode_from_image.js
    Original file line number Diff line number Diff line change
    @@ -21,12 +21,13 @@
    var doc = document,
    img = "object" == typeof imgOrId ? imgOrId : doc.getElementById(imgOrId),
    canvas = doc.createElement("canvas"),
    ctx = canvas.getContext("2d"),
    width = img.width,
    height = img.height,
    ctx = canvas.getContext("2d"),
    spoints = [1, 9, 2, 8, 3, 7, 4, 6, 5],
    numLines = spoints.length,
    slineStep = height / (numLines + 1);
    slineStep = height / (numLines + 1),
    round = Math.round;
    canvas.width = width;
    canvas.height = height;
    ctx.drawImage(img, 0, 0);
    @@ -77,10 +78,10 @@
    if(code.length < 6){ var group = lines.slice(i * 4, (i * 4) + 4); }
    else{ var group = lines.slice((i * 4 ) + 5, (i * 4) + 9); }
    var digits = [
    Math.round(group[0] / bar),
    Math.round(group[1] / bar),
    Math.round(group[2] / bar),
    Math.round(group[3] / bar)
    round(group[0] / bar),
    round(group[1] / bar),
    round(group[2] / bar),
    round(group[3] / bar)
    ];
    code += u[digits.join('')] || u[digits.reverse().join('')] || 'X';
    if(12 == code.length){ return code; break; }
    @@ -89,4 +90,4 @@
    }
    return false;
    }
    })();
    })();
  2. @invalid-email-address Anonymous revised this gist Jun 5, 2010. 1 changed file with 4 additions and 3 deletions.
    7 changes: 4 additions & 3 deletions get_barcode_from_image.js
    Original file line number Diff line number Diff line change
    @@ -26,11 +26,12 @@
    height = img.height,
    spoints = [1, 9, 2, 8, 3, 7, 4, 6, 5],
    numLines = spoints.length,
    slineStep = height / numLines;
    slineStep = height / (numLines + 1);
    canvas.width = width;
    canvas.height = height;
    ctx.drawImage(img, 0, 0);
    while(numLines--){
    console.log(spoints[numLines]);
    var pxLine = ctx.getImageData(0, slineStep * spoints[numLines], width, 2).data,
    sum = [],
    min = 0,
    @@ -70,7 +71,7 @@
    }
    }
    var code = '',
    bar = Math.round((lines[1] + lines[2] + lines[3]) / 3),
    bar = ~~((lines[1] + lines[2] + lines[3]) / 3),
    u = UPC_SET;
    for(var i = 1, l = lines.length; i < l; i++){
    if(code.length < 6){ var group = lines.slice(i * 4, (i * 4) + 4); }
    @@ -82,7 +83,7 @@
    Math.round(group[3] / bar)
    ];
    code += u[digits.join('')] || u[digits.reverse().join('')] || 'X';
    if(12 == code.length){ break; }
    if(12 == code.length){ return code; break; }
    }
    if(-1 == code.indexOf('X')){ return code || false; }
    }
  3. @invalid-email-address Anonymous revised this gist Jun 5, 2010. 1 changed file with 5 additions and 4 deletions.
    9 changes: 5 additions & 4 deletions get_barcode_from_image.js
    Original file line number Diff line number Diff line change
    @@ -24,13 +24,14 @@
    ctx = canvas.getContext("2d"),
    width = img.width,
    height = img.height,
    numLines = 3,
    slineStep = height / (numLines + 1);
    spoints = [1, 9, 2, 8, 3, 7, 4, 6, 5],
    numLines = spoints.length,
    slineStep = height / numLines;
    canvas.width = width;
    canvas.height = height;
    ctx.drawImage(img, 0, 0);
    while(numLines--){
    var pxLine = ctx.getImageData(0, slineStep * numLines, width, 2).data,
    var pxLine = ctx.getImageData(0, slineStep * spoints[numLines], width, 2).data,
    sum = [],
    min = 0,
    max = 0;
    @@ -83,7 +84,7 @@
    code += u[digits.join('')] || u[digits.reverse().join('')] || 'X';
    if(12 == code.length){ break; }
    }
    if(-1 == code.indexOf('X')){ return code; }
    if(-1 == code.indexOf('X')){ return code || false; }
    }
    return false;
    }
  4. @invalid-email-address Anonymous revised this gist Jun 2, 2010. 1 changed file with 7 additions and 9 deletions.
    16 changes: 7 additions & 9 deletions get_barcode_from_image.js
    Original file line number Diff line number Diff line change
    @@ -74,15 +74,13 @@
    for(var i = 1, l = lines.length; i < l; i++){
    if(code.length < 6){ var group = lines.slice(i * 4, (i * 4) + 4); }
    else{ var group = lines.slice((i * 4 ) + 5, (i * 4) + 9); }
    var digits = '' + Math.round(group[0] / bar)
    + Math.round(group[1] / bar)
    + Math.round(group[2] / bar)
    + Math.round(group[3] / bar),
    invert = '' + Math.round(group[3] / bar)
    + Math.round(group[2] / bar)
    + Math.round(group[1] / bar)
    + Math.round(group[0] / bar);
    code += u[digits] || u[invert] || 'X';
    var digits = [
    Math.round(group[0] / bar),
    Math.round(group[1] / bar),
    Math.round(group[2] / bar),
    Math.round(group[3] / bar)
    ];
    code += u[digits.join('')] || u[digits.reverse().join('')] || 'X';
    if(12 == code.length){ break; }
    }
    if(-1 == code.indexOf('X')){ return code; }
  5. @invalid-email-address Anonymous revised this gist Jun 1, 2010. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion get_barcode_from_image.js
    Original file line number Diff line number Diff line change
    @@ -59,7 +59,7 @@
    }
    var curr = bmp[0],
    count = 1,
    lines = Array();
    lines = [];
    for(var col = 0; col < width; col++){
    if(bmp[col] == curr){ count++; }
    else{
  6. @invalid-email-address Anonymous created this gist Jun 1, 2010.
    92 changes: 92 additions & 0 deletions get_barcode_from_image.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,92 @@
    /*
    * Copyright (c) 2010 Tobias Schneider
    * This script is freely distributable under the terms of the MIT license.
    */

    (function(){
    var UPC_SET = {
    "3211": '0',
    "2221": '1',
    "2122": '2',
    "1411": '3',
    "1132": '4',
    "1231": '5',
    "1114": '6',
    "1312": '7',
    "1213": '8',
    "3112": '9'
    };

    getBarcodeFromImage = function(imgOrId){
    var doc = document,
    img = "object" == typeof imgOrId ? imgOrId : doc.getElementById(imgOrId),
    canvas = doc.createElement("canvas"),
    ctx = canvas.getContext("2d"),
    width = img.width,
    height = img.height,
    numLines = 3,
    slineStep = height / (numLines + 1);
    canvas.width = width;
    canvas.height = height;
    ctx.drawImage(img, 0, 0);
    while(numLines--){
    var pxLine = ctx.getImageData(0, slineStep * numLines, width, 2).data,
    sum = [],
    min = 0,
    max = 0;
    for(var row = 0; row < 2; row++){
    for(var col = 0; col < width; col++){
    var i = ((row * width) + col) * 4,
    g = ((pxLine[i] * 3) + (pxLine[i + 1] * 4) + (pxLine[i + 2] * 2)) / 9,
    s = sum[col];
    pxLine[i] = pxLine[i + 1] = pxLine[i + 2] = g;
    sum[col] = g + (undefined == s ? 0 : s);
    }
    }
    for(var i = 0; i < width; i++){
    var s = sum[i] = sum[i] / 2;
    if(s < min){ min = s; }
    if(s > max){ max = s; }
    }
    var pivot = min + ((max - min) / 2),
    bmp = [];
    for(var col = 0; col < width; col++){
    var matches = 0;
    for(var row = 0; row < 2; row++){
    if(pxLine[((row * width) + col) * 4] > pivot){ matches++; }
    }
    bmp.push(matches > 1);
    }
    var curr = bmp[0],
    count = 1,
    lines = Array();
    for(var col = 0; col < width; col++){
    if(bmp[col] == curr){ count++; }
    else{
    lines.push(count);
    count = 1;
    curr = bmp[col];
    }
    }
    var code = '',
    bar = Math.round((lines[1] + lines[2] + lines[3]) / 3),
    u = UPC_SET;
    for(var i = 1, l = lines.length; i < l; i++){
    if(code.length < 6){ var group = lines.slice(i * 4, (i * 4) + 4); }
    else{ var group = lines.slice((i * 4 ) + 5, (i * 4) + 9); }
    var digits = '' + Math.round(group[0] / bar)
    + Math.round(group[1] / bar)
    + Math.round(group[2] / bar)
    + Math.round(group[3] / bar),
    invert = '' + Math.round(group[3] / bar)
    + Math.round(group[2] / bar)
    + Math.round(group[1] / bar)
    + Math.round(group[0] / bar);
    code += u[digits] || u[invert] || 'X';
    if(12 == code.length){ break; }
    }
    if(-1 == code.indexOf('X')){ return code; }
    }
    return false;
    }
    })();