• Android bitmap转byte


    逆转start协议输出

        private static byte[] bitmap2byte(Bitmap bmp) throws Exception{
            return bitmap2byte(bmp, null);
        }
        
        private static byte[] bitmap2byte(Bitmap bmp, StarReverseConfig config) throws Exception{
            Long s = System.currentTimeMillis();
            String TAG = "Start Bitmap2byte";
            int imgWidth = bmp.getWidth();
            int imgHeight = bmp.getHeight();
            
            //头  无配置
            byte[] header = { 0x1b, 0x2a, 0x72, 0x42, 
                    0x1b, 0x2a, 0x72, 0x61,    
                    0x1b, 0x2a, 0x72, 0x41 };
            
            //参数 配置  Set print speed : 1B1E74
            //0x1b, 0x2a, 0x72, 0x6d, 0x6c, 0x24, 0x00  1b 2a 72 6d 6c n 00; Set raster left margin: n x 8
            byte[] params = {  };
            //0x1b, 0x6c, 0x24     Set raster left margin
            //0x1b, 0x1d, 0x41  n1 n2 Point absolute position
            //尾 无配置
            byte[] footer = { 0x1b, 0x2a, 0x72, 0x65, 0x31, 0x33, 0x00, 0x1b, 0x0c, 0x19, 0x00 };
            //Storellet         
            //byte[] header = {0x1b, 0x2a, 0x72, 0x42, 0x1b, 0x1d, 0x03, 0x04, 0x00, 0x00, 0x17, 0x00, 0x1b, 0x1e, 0x45, 0x00, 0x1b, 0x06, 0x01, 0x00, 0x1b, 0x06, 0x01, 0x00, 0x1b, 0x1d, 0x03, 0x03, 0x00, 0x00, 0x1b, 0x2a, 0x72, 0x41, 0x1b, 0x2a, 0x72, 0x50, 0x30, 0x00, 0x1b, 0x2a, 0x72, 0x45, 0x31, 0x00, 0x1b, 0x2a, 0x72, 0x59, 0x30, 0x30, 0x33, 0x00 };
            //Storellet 
            //byte[] footer = { 0x1b, 0x2a, 0x72, 0x59, 0x30, 0x30, 0x31, 0x00, 0x1b, 0x2a, 0x72, 0x65, 0x31, 0x33, 0x00, 0x1b, 0x0c, 0x19, 0x1b, 0x2a, 0x72, 0x46, 0x31, 0x33, 0x00, 0x1b, 0x0c, 0x00, 0x1b, 0x2a, 0x72, 0x42, 0x1b, 0x2a, 0x72, 0x42, 0x1b, 0x1d, 0x03, 0x04, 0x00, 0x00, 0x17, 0x17, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
            
            // 每行的字节数
            int lineBytes = imgWidth / 8 + (imgWidth % 8 > 0 ? 1 : 0);
            
            //位移
            int movePosition = config != null ? config.getSetLeftWidth() / 8 : 0;
            
            Log.i(TAG,"reverse lineBytes:" + lineBytes + ", imgWidth:" + imgWidth + ", imgHeight:" + imgHeight);
            // 总字节长度
            byte[] srcbytes = new byte[header.length + params.length + footer.length + imgHeight * (lineBytes + 3 + movePosition)];
            
            int srclen = 0;
            System.arraycopy(header, 0, srcbytes,  srclen, header.length);
            srclen = srclen + header.length;
            System.arraycopy(params,  0, srcbytes, srclen, params.length);
            srclen = srclen + params.length;
            
            byte n1byte = (byte)((lineBytes + movePosition) % 256); 
            byte n2byte = (byte)((lineBytes + movePosition) / 256); 
            byte tmp = 0;
            byte p1 = 0x01, p0 = 0x00;
            int xcnt = 3; // x方向上的计数
            
            for (int y = 0; y < imgHeight; y++) {
                byte[] lineBytesArray = new byte[lineBytes + 3 + movePosition];
                //System.arraycopy(movePosition,  0, lineBytesArray, 0, movePosition.length);
                lineBytesArray[0] = (byte)0x62;
                lineBytesArray[1] = n1byte;
                lineBytesArray[2] = n2byte;
                
                xcnt = 3;     // x方向上的计数初始值
                for (int i = 0; i < movePosition; i++) {
                    lineBytesArray[xcnt] = 0x00;
                    xcnt++;
                }
                
                for (int x = 0; x < imgWidth; x++) {
                    if (x % 8 == 0 || x == imgWidth -1) {
                        if(x != 0){
                            lineBytesArray[xcnt] = tmp;
                            xcnt++;
                        }
                        tmp = 0;
                    }
                    int r = bmp.getPixel(x, y);
                    if (r == Color.BLACK) {
                        tmp += p1 * Math.pow(2, 7 - x % 8); //高位开始
                    } else {
                        tmp += p0 * Math.pow(2, 7 - x % 8); 
                    }
                }
                System.arraycopy(lineBytesArray,  0, srcbytes, srclen, lineBytesArray.length);
                srclen = srclen + lineBytesArray.length;
                Thread.yield();
            }
            
            
            System.arraycopy(footer,  0, srcbytes, srclen, footer.length);
            Log.i(TAG,"reverse receipt is :" + srcbytes.length + ", all Time:" + (System.currentTimeMillis() - s));
            return srcbytes;
        }
  • 相关阅读:
    Java MVC和三层架构
    EL表达式
    EL表达式中的11个隐式对象
    JDBC连接数据库7个步骤
    JSP九大内置对象和四个作用域
    Eclipse常用快捷键大全
    Java的绝对路径和相对路径
    Servlet中相对路径与绝对路径
    mysql8的深坑
    mysql单列索引和联合索引
  • 原文地址:https://www.cnblogs.com/eason-d/p/8550096.html
Copyright © 2020-2023  润新知