liuxiaolong
2019-05-06 573d1e33a32da5c55c638df2ee622d972976c1d0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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<byte[]> mRgbCameraQueue = new LinkedList<byte[]>();
 
    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();
        }
    }
 
}