zoukankan      html  css  js  c++  java
  • 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;
        }
  • 相关阅读:
    ubuntu 1510
    创业者的困境
    创业者的窘境
    (转)Ubuntu 16.04下搭建Web服务器(MySQL+PHP+Apache)
    ESP8266的smartconfig过程(使用esptouch)
    Virtualbox下ubuntu识别USB设备
    红外人体检测
    树莓派跑讯飞语音识别(2麦阵列)
    R16平台中断
    语音识别下的智能家居
  • 原文地址:https://www.cnblogs.com/eason-d/p/8550096.html
Copyright © 2011-2022 走看看