×

Loading...
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。

C高手请进:下面这段代码是向打印机直接发送控制码和数据打印出图形

本文发表在 rolia.net 枫下论坛其中send surface那一段没看明白,那位高手指点一下?谢谢!

void PrintSurface (int hDevice, SDL_Surface *surface, char justify)
{
Uint8 byte;
int bit;
SDL_Color pixel;
int i, pc;
int r, c;

**==================================================
** Start bit image printing.
**==================================================
*/
write (hDevice, "\x1B\x2A\x62", 3);

byte = surface->h % 256;
write (hDevice, &byte, sizeof (byte));
byte = surface->h / 256;
write (hDevice, &byte, sizeof (byte));

for (r = 0; r < surface->h; r++)
{
byte = 0x00;
bit = 7;
pc = 0;


/*
**==================================================
** Justify line.
**==================================================
*/
if (surface->w < DotsPerLine[Model])
{
switch (toupper (justify))
{
case 'C':
pc = (DotsPerLine[Model] - surface->w) / 2;
break;

case 'R':
pc = DotsPerLine[Model] - surface->w;
break;

default:
pc = 0;
break;
}

byte = 0x00;
for (i = 0; i < (pc / 8); i++)
{
write (hDevice, &byte, sizeof (byte));
}

bit = 7 - (pc % 8);
}


/*
**==================================================
** Send surface.
**==================================================
*/
for (c = 0; (pc < DotsPerLine[Model]) && (c < surface->w); c++, pc++)
{
pixel = GetPixel (surface, c, r);
if ((pixel.r < 128) && (pixel.g < 128) && (pixel.b < 128))
{
byte |= (0x01 << bit);
}
bit--;
if (bit < 0)
{
write (hDevice, &byte, sizeof (byte));
bit = 7;
byte = 0x00;
}
}


/*
**==================================================
** Finish out line.
**==================================================
*/
if (bit != 7)
{
write (hDevice, &byte, sizeof (byte));
pc += bit + 1;
}

byte = 0x00;
for (bit = (DotsPerLine[Model] - pc) / 8; bit > 0; bit--)
{
write (hDevice, &byte, sizeof (byte));
}
}
}更多精彩文章及讨论,请光临枫下论坛 rolia.net
Report

Replies, comments and Discussions:

  • 工作学习 / IT技术讨论 / C高手请进:下面这段代码是向打印机直接发送控制码和数据打印出图形
    本文发表在 rolia.net 枫下论坛其中send surface那一段没看明白,那位高手指点一下?谢谢!

    void PrintSurface (int hDevice, SDL_Surface *surface, char justify)
    {
    Uint8 byte;
    int bit;
    SDL_Color pixel;
    int i, pc;
    int r, c;

    **==================================================
    ** Start bit image printing.
    **==================================================
    */
    write (hDevice, "\x1B\x2A\x62", 3);

    byte = surface->h % 256;
    write (hDevice, &byte, sizeof (byte));
    byte = surface->h / 256;
    write (hDevice, &byte, sizeof (byte));

    for (r = 0; r < surface->h; r++)
    {
    byte = 0x00;
    bit = 7;
    pc = 0;


    /*
    **==================================================
    ** Justify line.
    **==================================================
    */
    if (surface->w < DotsPerLine[Model])
    {
    switch (toupper (justify))
    {
    case 'C':
    pc = (DotsPerLine[Model] - surface->w) / 2;
    break;

    case 'R':
    pc = DotsPerLine[Model] - surface->w;
    break;

    default:
    pc = 0;
    break;
    }

    byte = 0x00;
    for (i = 0; i < (pc / 8); i++)
    {
    write (hDevice, &byte, sizeof (byte));
    }

    bit = 7 - (pc % 8);
    }


    /*
    **==================================================
    ** Send surface.
    **==================================================
    */
    for (c = 0; (pc < DotsPerLine[Model]) && (c < surface->w); c++, pc++)
    {
    pixel = GetPixel (surface, c, r);
    if ((pixel.r < 128) && (pixel.g < 128) && (pixel.b < 128))
    {
    byte |= (0x01 << bit);
    }
    bit--;
    if (bit < 0)
    {
    write (hDevice, &byte, sizeof (byte));
    bit = 7;
    byte = 0x00;
    }
    }


    /*
    **==================================================
    ** Finish out line.
    **==================================================
    */
    if (bit != 7)
    {
    write (hDevice, &byte, sizeof (byte));
    pc += bit + 1;
    }

    byte = 0x00;
    for (bit = (DotsPerLine[Model] - pc) / 8; bit > 0; bit--)
    {
    write (hDevice, &byte, sizeof (byte));
    }
    }
    }更多精彩文章及讨论,请光临枫下论坛 rolia.net
    • loop throug all the Dots in this Line of this Surface. It does some freaking things to this dot's RGB, and every 8 dots, it write this Bytes to the printer. IMHO
    • dithering (convert rgb image to b/w ) + send image pixels ( one bit per pixel, 8 pixels one byte)
      • 谢谢ange0791(human)和canadiantire(八卦轮胎); 这个小程序是在linux下运行的,要想改写在windows下C实现,需要哪几方面改动?
        • call windows GDI.