|
3#
楼主 |
发表于 2017-6-22 11:59:48
|
只看该作者
- break;
- case 18:
- case 19:
- case 20:
- case 21:
- // virtual height
- int virtualHeight = banner.getVirtualHeight();
- virtualHeight += (byte10 << ((readBannerBytes - 18) * 8)) >>> 0;
- banner.setVirtualHeight(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.info(banner.toString());
- }
- } else if (readFrameBytes < 4) {
- // 第二次的缓冲区中前4位数字和为frame的缓冲区大小
- frameBodyLength += (byte10 << (readFrameBytes * 8)) >>> 0;
- cursor += 1;
- readFrameBytes += 1;
- total = frameBodyLength;
- } else {
- LOG.info("图片大小 : " + total);
- // LOG.info("frame body部分");
- // LOG.info(String.format("设想图片的大小 : %d", total));
- if (len - cursor >= frameBodyLength) {
- byte[] subByte = subByteArray(currentBuffer,
- 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;
- }
- LOG.info(String.format("实际图片的大小 : %d",
- frameBody.length));
- if (finalBytes == null) {
- finalBytes = subByteArray(frameBody, 0,
- frameBody.length);
- new Thread(new Runnable() {
- @Override
- public void run() {
- // TODO Auto-generated method stub
- try {
- createImageFromByte();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }).start();
- }
- cursor += frameBodyLength;
- frameBodyLength = 0;
- readFrameBytes = 0;
- frameBody = new byte[0];
- } else {
- // LOG.debug(String.format("body(len=%d)", len
- // - cursor));
- byte[] subByte = subByteArray(currentBuffer,
- cursor, len);
- frameBody = byteMerger(frameBody, subByte);
- frameBodyLength -= (len - cursor);
- readFrameBytes += (len - cursor);
- cursor = len;
- }
- }
- }
- }
- } catch (UnknownHostException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- finally {
- if (socket != null && socket.isConnected()) {
- try {
- socket.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- if (stream != null) {
- try {
- stream.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- }
- private synchronized void createImageFromByte() throws IOException {
- if (finalBytes.length == 0) {
- LOG.info("frameBody大小为0");
- }
- InputStream in = new ByteArrayInputStream(finalBytes);
- BufferedImage bufferedImage = ImageIO.read(in);
- notifyObservers(bufferedImage);
- // String filePath = String.format("0.jpg");
- // LOG.info(filePath);
- // ImageIO.write(bufferedImage, "jpg", new File(filePath));
- finalBytes = null;
- }
- // java合并两个byte数组
- private static byte[] byteMerger(byte[] byte_1, byte[] byte_2) {
- byte[] byte_3 = new byte[byte_1.length + byte_2.length];
- System.arraycopy(byte_1, 0, byte_3, 0, byte_1.length);
- System.arraycopy(byte_2, 0, byte_3, byte_1.length, byte_2.length);
- return byte_3;
- }
- private static byte[] subByteArray(byte[] byte1, int start, int end) {
- byte[] byte2 = new byte[end - start];
- System.arraycopy(byte1, start, byte2, 0, end - start);
- return byte2;
- }
- private String bytesToHexString(byte[] src) {
- StringBuilder stringBuilder = new StringBuilder();
- if (src == null || src.length <= 0) {
- return null;
- }
- for (int i = 0; i < src.length; i++) {
- int v = src[i] & 0xFF;
- String hv = Integer.toHexString(v);
- if (hv.length() < 2) {
- stringBuilder.append(0);
- }
- stringBuilder.append(hv + " ");
- }
- return stringBuilder.toString();
- }
- /*
- * (non-Javadoc)
- *
- * @see
- * com.wuba.utils.screenshot.AndroidScreenSubject#registerObserver(com.wuba
- * .utils.screenshot.AndroidScreenObserver)
- */
- @Override
- public void registerObserver(AndroidScreenObserver o) {
- // TODO Auto-generated method stub
- observers.add(o);
- }
- /*
- * (non-Javadoc)
- *
- * @see
- * com.wuba.utils.screenshot.AndroidScreenSubject#removeObserver(com.wuba
- * .utils.screenshot.AndroidScreenObserver)
- */
- @Override
- public void removeObserver(AndroidScreenObserver o) {
- // TODO Auto-generated method stub
- int index = observers.indexOf(o);
- if (index != -1) {
- observers.remove(o);
- }
- }
- /*
- * (non-Javadoc)
- *
- * @see com.wuba.utils.screenshot.AndroidScreenSubject#notifyObservers()
- */
- @Override
- public void notifyObservers(BufferedImage image) {
- // TODO Auto-generated method stub
- for (AndroidScreenObserver observer : observers) {
- observer.frameImageChange(image);
- }
- }
- }
复制代码 |
|