Last active
April 15, 2019 19:41
-
-
Save brianmfear/b85fabe80f2ccd92abc383dfc0b33a89 to your computer and use it in GitHub Desktop.
Basic Code 39 In Visualforce
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<apex:component controller="code39" access="global"> | |
<!-- global required to use in templates --> | |
<apex:attribute assignTo="{!shouldCheckDigit}" type="boolean" name="checkDigit" default="false" description="True if should append additional check character." /> | |
<!-- codeValue must be supplied, but cannot be required because of global attribute --> | |
<apex:attribute assignTo="{!sourceCodeValue}" type="string" name="codeValue" description="The string to encode." /> | |
<!-- style sheet must be included to work properly --> | |
<apex:stylesheet value="{!URLFOR($Resource.barcodeCSS)}"/> | |
<!-- leave some space on the side for scanners --> | |
<div style="display: inline-block; border: 1px solid black; min-width: 1in; padding: 0.25in 1in;" > | |
<div> | |
<apex:repeat value="{!barCodeBars}" var="bar"> | |
<apex:outputText value="" styleClass="{!if(bar='1','blackbar','whitebar')}" /> | |
</apex:repeat> | |
</div> | |
<div style="text-align: center"> | |
<!-- we'll show the output text on the bottom, too --> | |
{!sourceCodeValue} | |
</div> | |
</div> | |
</apex:component> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.blackbar, .whitebar { | |
display: inline-block; | |
overflow: hidden; | |
padding: 0; | |
width: 1px; | |
height: 1cm; | |
} | |
.blackbar { | |
background-color: black; | |
} | |
.whitebar { | |
background-color: white; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Code39Controller { | |
// Determines if the check digit should be generated | |
// If true, scanners must be enabled to use it | |
public Boolean shouldCheckDigit { get; set; } | |
// The source string to use. Currently only supports | |
// the characters in the "keys" string. Do not use '*'. | |
public String sourceCodeValue { get; set; } | |
// The index for supported characters. | |
static String keys = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%*'; | |
// The binary representation of each character, 16 bits each. | |
static String[] values = new String[] { | |
'1010001110111010', '1110100010101110', '1011100010101110', '1110111000101010', | |
'1010001110101110', '1110100011101010', '1011100011101010', '1010001011101110', | |
'1110100010111010', '1011100010111010', '1110101000101110', '1011101000101110', | |
'1110111010001010', '1010111000101110', '1110101110001010', '1011101110001010', | |
'1010100011101110', '1110101000111010', '1011101000111010', '1010111000111010', | |
'1110101010001110', '1011101010001110', '1110111010100010', '1010111010001110', | |
'1110101110100010', '1011101110100010', '1010101110001110', '1110101011100010', | |
'1011101011100010', '1010111011100010', '1110001010101110', '1000111010101110', | |
'1110001110101010', '1000101110101110', '1110001011101010', '1000111011101010', | |
'1000101011101110', '1110001010111010', '1000111010111010', '1000100010001010', | |
'1000100010100010', '1000101000100010', '1010001000100010', '1000101110111010' }; | |
// Renders the barcode on the screen | |
public String[] getBarCodeBars() { | |
return generateCode39(sourceCodeValue, shouldCheckDigit).split(''); | |
} | |
// Returns a string in case we also want to debug the output. | |
String generateCode39(String source, Boolean checkDigit) { | |
// Output | |
String[] result = new String[0]; | |
Integer index, // Temp variable | |
total = 0; // Checksum calculation | |
// Avoid System.NullPointerException | |
source = source == null? '': source; | |
// Start character is * | |
result.add(values[keys.indexOf('*')]); | |
// For each character in source | |
for(String sourceChar: source.toUpperCase().split('')) { | |
// Valid character, add to checksum and output bits | |
if((index = keys.indexOf(sourceChar)) > -1) { | |
result.add(values[index]); | |
total += index; | |
} | |
} | |
// Add the check digit | |
if(checkDigit) { | |
result.add(values[Math.mod(total, 43)]); | |
} | |
// Add the stop character | |
result.add(values[keys.indexOf('*')]); | |
// Join as string | |
return String.join(result,''); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Simply include the code in your page: | |
<c:barcode39 codeValue="Hello World" /> | |
Use the optional checkDigit Boolean if you want it in the bar code. | |
This is a very barebones implementation. Does not support extended characters, minimal error checking, and so on. | |
It does function as adverised, but should be considered carefully before using in production. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment