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;
    }

 

转载于:https://www.cnblogs.com/eason-d/p/8550096.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值