UNPKG

javascript-barcode-reader

Version:
112 lines (93 loc) 2.83 kB
const DecoderCode93 = require('./code-93') const DecoderCode39 = require('./code-39') const DecoderEAN13 = require('./ean-13') const BARCODE_DECODERS = { 'code-93': DecoderCode93, 'code-39': DecoderCode39, 'ean-13': DecoderEAN13, } const barcodeDecoder = (imgOrId, options) => { const doc = document const img = typeof imgOrId === 'object' ? imgOrId : doc.getElementById(imgOrId) const width = img.naturalWidth const height = img.naturalHeight const canvas = doc.createElement('canvas') canvas.width = width canvas.height = height const ctx = canvas.getContext('2d') // check points for barcode location const spoints = [1, 9, 2, 8, 3, 7, 4, 6, 5] let numLines = spoints.length const slineStep = height / (numLines + 1) ctx.drawImage(img, 0, 0) // eslint-disable-next-line while ((numLines -= 1)) { // create section of height 2 const pxLine = ctx.getImageData(0, slineStep * spoints[numLines], width, 2) .data const sum = [] let min = 0 let max = 0 // grey scale section and sum of columns pixels in section for (let row = 0; row < 2; row += 1) { for (let col = 0; col < width; col += 1) { const i = (row * width + col) * 4 const g = (pxLine[i] * 3 + pxLine[i + 1] * 4 + pxLine[i + 2] * 2) / 9 const s = sum[col] pxLine[i] = g pxLine[i + 1] = g pxLine[i + 2] = g sum[col] = g + (s === undefined ? 0 : s) } } for (let i = 0; i < width; i += 1) { sum[i] /= 2 const s = sum[i] if (s < min) { min = s } if (s > max) { max = s } } // matches columns in two rows const pivot = min + (max - min) / 2 const bmp = [] for (let col = 0; col < width; col += 1) { let matches = 0 for (let row = 0; row < 2; row += 1) { if (pxLine[(row * width + col) * 4] > pivot) { matches += 1 } } bmp.push(matches > 1) } // matches width of barcode lines let curr = bmp[0] let count = 1 const lines = [] for (let col = 0; col < width; col += 1) { if (bmp[col] === curr) { count += 1 } else { lines.push(count) count = 1 curr = bmp[col] } } // TODO: If not found in first step, continue searching until while loop // Run the decoder return BARCODE_DECODERS[options.barcode].decode(lines) } return false } if (typeof exports !== 'undefined') { if (typeof module !== 'undefined' && module.exports) { exports = barcodeDecoder module.exports = barcodeDecoder } exports.barcodeDecoder = barcodeDecoder } else { root.barcodeDecoder = barcodeDecoder }