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

你可能感兴趣的:(Minicap数据解析(java))