Esempio n. 1
0
        public static List <byte[]> DecodeBitmapToDataList(Bitmap image, int parting)
        {
            if (parting <= 0 || parting > 255)
            {
                parting = 255;
            }

            if (image == null)
            {
                return(null);
            }

            int width  = image.Width;
            int height = image.Height;

            if (width <= 0 || height <= 0)
            {
                return(null);
            }

            if (width > 2040)
            {
                // 8位9针,宽度限制2040像素(但一般纸张都没法打印那么宽,但并不影响打印)
                float  scale  = 2040 / (float)width;
                Matrix matrix = new Matrix();
                matrix.PostScale(scale, scale);
                Bitmap resizeImage;
                try
                {
                    resizeImage = Bitmap.CreateBitmap(image, 0, 0, width, height, matrix, true);

                    var datas = DecodeBitmapToDataList(resizeImage, parting);

                    resizeImage.Recycle();

                    return(datas);
                }
                catch (OutOfMemoryError)
                {
                    return(null);
                }
            }

            // 宽命令
            string widthHexString = Integer.ToHexString(width % 8 == 0 ? width / 8 : (width / 8 + 1));

            if (widthHexString.Length > 2)
            {
                // 超过2040像素才会到达这里
                return(null);
            }
            else if (widthHexString.Length == 1)
            {
                widthHexString = "0" + widthHexString;
            }
            widthHexString += "00";

            // 每行字节数(除以8,不足补0)
            string zeroStr   = "";
            int    zeroCount = width % 8;

            if (zeroCount > 0)
            {
                for (int i = 0; i < (8 - zeroCount); i++)
                {
                    zeroStr += "0";
                }
            }
            List <string> commandList = new List <string>();
            // 高度每parting像素进行一次分割
            int time = height % parting == 0 ? height / parting : (height / parting + 1);// 循环打印次数

            for (int t = 0; t < time; t++)
            {
                int partHeight = t == time - 1 ? height % parting : parting;// 分段高度

                // 高命令
                string heightHexString = Integer.ToHexString(partHeight);
                if (heightHexString.Length > 2)
                {
                    // 超过255像素才会到达这里
                    return(null);
                }
                else if (heightHexString.Length == 1)
                {
                    heightHexString = "0" + heightHexString;
                }
                heightHexString += "00";

                // 宽高指令
                string commandHexString = "1D763000";
                commandList.Add(commandHexString + widthHexString + heightHexString);

                List <string>           list = new List <string>(); //binaryString list
                Java.Lang.StringBuilder sb   = new Java.Lang.StringBuilder();
                // 像素二值化,非黑即白
                for (int i = 0; i < partHeight; i++)
                {
                    sb.Delete(0, sb.Length());
                    for (int j = 0; j < width; j++)
                    {
                        // 实际在图片中的高度
                        int startHeight = t * parting + i;
                        //得到当前像素的值
                        int color = image.GetPixel(j, startHeight);
                        int red, green, blue;
                        if (image.HasAlpha)
                        {
                            //得到alpha通道的值
                            int alpha = Color.GetAlphaComponent(color);
                            //得到图像的像素RGB的值
                            red   = Color.GetRedComponent(color);
                            green = Color.GetGreenComponent(color);
                            blue  = Color.GetBlueComponent(color);
                            float offset = alpha / 255.0f;
                            // 根据透明度将白色与原色叠加
                            red   = 0xFF + (int)Java.Lang.Math.Ceil((red - 0xFF) * offset);
                            green = 0xFF + (int)Java.Lang.Math.Ceil((green - 0xFF) * offset);
                            blue  = 0xFF + (int)Java.Lang.Math.Ceil((blue - 0xFF) * offset);
                        }
                        else
                        {
                            //得到图像的像素RGB的值
                            red   = Color.GetRedComponent(color);
                            green = Color.GetGreenComponent(color);
                            blue  = Color.GetBlueComponent(color);
                        }
                        // 接近白色改为白色。其余黑色
                        if (red > 160 && green > 160 && blue > 160)
                        {
                            sb.Append("0");
                        }
                        else
                        {
                            sb.Append("1");
                        }
                    }
                    // 每一行结束时,补充剩余的0
                    if (zeroCount > 0)
                    {
                        sb.Append(zeroStr);
                    }
                    list.Add(sb.ToString());
                }
                // binaryStr每8位调用一次转换方法,再拼合
                List <string> bmpHexList = new List <string>();
                foreach (string binaryStr in list)
                {
                    sb.Delete(0, sb.Length());
                    for (int i = 0; i < binaryStr.Length; i += 8)
                    {
                        string str = binaryStr.Substring(i, i + 8);
                        // 2进制转成16进制
                        string hexString = BinaryStrToHexString(str);
                        sb.Append(hexString);
                    }
                    bmpHexList.Add(sb.ToString());
                }

                // 数据指令
                commandList.AddRange(bmpHexList);
            }
            List <byte[]> data = new List <byte[]>();

            foreach (string hexStr in commandList)
            {
                data.Add(HexStringToBytes(hexStr));
            }
            return(data);
        }