iOS Question Generate Barcode 39

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Sub Process_Globals
    Public App As Application
    Public NavControl As NavigationController
    Private Page1 As Page
    Private ImageView1 As ImageView
End Sub

Private Sub Application_Start (Nav As NavigationController)
    NavControl = Nav
    Page1.Initialize("Page1")
    Page1.Title = "Page 1"
    Page1.RootPanel.LoadLayout("1")
    NavControl.ShowPage(Page1)
    ImageView1.Bitmap = CreateCode39("1232323", ImageView1.Width, ImageView1.Height)
End Sub

Private Sub CreateCode39 (Text As String, Width As Float, Height As Float) As Bitmap
    Dim no As NativeObject = Me
    Dim bmp As Bitmap = no.RunMethod("code39ImageFromString:::", Array(Text, Width, Height))
    Return bmp
End Sub

Private Sub Page1_Resize(Width As Int, Height As Int)
    
End Sub

Private Sub Application_Background
    
End Sub

#if OBJC
- (UIImage *)code39ImageFromString:(NSString *)strSource    // Source string
                             :(CGFloat)barcodew        // Barcode Width
                            :(CGFloat)barcodeh        // Barcode Height
{
    int intSourceLength = (int)strSource.length;
    CGFloat x = 1; // Left Margin
    CGFloat y = 0; // Top Margin
    // Width = ((WidLength * 3 + NarrowLength * 7) * (intSourceLength + 2)) + (x * 2)
    CGFloat NarrowLength = (barcodew/(intSourceLength + 2)) / 17.0; // Length of narrow bar
    CGFloat WidLength = NarrowLength * 2; // Length of Wide bar
    NSString *strEncode = @"010010100"; // Encoding string for starting and ending mark *
    NSString * AlphaBet = @"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%*"; // Code39 alphabets
    NSString* Code39[] = //Encoding strings for Code39 alphabets
    {
        /* 0 */ @"000110100",
        /* 1 */ @"100100001",
        /* 2 */ @"001100001",
        /* 3 */ @"101100000",
        /* 4 */ @"000110001",
        /* 5 */ @"100110000",
        /* 6 */ @"001110000",
        /* 7 */ @"000100101",
        /* 8 */ @"100100100",
        /* 9 */ @"001100100",
        /* A */ @"100001001",
        /* B */ @"001001001",
        /* C */ @"101001000",
        /* D */ @"000011001",
        /* E */ @"100011000",
        /* F */ @"001011000",
        /* G */ @"000001101",
        /* H */ @"100001100",
        /* I */ @"001001100",
        /* J */ @"000011100",
        /* K */ @"100000011",
        /* L */ @"001000011",
        /* M */ @"101000010",
        /* N */ @"000010011",
        /* O */ @"100010010",
        /* P */ @"001010010",
        /* Q */ @"000000111",
        /* R */ @"100000110",
        /* S */ @"001000110",
        /* T */ @"000010110",
        /* U */ @"110000001",
        /* V */ @"011000001",
        /* W */ @"111000000",
        /* X */ @"010010001",
        /* Y */ @"110010000",
        /* Z */ @"011010000",
        /* - */ @"010000101",
        /* . */ @"110000100",
        /*' '*/ @"011000100",
        /* $ */ @"010101000",
        /* / */ @"010100010",
        /* + */ @"010001010",
        /* % */ @"000101010",
        /* * */ @"010010100"
    };
    
    strSource = [strSource uppercaseString];
    // calculate graphic size
    CGSize size = CGSizeMake(barcodew, barcodeh + (y * 2));
    UIGraphicsBeginImageContext(size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    // fill background color (white)
    CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);
    CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0);
    CGContextFillRect(context, CGRectMake(0.0, 0.0, size.width, size.height));
    
    // beging encoding
    for (int i = 0; i < intSourceLength; i++)
    {
        // check for illegal characters
        char c = [strSource characterAtIndex:i];
        long index = [AlphaBet rangeOfString:[NSString stringWithFormat:@"%c",c]].location;
        if ((index == NSNotFound) || (c == '*'))
        {
            NSLog(@"This string contains illegal characters");
            return nil;
        }
        // get and concat encoding string
        strEncode = [NSString stringWithFormat:@"%@0%@",strEncode, Code39[index]];
    }
    // pad with ending *
    strEncode = [NSString stringWithFormat:@"%@0010010100", strEncode];
    
    int intEncodeLength = (int)strEncode.length; // final encoded data length
    CGFloat fBarWidth;
    // Draw Code39 BarCode according the the encoded data
    for (int i = 0; i < intEncodeLength; i++)
    {
        fBarWidth = ([strEncode characterAtIndex:i] == '1' ? WidLength : NarrowLength);
        // drawing with black color
        if (i % 2 == 0) {
            CGContextSetRGBStrokeColor(context, 0.0, 0.0, 0.0, 1.0);
            CGContextSetRGBFillColor(context, 0.0, 0.0, 0.0, 1.0);
        }
        // drawing with white color
        else {
            CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);
            CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0);
        }
        CGContextFillRect(context, CGRectMake(x, y, fBarWidth, barcodeh));
        x += fBarWidth;
    }
    // get image from context and return
    UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return resultingImage;
}

