Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
524 views
in Technique[技术] by (71.8m points)

vb.net - QRCODE IMAGE in Epson ESC/POS raster

having the following problem sending a bitarray to ESC/POS compatible printers to print some images, in this case QRCodes,

this is the image of the problem:

image with wihte spaces on the lines

this is the code:

    Public Shared Function ConvertImagetoBytes(BM As Bitmap) As Byte()
    Dim Data As BitMapData = GetBitmapData(BM)
    Dim Op As New MemoryStream
    Dim bw As New BinaryWriter(Op)

    bw.Write(Chr(Keys.Escape))
    bw.Write("@"c)
    bw.Write(Chr(Keys.Escape))
    bw.Write("3"c)
    bw.Write(CByte(24))

    Dim offset As Integer = 0
    Dim width As Byte()

    While offset < Data.Height
        bw.Write(Chr(Keys.Escape))
        bw.Write("*"c)
        bw.Write(CByte(33))
        width = BitConverter.GetBytes(Data.Width)
        bw.Write(width(0))
        bw.Write(width(1))
        For x As Integer = 0 To Data.Width - 1
            For k As Integer = 0 To 2
                Dim slice As Byte = 0
                For b As Integer = 0 To 7
                    Dim y As Integer = (((offset  8) + k) * 8) + b
                    Dim i As Integer = (y * Data.Width) + x
                    Dim v As Boolean = False
                    If i < Data.Dots.Length Then
                        v = Data.Dots(i)
                    End If
                    slice = slice Or CByte((If(v, 1, 0)) << (7 - b))
                Next
                bw.Write(slice)
            Next
        Next
        offset = offset + 24
        bw.Write(vbLf.ToCharArray)
    End While

    bw.Write(Chr(Keys.Escape))
    bw.Write("2"c)
    bw.Write(CByte(30))

    bw.Flush()

    Return Op.ToArray
End Function

this converts the image in bitmapdata to be processed in the upper function

Private Shared Function GetBitmapData(BM As Bitmap) As BitMapData
    Dim threshold = 127
    Dim index As Integer = 0
    Dim dimensions As Integer = BM.Width * BM.Height
    Dim dots As BitArray = New BitArray(dimensions)
    Dim res As New BitMapData
    Dim a As Integer

    For y = 0 To BM.Height - 1
        For x = 0 To BM.Width - 1
            Dim col As Color = BM.GetPixel(x, y)
            Dim luminance = CInt(col.R * 0.3 + col.G * 0.59 + col.B * 0.11)
            If (luminance < threshold) = True Then
                a = 1
            End If
            dots(index) = (luminance < threshold)
            index = index + 1
        Next
    Next
    res.Dots = dots : res.Height = BM.Height : res.Width = BM.Width
    Return res
End Function

the structure of the bitmap data

Private Class BitMapData
    Public Dots As BitArray
    Public Height As Int16
    Public Width As Int16
End Class

thanks for your help.

i have no idea why is this happening


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...