JavaScript条码生成技术求助:替换为Code 39 Full ASCII以生成完整长度条码
Alright, let's get your Code 39 Full ASCII barcode working properly. The issue you're hitting is that standard Code 39 only handles 43 basic characters, so when you try to use extended ASCII (like lowercase letters, special symbols), it either truncates or generates invalid barcodes. Here's how to tweak your barcode.js and script.js to fix this:
Step 1: Replace the Character Map in barcode.js
First, you need to update the Code 39 character set to include the Full ASCII escape sequences. Standard Code 39 uses 4 characters ($, /, %, +) as escape codes to represent extended ASCII characters. Replace your existing Code 39 mapping with this full version (include all standard and extended pairs):
// In barcode.js: Replace your existing code39 map with this const code39FullAsciiMap = { // Standard Code 39 characters '*': '101001101101', // Start/stop delimiter (required!) '0': '110100101011', '1': '101100101011', '2': '110110010101', '3': '101001101011', '4': '110100110101', '5': '101100110101', '6': '101001011011', '7': '110100101101', '8': '101100101101', '9': '101001101101', 'A': '110101001011', 'B': '101101001011', 'C': '110110100101', 'D': '101011001011', 'E': '110101100101', 'F': '101101100101', 'G': '101010011011', 'H': '110101001101', 'I': '101101001101', 'J': '101011001101', 'K': '110101010011', 'L': '101101010011', 'M': '110110101001', 'N': '101011010011', 'O': '110101101001', 'P': '101101101001', 'Q': '101010110011', 'R': '110101011001', 'S': '101101011001', 'T': '101011011001', 'U': '110010101011', 'V': '100110101011', 'W': '110011010101', 'X': '100101101011', 'Y': '110010110101', 'Z': '100110110101', '-': '100101011011', '.': '110010101101', ' ': '100110101101', '$': '100100100101', '/': '100100101001', '%': '100101001001', '+': '101001001001', // Full ASCII escape sequence pairs (examples - add all per standard) '$A': '110100101011', // Maps to 'a' '$B': '101100101011', // Maps to 'b' '$C': '110110010101', // Maps to 'c' // ... continue mapping all lowercase letters to $A-$Z '/A': '101001101011', // Maps to '[' '/B': '110100110101', // Maps to '\' '/C': '101100110101', // Maps to ']' // ... add all other extended ASCII mappings as per Code 39 Full ASCII standard };
Step 2: Add Full ASCII Encoding Logic to barcode.js
Next, create a function that handles converting extended characters to their escape sequence pairs. This function will process your input string, replace extended chars with the correct escape pairs, then encode the full sequence:
// In barcode.js: Add this encoding function function encodeCode39FullAscii(input) { if (!input || typeof input !== 'string') { throw new Error('Input must be a non-empty string'); } // Mapping of extended ASCII chars to Code 39 escape pairs const extendedCharMap = { 'a': '$A', 'b': '$B', 'c': '$C', 'd': '$D', 'e': '$E', 'f': '$F', 'g': '$G', 'h': '$H', 'i': '$I', 'j': '$J', 'k': '$K', 'l': '$L', 'm': '$M', 'n': '$N', 'o': '$O', 'p': '$P', 'q': '$Q', 'r': '$R', 's': '$S', 't': '$T', 'u': '$U', 'v': '$V', 'w': '$W', 'x': '$X', 'y': '$Y', 'z': '$Z', '[': '/A', '\\': '/B', ']': '/C', '^': '/D', '_': '/E', '`': '/F', '{': '/G', '|': '/H', '}': '/I', '~': '/J', '!': '%A', '"': '%B', '#': '%C', '$': '%D', '%': '%E', '&': '%F', '\'': '%G', '(': '%H', ')': '%I', '*': '%J', '+': '%K', ',': '%L', '/': '%M', ':': '%N', ';': '%O', '<': '%P', '=': '%Q', '>': '%R', '?': '%S', '@': '%T', // Add any other required extended characters here }; const encodedSegments = []; // Process each character in the input for (const char of input) { if (extendedCharMap[char]) { // Split escape pair into two characters and encode each const [escapeCode, charCode] = extendedCharMap[char]; encodedSegments.push(code39FullAsciiMap[escapeCode]); encodedSegments.push(code39FullAsciiMap[charCode]); } else if (code39FullAsciiMap[char]) { // Standard character - encode directly encodedSegments.push(code39FullAsciiMap[char]); } else { throw new Error(`Unsupported character: "${char}" (ASCII code: ${char.charCodeAt(0)})`); } } // Add mandatory start/stop delimiters (*) encodedSegments.unshift(code39FullAsciiMap['*']); encodedSegments.push(code39FullAsciiMap['*']); // Join segments with narrow spaces (1 unit of white space) return encodedSegments.join('0'); }
Step 3: Update script.js to Use the New Encoder
Finally, modify your script.js to call the new Full ASCII encoder instead of the old standard Code 39 function. Update your barcode generation logic like this:
// In script.js: Replace your existing generation logic with this function generateFullAsciiBarcode(inputText) { try { // Get the encoded barcode data const barcodeData = encodeCode39FullAscii(inputText); // Use your existing rendering function (canvas/SVG) to draw the barcode // Example render call (adjust to match your existing code): renderBarcode(barcodeData, { width: 2, // Bar width in pixels height: 100, // Bar height in pixels container: document.getElementById('barcode-container') }); } catch (error) { console.error('Failed to generate barcode:', error.message); // Show error to user if needed document.getElementById('error-message').textContent = error.message; } } // Example usage: // generateFullAsciiBarcode("HelloWorld123!@#");
Key Notes to Verify
- Start/Stop Delimiters: Always include the
*character at the start and end of the barcode - this is required for Code 39 scanners to recognize the barcode boundaries. - Test with Extended Characters: Try generating a barcode with lowercase letters, special symbols, and spaces to confirm the full length is preserved and the barcode scans correctly.
- Rendering Adjustments: Since extended characters use two Code 39 segments, the final barcode length will be longer than standard Code 39 for the same input - make sure your rendering container can accommodate the wider barcode.
内容的提问来源于stack exchange,提问作者Jay Melbourne