#End If

Based on: https://github.com/bclin087/Simple-Code39-generator-for-iOS

License: MIT
 
Upvote 0

Jan Vrielink

Member
Licensed User
Here is b4a-code for generating code39-barcode.

PaintBarCode(Canvas1, 5dip, 1dip, 100dip, "ABCDEF", 2)



B4X:
Public Sub PaintBarCode( xCanvas As Canvas, xLeft As Int,  xTop As Int, xHeight As Int, xCode As String, xThicknes As Float)
Dim intercharacterGap As String = "0"
Dim str As String
Dim alphabet39 As String = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%*"
Dim coded39Char() As String = Array As String("000110100", "100100001", "001100001", "101100000", "000110001", "100110000", _
  "001110000", "000100101", "100100100", "001100100", "100001001", "001001001", _
  "101001000", "000011001", "100011000", "001011000", "000001101", "100001100", _
  "001001100", "000011100", "100000011", "001000011", "101000010", "000010011", _
  "100010010", "001010010", "000000111", "100000110", "001000110", "000010110", _
  "110000001", "011000001", "111000000", "010010001", "110010000", "011010000", _
  "010000101", "110000100", "011000100", "010101000", "010100010", "010001010", _
  "000101010", "010010100")

  If xCode = "" Then
        Return
    End If
  str = "*" & xCode.ToUpperCase & "*"

  For i = 0 To xCode.Length - 1
        If alphabet39.IndexOf( xCode.CharAt(i)) = -1 Or xCode.CharAt(i) = "*" Then
        Return
    End If
  Next

  Dim encodedString As String = ""

    For i  = 0 To str.Length - 1
    If i > 0 Then
            encodedString = encodedString & intercharacterGap
    End If
   encodedString = encodedString & coded39Char(alphabet39.IndexOf(str.CharAt(i)))
  Next

  Dim encodedStringLength As Int = encodedString.Length
    Dim wideToNarrowRatio As Float = 3

    Dim x As Float = 0
    Dim wid As Float = 0

  x = xLeft
  For i = 0 To encodedStringLength - 1
    If encodedString.CharAt(i) = "1" Then
            wid = wideToNarrowRatio * xThicknes
    Else
            wid = xThicknes
    End If

        Dim d2 As Rect
        d2.Initialize(  x, xTop,  x+ wid,  xHeight)
        xCanvas.DrawRect(d2, IIf(i Mod 2 = 0, Colors.Black, Colors.WHITE), True, wid)
    x = x + wid
  Next


 End Sub
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…