ok! I try to Log its Width and Height.Check the size of the returned bitmap. I'm pretty sure that it will not include the empty space. This will allow you to to change the ImageView dimensions as needed.
Check the size of the returned bitmap. I'm pretty sure that it will not include the empty space. This will allow you to to change the ImageView dimensions as needed.
Sub scanner_Detected (Codes As List)
Dim sb As StringBuilder
Dim codice As String
sb.Initialize
sb.Append("Detected code:").Append(CRLF)
For Each code As BarcodeCode In Codes
sb.Append(code.Value).Append(CRLF)
codice = code.Value
Next
Log(sb.ToString)
If SelectedImageView.IsInitialized Then
'Log($"SelectedImageView -> Width: ${SelectedImageView.Width}, Height: ${SelectedImageView.Height}"$)
'SelectedImageView.Bitmap = CreateCode39(codice, SelectedImageView.Width, SelectedImageView.Height)
Log($"ImageView1 -> Width: ${ImageView1.Width}, Height: ${ImageView1.Height}"$)
ImageView1.Bitmap = CreateCode39(codice, ImageView1.Width, ImageView1.Height)
End If
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))
Log($"Returned BMP -> Width: ${bmp.Width}, Height: ${bmp.Height}"$) '<--'
Return bmp
End Sub
Upload a small project with this code.
ImageView1.Width = 100%x - 20dip
ImageView1.Left = 10dip
ImageView1.SetLeftAndRight(10dip, 100%x-10dip)
Panel1.Top = 10dip
Panel2.Height = 4dip
Why is this wrong?ImageView1.Width = 100%x - 20dip ImageView1.Left = 10dip
Actually it does you didn't noticed because the view was positioned very close to the final positionImageView1.SetLeftAndRight(10dip, 100%x-10dip)
This is necessary, this is the Red Guide Line for the scanner (if i remove this code, you can see how the red line looks).Panel1.Top = 10dip Panel2.Height = 4dip
It is not needed. Use anchors for this.I always used this to center a view with 10dip space from the edges
Private Sub CreateCode39 (Text As String, Width As Float, Height As Float) As Bitmap
Dim no As NativeObject = Me
Dim res As List = no.RunMethod("code39ImageFromString:::", Array(Text, Width, Height))
Dim bmp As B4XBitmap = res.Get(0)
Dim Width As Float = res.Get(1)
Log($"Returned BMP -> Width: ${bmp.Width}, Height: ${bmp.Height}"$)
bmp = bmp.Crop(0, 0, Width + 10, bmp.Height)
Return bmp
End Sub
#if OBJC
- (NSArray *)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, 0.0, 1.0, 1.0);
CGContextSetRGBFillColor(context, 1.0, 0.0, 0.0, 0.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, @(x)];
}
#End If
Thanks erel!! now it is centered!It is not needed. Use anchors for this.
Updated barcode code that crops the image:
B4X:Private Sub CreateCode39 (Text As String, Width As Float, Height As Float) As Bitmap Dim no As NativeObject = Me Dim res As List = no.RunMethod("code39ImageFromString:::", Array(Text, Width, Height)) Dim bmp As B4XBitmap = res.Get(0) Dim Width As Float = res.Get(1) Log($"Returned BMP -> Width: ${bmp.Width}, Height: ${bmp.Height}"$) bmp = bmp.Crop(0, 0, Width + 10, bmp.Height) Return bmp End Sub #if OBJC - (NSArray *)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, 0.0, 1.0, 1.0); CGContextSetRGBFillColor(context, 1.0, 0.0, 0.0, 0.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, @(x)]; } #End If
It is not needed. Use anchors for this.
Updated barcode code that crops the image:
B4X:Private Sub CreateCode39 (Text As String, Width As Float, Height As Float) As Bitmap Dim no As NativeObject = Me Dim res As List = no.RunMethod("code39ImageFromString:::", Array(Text, Width, Height)) Dim bmp As B4XBitmap = res.Get(0) Dim Width As Float = res.Get(1) Log($"Returned BMP -> Width: ${bmp.Width}, Height: ${bmp.Height}"$) bmp = bmp.Crop(0, 0, Width + 10, bmp.Height) Return bmp End Sub #if OBJC - (NSArray *)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, 0.0, 1.0, 1.0); CGContextSetRGBFillColor(context, 1.0, 0.0, 0.0, 0.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, @(x)]; } #End If
Sub scanner_Detected (Codes As List)
Dim sb As StringBuilder
sb.Initialize
For Each code As BarcodeCode In Codes
sb.Append(code.Value).Append(CRLF)
Next
Log(sb.ToString)
If SelectedImageView.IsInitialized Then
ImageView1.Bitmap = CreateCode39(sb.ToString, ImageView1.Width, ImageView1.Height) '<---- If i put "Ciao" instead it works, but if the string comes from the reader it doesnt... even if in the log the string looks the same
End If
scanner.Stop
End Sub
@Erel There is some sort of error: If i pass a string in the function Create39Barcode like so:
B4X:Sub scanner_Detected (Codes As List) Dim sb As StringBuilder sb.Initialize For Each code As BarcodeCode In Codes sb.Append(code.Value).Append(CRLF) Next Log(sb.ToString) If SelectedImageView.IsInitialized Then ImageView1.Bitmap = CreateCode39(sb.ToString, ImageView1.Width, ImageView1.Height) '<---- If i put "Ciao" instead it works, but if the string comes from the reader it doesnt... even if in the log the string looks the same End If scanner.Stop End Sub
it crashes with:
Application_Start
ImageView1 -> Width: 390.1407, Height: 95.437
ImageView1 -> Width: 390.1407, Height: 95.437
Returned BMP -> Width: 391, Height: 96
Application_Active
Setting torch mode: 0
Ciao
This string contains illegal characters
Error occurred on line: 118 (Main)
Object was not initialized (NSObject)
Stack Trace: (
CoreFoundation 9624AAFD-5437-3772-A507-0F357875808D + 1253752
libobjc.A.dylib objc_exception_throw + 60
CoreFoundation 9624AAFD-5437-3772-A507-0F357875808D + 138168
Barcode Scanner -[B4IObjectWrapper object] + 136
CoreFoundation 9624AAFD-5437-3772-A507-0F357875808D + 1281300
CoreFoundation 9624AAFD-5437-3772-A507-0F357875808D + 7656
Barcode Scanner +[B4I runDynamicMethod:method:throwErrorIfMissing:args:] + 1608
Barcode Scanner -[B4IShell runMethod:] + 448
Barcode Scanner -[B4IShell raiseEventImpl:method:args::] + 1648
Barcode Scanner -[B4IShellBI raiseEvent:eventarams:] + 1580
Barcode Scanner +[B4IObjectWrapper raiseEvent:::] + 300
Barcode Scanner __17-[iBarcode Start]_block_invoke + 200
Barcode Scanner -[MTBBarcodeScanner captureOutput:didOutputMetadataObjects:fromConnection:] + 428
AVFoundation E984C99B-C3F2-314C-956F-A65B67DCD4CD + 1446900
AVFoundation E984C99B-C3F2-314C-956F-A65B67DCD4CD + 1443760
CoreMedia FE577D32-0419-3D14-B24C-A9650A420869 + 350556
CoreMedia FE577D32-0419-3D14-B24C-A9650A420869 + 486256
libdispatch.dylib 783B1B75-5A87-3A6E-B723-5CAA2663C75F + 13116
libdispatch.dylib 783B1B75-5A87-3A6E-B723-5CAA2663C75F + 23288
libdispatch.dylib 783B1B75-5A87-3A6E-B723-5CAA2663C75F + 91684
libdispatch.dylib _dispatch_main_queue_callback_4CF + 560
CoreFoundation 9624AAFD-5437-3772-A507-0F357875808D + 709660
CoreFoundation 9624AAFD-5437-3772-A507-0F357875808D + 688180
CoreFoundation CFRunLoopRunSpecific + 480
GraphicsServices GSEventRunModal + 164
UIKitCore UIApplicationMain + 1944
Barcode Scanner main + 124
libdyld.dylib 95B366E7-F5BD-3308-9416-24B35999029B + 4588
)