Convert VB.net to Gambas
Posted: Friday 16th July 2021 9:38pm
hi Everyone,
I have managed to convert some code from Java to VB.net but I was wondering if someone could look over it and see what I need to change to covert it to Gambas (so it would work)
This is the code that would send the image to the Printer
I Know i am asking a lot but I am still not 100% up on what I have to do to convert from VB to Gambas
Below is the Java Code (I managed to use a online service that converted it from Java to VB.net)
I have managed to convert some code from Java to VB.net but I was wondering if someone could look over it and see what I need to change to covert it to Gambas (so it would work)
This is the code that would send the image to the Printer
Code: Select all
Dim bitmapString As String = New String(bitmapBytes, Charsets.US_ASCII)
bitmapString = bitmapString.substring(DataOffset, bitmapStringSize)
Dim commandString As String = ""
Dim commandLength As Integer = (bitmapStringSize.length + 11)
pL = (commandLength Mod 256)
If (commandLength < 256) Then
pH = 0
Else
pH = ((commandLength - pL) _
/ 256)
End If
xL = (bitmapWidth Mod 256)
If (bitmapWidth < 256) Then
xH = 0
Else
xH = ((bitmapWidth _
- (bitmapWidth Mod 256)) _
/ 256)
End If
yL = (bitmapHeight Mod 256)
If (bitmapHeight < 256) Then
yH = 0
Else
yH = ((bitmapHeight _
- (bitmapHeight Mod 256)) _
/ 256)
End If
commandString = (commandString + Utils.H("1B"))' 27
Utils.H("40")' 64
Utils.H("1B")' 27
Utils.H("3D")' 61
Utils.H("01")' 1
Utils.H("1D")' GS = 29
Utils.H("28")' ( = 40
Utils.H("4C")' L = 76
Utils.D(pL)' pL
Utils.D(pH)' pH
Utils.H("30")' m = 48
Utils.H("43")' fn = 67
Utils.H("30")' a = 48
Utils.H(KC1)' kc1
Utils.H(KC2)' kc2
Utils.H("01")' b = 1
Utils.D(xL)' xL
Utils.D(xH)' xH
Utils.D(yL)' yL
Utils.D(yH)' yH
Utils.H("31")' c = 49
commandString = (commandString + bitmapString)
Code: Select all
Public Shared Function decodeBitmap(ByVal bitmapBytes() As Byte) As Byte()
Dim bmp As Bitmap = BitmapFactory.decodeByteArray(bitmapBytes, 0, bitmapBytes.length)
Dim zeroCount As Integer = (bmp.getWidth Mod 8)
Dim zeroStr As String = ""
If (zeroCount > 0) Then
Dim i As Integer = 0
Do While (i < (8 - zeroCount))
zeroStr = (zeroStr + "0")
i = (i + 1)
Loop
End If
Dim list As List(Of String) = New ArrayList
Dim i As Integer = 0
Do While (i < bmp.getHeight)
Dim sb As StringBuilder = New StringBuilder
Dim j As Integer = 0
Do While (j < bmp.getWidth)
Dim color As Integer = bmp.getPixel(j, i)
Dim r As Integer = ((color + 16) _
And 255)
Dim g As Integer = ((color + 8) _
And 255)
Dim b As Integer = (color And 255)
' if color close to whitebit='0', else bit='1'
If ((r > 160) _
AndAlso ((g > 160) _
AndAlso (b > 160))) Then
sb.append("0")
Else
sb.append("1")
End If
j = (j + 1)
Loop
If (zeroCount > 0) Then
sb.append(zeroStr)
End If
list.add(sb.toString)
i = (i + 1)
Loop
Dim bmpHexList As List(Of String) = binaryListToHexStringList(list)
Dim commandList As List(Of String) = New ArrayList
commandList.addAll(bmpHexList)
Return hexListToBytes(commandList)
End Function
Below is the Java Code (I managed to use a online service that converted it from Java to VB.net)
Code: Select all
public static byte[] decodeBitmap(byte[] bitmapBytes) {
Bitmap bmp = BitmapFactory.decodeByteArray(bitmapBytes, 0, bitmapBytes.length);
int zeroCount = bmp.getWidth() % 8;
String zeroStr = "";
if (zeroCount > 0) {
for (int i = 0; i < (8 - zeroCount); i++) {
zeroStr = zeroStr + "0";
}
}
List<String> list = new ArrayList<>();
for (int i = 0; i < bmp.getHeight(); i++) {
StringBuilder sb = new StringBuilder();
for (int j = 0; j < bmp.getWidth(); j++) {
int color = bmp.getPixel(j, i);
int r = (color >> 16) & 0xff;
int g = (color >> 8) & 0xff;
int b = color & 0xff;
// if color close to white,bit='0', else bit='1'
if (r > 160 && g > 160 && b > 160)
sb.append("0");
else
sb.append("1");
}
if (zeroCount > 0) {
sb.append(zeroStr);
}
list.add(sb.toString());
}
List<String> bmpHexList = binaryListToHexStringList(list);
List<String> commandList = new ArrayList<>();
commandList.addAll(bmpHexList);
return hexListToBytes(commandList);
}