将任何图像转换为ASCII文本

时间:2018-10-16 06:21:37

标签: vb.net

鉴于对“ ASCII ART”的喜爱,这对研究尤其困难和烦人。那是我在这里试图做的。我需要从旧的软件包中复制一个过程,该过程基本上将图像(任何图像)转换为ASCII文本,然后再将其发送到打印机。这是它吐出来的一个例子...

ascii image

前两行是命令。接下来的315行是纯ASCII码(我们称其为ASCII,但显然是十六进制的(80个字符行),随后在执行打印之前还有其他一些命令和设置。

我已经编写了所有软件,除了将图像转换为ascii之外。最初,根据文档,打印机应该接受几种图像类型的原始二进制数据。好了,经过一天的努力,我现在明白了为什么制造商自己不走这条路。

所以,我的问题是,如何最好地将图像转换为ascii(十六进制)字符以发送到打印机?我的第一个猜测是遍历每个字符并将其转换,但这似乎是最慢的方法。

Public Shared Sub ConvertImageHex(filepath As String)
    Dim f_convOutBytes() As Byte = File.ReadAllBytes(filepath)
    Dim totalStreamLength As Int32 = f_convOutBytes.Length
    Dim labelOutStream(totalStreamLength) As Byte
    f_convOutBytes.CopyTo(labelOutStream, 0)
    Dim HexValue As String = String.Empty

    Dim labelPath As String = IO.Path.GetTempPath & "testasci.lbl"
    If File.Exists(labelPath) Then
        File.Delete(labelPath)
    End If
    Dim fs As FileStream = New FileStream(labelPath, FileMode.OpenOrCreate, FileAccess.ReadWrite)
    Dim s As StreamWriter = New StreamWriter(fs)
    s.Close()
    fs.Close()
    fs = New FileStream(labelPath, FileMode.OpenOrCreate, FileAccess.ReadWrite)
    s = New StreamWriter(fs)

    Dim i As Int32 = 0
    For Each c In f_convOutBytes
        i += 1
        If i > 38 Then
            s.Write(vbCrLf)
            i = 0
        End If
        HexValue = Convert.ToString(Convert.ToInt32(c), 16)
        s.Write(HexValue)
    Next
    s.Close()
    fs.Close()
    If Not s Is Nothing Then s = Nothing
    If Not fs Is Nothing Then fs = Nothing
End Sub

enter image description here

文档中的行情(此时杂草可能过多):

d = download data
type = the type of data that will follow, using standard file name extensions
ASC - Graphic in ASCII format

ASCII graphics
ASCII-Graphic format
The stucture is similar to the IMG format, but uses only ASCII characters, to enable a easy usage for
host devices or ERP systems.
Following rules are used:
• all data are hex bytes, i.e. 0-9 and a-f or A-F
• The printer waits for data until the defined picture size is received.
• Spaces and carriage returns can be added on different locations. It is required that a
carriage return is sent at the end of the picture data.
• The image data can be compressed with a simple algorithm which is black/white
optimized.
• The image data are transmitted from top to bottom, each time from left to right. A value
byte 80 stands left of 01.
• The first line describes the width and the height of a picture. Width and height are 16 bit
values each in the Big-Endian format.
• Also if the width is not devidable by 8, it is required that the missing pixel must be
transmitted.
Each line will be transmitted with following values:
• Optional repetition factor, caused by 00 00 FF xx, whereby xx describes the amount of
copies of the current line.
• Picture data - whereby different descriptions are optional possible:
a: Zerobytes are displayed through the amount of bytes.Valid input: 00 to FF.
b: Blackbytes (FF) can also be described through the amount of bytes, beginning
from 81 (81 means 1 time FF, - valid values are 81 to FF ).
c: A directly encoded number of bytes starts with 80 - followed by the amount of data, i.e.
80 03 123456. The amout of transmitted bytes can be between 01 and 7F.
d: A repeated pattern of arbitrary bytes can be initiated with a sequence 00 nn xx, which
means that xx bytes will be inserted nn times.
Example: 00 04 AA generates AAAAAAAA.

0 个答案:

没有答案