java解析数据,Minicap数据解析(java)

该代码实现了一个从数据队列中读取字节并解析图片帧的线程。它首先解析图片的元数据(如版本、长度、PID等),然后获取帧的大小,并将帧数据合并。一旦找到有效的JPEG头,就创建图片并通知观察者。整个过程记录了耗时,并在队列空时继续等待新的数据。
摘要由CSDN通过智能技术生成

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;

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值