【问题标题】:Printing Image to Zebra iMZ320将图像打印到 Zebra iMZ320
【发布时间】:2016-05-18 20:12:32
【问题描述】:

平台:Windows Mobile 6.5 Handheld

语言:C#

我的问题:我被要求从用户那里获取签名,然后将该签名发送到打印机以打印在收据上。我已经成功捕获了签名的图像并将签名的字节数组保存在内存中,但似乎无法正确打印它。

为了让我开始,我按照blog here 获取位图的十六进制表示。但是,这只是打印了一张很长的带有十六进制的收据 签名的表示。代码在这里而不是跟随链接:

    private static string DrawBitmap(Bitmap bmp, int xPosition, int yPosition)
    {
        if (bmp == null)
            throw new ArgumentNullException("bmp");

        StringBuilder DataString = new StringBuilder();
        //Make sure the width is divisible by 8
        int loopWidth = 8 - (bmp.Width % 8);
        if (loopWidth == 8)
            loopWidth = bmp.Width;
        else
            loopWidth += bmp.Width;

        //DataString.Append(string.Format("EG {0} {1} {2} {3} ", xPosition, yPosition));
        DataString.Append(string.Format("EG 64 128 {0} {1} ", xPosition, yPosition));

        for (int y = 0; y < bmp.Height; y++)
        {
            int bit = 128;
            int currentValue = 0;
            for (int x = 0; x < loopWidth; x++)
            {
                int intensity;

                if (x < bmp.Width)
                {
                    Color color = bmp.GetPixel(x, y);
                    intensity = 255 - ((color.R + color.G + color.B) / 3);
                }
                else
                    intensity = 0;

                if (intensity >= 128)
                    currentValue |= bit;
                bit = bit >> 1;
                if (bit == 0)
                {
                    DataString.Append(currentValue.ToString("X2"));
                    bit = 128;
                    currentValue = 0;
                }
            }//x
        }//y
        DataString.Append("\r\n");

        return DataString.ToString();
    }

失败后,我找到了 Zebra 打印机的CPCL programming guide,并按照第 95 页的示例打印了小瓷砖图像。但是,这与签名的作用相同。一旦失败,我发现我需要在执行任何 EG 命令之前运行命令:! U1 setvar "device.languages" "zpl";所以我继续这样做,但这里的情况出现了糟糕的转折,最终迫使我完全重置打印机和/或干净启动手持设备,因为它导致 COM 异常导致 COM6 和打印机崩溃。

我已经用尽了我能想到的大部分资源,但没有一个有效。

有没有人有任何其他想法或示例可以帮助我完成这项工作?

谢谢

【问题讨论】:

  • 这项工作有效吗:mrpmorris.blogspot.fr/2007/08/printing-bitmaps-using-cpcl.html。大多数情况下,当打印机打印代码而不是文本或图像时,这是由于被设置为备用打印机语言。而且,通常传输是一个问题,大多数打印机只理解 ASCII-7。因此,DataString 在发送到打印机之前可能会被转换为 7Bit 字节数组。

标签: c# bitmap zebra-printers windows-mobile-6.5


【解决方案1】:

我找到了另一个 CPCL 程序员指南,它有这个简单的(测试)示例:

! 0 200 200 210 1
EG 2 16 90 45 F0F0F0F0F0F0F0F00F0F0F0F0F0F0F0F
F0F0F0F0F0F0F0F00F0F0F0F0F0F0F0F
FORM
PRINT

这应该打印一个小的棋盘图案。

下一个示例打印一个 PCX 图形:

PCX Commands
The PCX command gives a user the ability to send “.PCX” graphics formatted images to the printer. The
.PCX image MUST be encoded as a black and white image.
Format:
{command} {x} {y}
{data}
where:
{command}: PCX
{x}: X-coordinate of the top-left corner.
{y}: Y-coordinate of the top-left corner.
{data}: PCX image data.

例子:

! 0 200 200 500 1
PCX 0 30
<binary black and white pcx data stream>
FORM
PRINT

使用文件的示例(之前加载的打印机文件系统)

! 0 200 200 500 1
PCX 0 30 !<IMAGE.PCX
FORM
PRINT

如果打印机已经切换到行式打印机模式,命令

! U1 PCX {x coordinate} {y coordinate} !< {filename.pcx}

例如

! U1 PCX 0 30 !< IMAGE.PCX

可用于从文件系统打印单色 PCX。

请记住,.NET 是 UTF-8,因此在通过 COM 端口发送之前,所有命令和数据都必须转换为 ASCII。所以做这样的事情:

        Encoding ansi = Encoding.GetEncoding(1252);
        byte[] buf = ansi.GetBytes(DataString);
        System.IO.Ports.SerialPort sp = new System.IO.Ports.SerialPort("COM1:");
        sp.Write(buf, 0, buf.Length);

对于 PCX 数据,只需将字节流用于 byte[] 缓冲区。

【讨论】:

  • 我能够成功打印棋盘格和我尝试过的其他一些图像。我正在与我正在从事的项目的人交谈,他提出我需要支持 Intermec打印机以及 Zebra 打印机。我可以同时使用这两种方法,还是必须根据打印机有条件?
  • 虽然一些 Intermec 打印机支持 ZSim(Zebra CPCL 模拟),但有些不支持。所以最好有基于接口类的单独实现。 PS:为什么它现在对你有用?诀窍是什么?
  • 好吧,我已经使用 Zebra Utilities 让它工作了,而不是通过我目前正在编写的应用程序。我相信我在应用程序中的问题是我试图在打印缓冲区中间从行打印模式更改为 zpl 模式。所以,我相信我要么完全切换到 zpl 打印模式,要么发送多个打印作业。我的问题的另一大部分是我需要压缩传递的位,并且我的压缩算法无法与我传递给 EG 命令的参数一起正常工作。感谢您在这方面的帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-28
  • 1970-01-01
相关资源
最近更新 更多