Bar code generation and decoding¶
In this article we will go into detail on how to convert a character string into a black and white barcode (as an image) and vice versa. The code uses the Images
package to work with images.
The programme we have implemented consists of two main functions:
string_to_barcode
- Converts a string into a barcode image;barcode_to_string
- decodes the barcode image back to a string.
using Images
String_to_barcode function¶
Operating logic¶
Initialisation of empty array bits: it stores bits of all characters of the string.
Cycle through each character of the string:
bitstring(UInt8(char))
converts a character to its 8-bit representation,parse(Int, bit)
converts each bit to the corresponding integer,append!
adds these numbers to the arraybits
.
Creating a two-dimensional array for an image:
bits'
transposes a vector of bits (converts to a string),repeat
repeats this stringlength(bits)/2
times to create a two-dimensional array (image) where each row is identical,- factor
length(bits)/2
is selected to create a rectangular image.
Return result: the function returns a two-dimensional array of 0's and 1's, which can be interpreted as a black and white image.
function string_to_barcode(s::String)
bits = []
for char in s
binary_str = bitstring(UInt8(char)) # Получаем бинарное представление символа (8 бит)
append!(bits, [parse(Int, bit) for bit in binary_str]) # Преобразуем каждый бит в число и добавляем в вектор
end
repeated_bits = repeat(bits', Int.(length(bits)/2),1) # Повторяем вектор бит
return repeated_bits
end
Example of use:¶
barcode = string_to_barcode("_Engee_") # Переводим строку в битовое представление
img = Gray.(barcode) # Преобразуем в изображение
display(img) # Показываем изображение
save("barcode.png", img) # Сохраняем в файл
Function barcode_to_string¶
Operating logic¶
Binarising the image:
Gray.(img)
converts the image to grayscale;.> 0.5
converts to pure black and white (values greater than 0.5 become 1, the rest become 0).
Extracting a bit sequence:
binary_img[10, :]
takes the 10th line of the image (you can take any line, since all lines are the same).
Character decoding:
- loops through the bits in increments of 8 (each character is encoded with 8 bits),
- for every 8 bits
reduce((x, y) -> x * 2 + y, byte)
converts an array of bits into a number,Char()
converts the number to the corresponding character.
String assembly:
join(chars)
combines all characters into a single string.
function barcode_to_string(img)
binary_img = Gray.(img) .> 0.5 # Преобразуем изображение в бинарный формат (0 или 1)
bits = binary_img[10, :] # Получаем первую строку изображения (так как все строки одинаковы)
# Преобразуем биты в символы
chars = []
for i in 1:8:length(bits)
# Берем 8 бит и преобразуем их в символ
byte = bits[i:i+7]
char = Char(reduce((x, y) -> x * 2 + y, byte))
push!(chars, char)
end
# Собираем символы в строку
return join(chars)
end
Example usage:¶
img = load("barcode.png") # Загружаем изображение штрихкода
display(img) # Показываем изображение
decoded_string = barcode_to_string(img) # Декодируем строку
println("Decoded string: ", decoded_string) # Выводим результат
Conclusion¶
This code demonstrates the basic principle of converting a string to a visual representation and back again. This principle can be extended for more complex usage scenarios.
Some implementation features are also worth mentioning.
Character encoding: each character is encoded with exactly 8 bits (ASCII), which limits the character set but simplifies the implementation.
Visible representation: The barcode is created by repeating the same bit sequence in multiple lines, which makes it more visible.
Stability: a specific image line (10th line) is taken during decoding, which makes the algorithm vulnerable to image corruption. Real systems usually use more complex reconstruction logic.