package com.basic.security.utils.yuv.pull.server; import android.os.SystemClock; import com.basic.security.utils.Constants; import com.basic.security.utils.Preview; import com.google.gson.JsonObject; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.net.Socket; import java.util.LinkedList; public class AcceptedClient extends Thread { Socket mSocket; private static final int MAX_BUFFER = 15; private static LinkedList mRgbCameraQueue = new LinkedList(); public AcceptedClient(Socket client) { mSocket = client; } public static void addCameraData(byte[] data) { synchronized (mRgbCameraQueue) { if (mRgbCameraQueue.size() == MAX_BUFFER) { mRgbCameraQueue.poll(); } mRgbCameraQueue.add(data); } } public byte[] getImageBuffer() { byte[] mLastFrame = null; synchronized (mRgbCameraQueue) { if (mRgbCameraQueue.size() > 0) { mLastFrame = mRgbCameraQueue.poll(); } } return mLastFrame; } @Override public void run() { try { BufferedOutputStream outputStream = new BufferedOutputStream(mSocket.getOutputStream()); BufferedInputStream inputStream = new BufferedInputStream(mSocket.getInputStream()); JsonObject jsonObj = new JsonObject(); jsonObj.addProperty("type", "data"); jsonObj.addProperty("length", 460800 ); jsonObj.addProperty("width", 640); jsonObj.addProperty("height", 480); int rotation = 0; if (Constants.isHuaWeiPad) { rotation = 270;// 华为平板270 } else { rotation = 90; } jsonObj.addProperty("rotation", rotation); byte[] buff = new byte[256]; int len = 0; String msg = null; outputStream.write(jsonObj.toString().getBytes()); outputStream.flush(); while (true) { try { SystemClock.sleep(40); // 一秒发送25张 byte[] byteArray = getImageBuffer(); if (byteArray == null) { continue; } outputStream.write(byteArray); outputStream.flush(); if (Thread.currentThread().isInterrupted()) break; } catch (Exception e) { e.printStackTrace(); break; } } } catch (Exception e) { e.printStackTrace(); } } }