Minicap数据解析(java)

class ImageConverter implements Runnable {
private int readBannerBytes = 0;
private int bannerLength = 2;
private int readFrameBytes = 0;
private int frameBodyLength = 0;
private byte[] frameBody = new byte[0];

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Runnable#run()
     */
    public void run() {
        // TODO Auto-generated method stub
        long start = System.currentTimeMillis();
        while (isRunning) {
            if (dataQueue.isEmpty()) {
                // LOG.info("数据队列为空");
                continue;
            }
            byte[] buffer = dataQueue.poll();
            int len = buffer.length;
            for (int cursor = 0; cursor < len;) {
                int byte10 = buffer[cursor] & 0xff;
                if (readBannerBytes < bannerLength) {
                    cursor = parserBanner(cursor, byte10);
                } else if (readFrameBytes < 4) {
                    // 第二次的缓冲区中前4位数字和为frame的缓冲区大小
                    frameBodyLength += (byte10 << (readFrameBytes * 8)) >>> 0;
                    cursor += 1;
                    readFrameBytes += 1;
                    // LOG.debug("解析图片大小 = " + readFrameBytes);
                } else {
                    if (len - cursor >= frameBodyLength) {
                        LOG.debug("frameBodyLength = " + frameBodyLength);
                        byte[] subByte = subByteArray(buffer, cursor,
                                cursor + frameBodyLength);
                        frameBody = byteMerger(frameBody, subByte);
                        if ((frameBody[0] != -1) || frameBody[1] != -40) {
                            LOG.error(String
                                    .format("Frame body does not start with JPG header"));
                            return;
                        }
                        final byte[] finalBytes = subByteArray(frameBody,
                                0, frameBody.length);
                        // 转化成bufferImage
                        new Thread(new Runnable() {

                            @Override
                            public void run() {
                                // TODO Auto-generated method stub
                                Image image = createImageFromByte(finalBytes);
                                notifyObservers(image);
                            }
                        }).start();

                        long current = System.currentTimeMillis();
                        LOG.info("图片已生成,耗时: "
                                + TimeUtil.formatElapsedTime(current
                                        - start));
                        start = current;
                        cursor += frameBodyLength;
                        restore();
                    } else {
                        LOG.debug("所需数据大小 : " + frameBodyLength);
                        byte[] subByte = subByteArray(buffer, cursor, len);
                        frameBody = byteMerger(frameBody, subByte);
                        frameBodyLength -= (len - cursor);
                        readFrameBytes += (len - cursor);
                        cursor = len;
                    }
                }
            }
        }

    }

    private void restore() {
        frameBodyLength = 0;
        readFrameBytes = 0;
        frameBody = new byte[0];
    }

    private int parserBanner(int cursor, int byte10) {
        switch (readBannerBytes) {
        case 0:
            // version
            banner.setVersion(byte10);
            break;
        case 1:
            // length
            bannerLength = byte10;
            banner.setLength(byte10);
            break;
        case 2:
        case 3:
        case 4:
        case 5:
            // pid
            int pid = banner.getPid();
            pid += (byte10 << ((readBannerBytes - 2) * 8)) >>> 0;
            banner.setPid(pid);
            break;
        case 6:
        case 7:
        case 8:
        case 9:
            // real width
            int realWidth = banner.getReadWidth();
            System.out.println("realwidth0"+realWidth);
            realWidth += (byte10 << ((readBannerBytes - 6) * 8)) >>> 0;
            System.out.println("realwidth1"+realWidth);
            banner.setReadWidth(realWidth);
            break;
        case 10:
        case 11:
        case 12:
        case 13:
            // real height
            int realHeight = banner.getReadHeight();
            realHeight += (byte10 << ((readBannerBytes - 10) * 8)) >>> 0;
            banner.setReadHeight(realHeight);
            break;
        case 14:
        case 15:
        case 16:
        case 17:
            // virtual width
            int virtualWidth = banner.getVirtualWidth();
            virtualWidth += (byte10 << ((readBannerBytes - 14) * 8)) >>> 0;
            banner.setVirtualWidth(virtualWidth);
            System.out.println("virtual"+virtualWidth);
            break;
        case 18:
        case 19:
        case 20:
        case 21:
            // virtual height
            int virtualHeight = banner.getVirtualHeight();
            virtualHeight += (byte10 << ((readBannerBytes - 18) * 8)) >>> 0;
            banner.setVirtualHeight(virtualHeight);
            System.out.println("virtualhegith"+virtualHeight);
            break;
        case 22:
            // orientation
            banner.setOrientation(byte10 * 90);
            break;
        case 23:
            // quirks
            banner.setQuirks(byte10);
            break;
        }

        cursor += 1;
        readBannerBytes += 1;

        if (readBannerBytes == bannerLength) {
            LOG.debug(banner.toString());
        }
        return cursor;
    }
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,805评论 18 399
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,156评论 19 139
  • 八组的孩子集体没交作业,以往的我一定会暴跳如雷,然后恶语相向,再用并无实效的方法惩戒他们,以发泄我心中的怒...
    芳菲_0128阅读 192评论 0 0
  • 何以解忧?唯有杜康。在这里,没有解忧酒,唯有悠悠(店主)。 悠悠铺为你存放记忆。随存随取,分文不收,只...
    叶悠悠阅读 366评论 0 0
  • (一) 经常听到这样一句话:“能力越差的人,脾气越大。”我们办公室的王大姐就属于典型的这类的人。 她就是负责我们办...
    协和康复刘淑芬阅读 522评论 2 3