Search code examples
performanceprintingxamarin.androidthermal-printer

Ways to improve ESC/POS Thermal_Printer image printing speed?


I have been doing printing job with Thermal Printer Image Printing on portable thermal printer for weeks and this is code I got for Image Printing.

        public static byte[] GetByteImage(Bitmap bm, int BitmapWidth)
        {
            BitmapData data = GetGreyScaledBitmapData(bm, BitmapWidth);
            BitArray dots = data.Dots;
            string t = data.Width.ToString();
            byte[] width = BitConverter.GetBytes(data.Width);

            int offset = 0;
            MemoryStream stream = new MemoryStream();
            BinaryWriter bw = new BinaryWriter(stream);

            //Line spacing
            bw.Write((char)0x1B);
            bw.Write('3');
            bw.Write((byte)0);


            while (offset < data.Height)
            {
                //Declare printer to print image mode
                bw.Write((char)0x1B);
                bw.Write('*');
                bw.Write((byte)33);
                bw.Write(width[0]);
                bw.Write(width[1]);

                for (int x = 0; x < data.Width; ++x)
                {
                    for (int k = 0; k < 3; ++k)
                    {
                        byte slice = 0;
                        for (int b = 0; b < 8; ++b)
                        {
                            int y = (((offset / 8) + k) * 8) + b;
                            int i = (y * data.Width) + x;

                            bool v = false;
                            if (i < dots.Length)
                            {
                                v = dots[i];
                            }
                            slice |= (byte)((v ? 1 : 0) << (7 - b));
                        }

                        bw.Write(slice);
                    }
                }
                offset += 24;
                bw.Write((char)0x0A);
            }
            bw.Write((char)0x1B);
            bw.Write('3');
            bw.Write((byte)0);

            bw.Flush();
            byte[] bytes = stream.ToArray();
            return bytes;
        }

        public static BitmapData GetGreyScaledBitmapData(Bitmap bmpFileName, double imgsize)
        {
            using (var bitmap = (Bitmap)(bmpFileName))
            {
                var threshold = 127;
                var index = 0;
                double multiplier = imgsize;
                double scale = (double)(multiplier / (double)bitmap.Width);
                int xheight = (int)(bitmap.Height * scale);
                int xwidth = (int)(bitmap.Width * scale);
                var dimensions = xwidth * xheight;
                var dots = new BitArray(dimensions);

                for (var y = 0; y < xheight; y++)
                {
                    for (var x = 0; x < xwidth; x++)
                    {
                        var _x = (int)(x / scale);
                        var _y = (int)(y / scale);
                        Android.Graphics.Color color = new Android.Graphics.Color(bitmap.GetPixel(_x, _y));
                        var luminance = (int)(color.R * 0.3 + color.G * 0.59 + color.B * 0.11);
                        dots[index] = (luminance < threshold);
                        index++;
                    }
                }
                return new BitmapData()
                {
                    Dots = dots,
                    Height = (int)(bitmap.Height * scale),
                    Width = (int)(bitmap.Width * scale)
                };
            }
        }

        public class BitmapData
        {
            public BitArray Dots
            {
                get;
                set;
            }

            public int Height
            {
                get;
                set;
            }

            public int Width
            {
                get;
                set;
            }
        }

The problem is, it print very slow and make jerking sound while printing. Another problem is, the method of image converting to Grey Scale is a bit slow.

And when I test with other apps I found that they have no jerking sound and almost instantly print image after clicked print button.

Is there a way to improve above code so it can print smoothly ?

This is the app I tested Printer Lab - Thermal printer manager

The Thermal Printer I used RPP300 72mm Mobile Printer


Solution

  • Finally got a solution. I was really dumb back then. Just ask your printer manufacturer company for SDK or find SDK from other printer manufacturer.