rk
昨天 4a8ff39b0fab0627ef8f7459587d514cc01c3676
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package com.example.datalibrary.deptrum;
 
import android.os.Bundle;
import android.util.Log;
 
import com.deptrum.usblite.callback.IDeviceListener;
import com.deptrum.usblite.callback.IStreamListener;
import com.deptrum.usblite.param.DTFrameStreamBean;
import com.deptrum.usblite.param.StreamParam;
import com.deptrum.usblite.param.StreamType;
import com.deptrum.usblite.sdk.DeptrumSdkApi;
import com.example.datalibrary.activity.BaseActivity;
 
import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.imgproc.Imgproc;
 
import java.util.concurrent.Executors;
 
public abstract class BaseDeptrumActivity extends BaseActivity {
 
 
    protected static final int RGB_WIDTH = 480;
    protected static final int RGB_HEIGHT = 768;
 
    private GLDisplay mRgbisplay;
    private GLDisplay mIrDisplay;
    private GLDisplay mDepthDisplay;
 
    protected GLFrameSurface mRgbSurface;
    protected GLFrameSurface mIrSurface;
    protected GLFrameSurface mDepSurface;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mRgbisplay = new GLDisplay();
        mIrDisplay = new GLDisplay();
        mDepthDisplay = new GLDisplay();
        // 摄像头图像预览
        Executors.newSingleThreadExecutor().execute(new Runnable() {
            @Override
            public void run() {
                openDevice();
            }
        });
    }
 
    private void openDevice() {
        final long startTime = System.currentTimeMillis();
        DeptrumSdkApi.getApi().open(getApplicationContext(), new IDeviceListener() {
            @Override
            public void onAttach() {
 
            }
 
            @Override
            public void onDetach() {
 
            }
 
            @Override
            public void onOpenResult(int result) {
                if (0 == result) {
//                    mHandler.sendEmptyMessage(MESSAGE_UI);
                    DeptrumSdkApi.getApi().setStreamListener(new IStreamListener() {
                        @Override
                        public void onFrame(final DTFrameStreamBean iFrame) {
 
                            if (isFinishing()){
                                return;
                            }
 
                            final byte[] data = iFrame.getData();
                            switch (iFrame.getImageType()) {
                                case RGB: {
                                    long endTime = System.currentTimeMillis();
                                    Log.d("xjk open -> stream ", (endTime - startTime) + "");
                                    convertRGBToRGBA(data, RGB_WIDTH, RGB_HEIGHT);
                                    Log.d("xjk open", "rgb数据接收" + data.length);
                                    dealRgb(data);
                                    if (null != data) {
                                        mRgbSurface.post(new Runnable() {
                                            @Override
                                            public void run() {
                                                if (null != mRgbisplay && null != mRgbSurface){
                                                    mRgbisplay.render(mRgbSurface, 0, false, data,
                                                            480, 768, 1);
                                                }
                                            }
                                        });
                                    }
                                }
                                break;
                                case IR: {
                                    if (null == data || RGB_WIDTH * RGB_HEIGHT != data.length) {
                                        return;
                                    }
                                    Log.d("xjk open", "nir数据接收" + data.length);
                                    convertGrayToRGBA(data, RGB_WIDTH, RGB_HEIGHT);
                                    dealIr(mIrBits);
                                    mIrSurface.post(new Runnable() {
                                        @Override
                                        public void run() {
                                            if (null != mIrDisplay && null != mIrSurface) {
                                                mIrDisplay.render(mIrSurface, 0, false, mIrBits,
                                                        RGB_WIDTH, RGB_HEIGHT, 1);
                                            }
                                        }
                                    });
                                }
                                break;
                                case DEPTH: {
                                    if (null != data ) {
                                        Log.d("xjk open", "depth数据接收" + data.length);
                                        dealDepth(data);
                                        convertDepthToRGBA(data, RGB_WIDTH, RGB_HEIGHT);
                                        mDepSurface.post(new Runnable() {
                                            @Override
                                            public void run() {
                                                if (null != mDepthDisplay && null != mDepSurface) {
                                                    mDepthDisplay.render(mDepSurface, 0, false, depthGLData,
                                                            RGB_WIDTH, RGB_HEIGHT, 1);
                                                }
                                            }
                                        });
                                    }
                                }
                                break;
                            }
                        }
                    });
                    DeptrumSdkApi.getApi().setScanFaceMode();
                    StreamParam param = new StreamParam();
                    param.width = RGB_WIDTH;
                    param.height = RGB_HEIGHT;
                    DeptrumSdkApi.getApi().setStreamParam(param);
                    DeptrumSdkApi.getApi().startStream(StreamType.STREAM_RGB_IR_DEPTH);
                    DeptrumSdkApi.getApi().configSet("enable_dt_face_kit", "1");
                }
            }
 
            @Override
            public void onErrorEvent(String s, int i) {
 
            }
        });
    }
    protected abstract void dealRgb(byte[] data);
    protected abstract void dealIr(byte[] data);
    protected abstract void dealDepth(byte[] data);
 
    private byte[] mRgbBits = null;
    private int mRgbLength = 0;
    public void convertRGBToRGBA(byte[] data, int width, int height) {
        try {
            int len = data.length / 3 * 4;
            if (null == mRgbBits || len != mRgbLength){
                mRgbBits = new byte[data.length / 3 * 4]; // RGBA 数组
                mRgbLength = data.length / 3 * 4;
            }
 
//            byte[] Bits = new byte[data.length / 3 * 4]; // RGBA 数组
            int i;
            for (i = 0; i < data.length / 3; i++) {
                // 原理:4个字节表示一个灰度,则RGB  = 灰度值,最后一个Alpha = 0xff;
                mRgbBits[i * 4] = data[i * 3];
                mRgbBits[i * 4 + 1] = data[i * 3 + 1];
                mRgbBits[i * 4 + 2] = data[i * 3 + 2];
                mRgbBits[i * 4 + 3] = -1; // 0xff
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }  /**
     * gray convert to rgba
     *
     * @param data
     * @param width
     * @param height
     * @return
     */
    private Mat outPutMat;
 
    protected byte[] decodeIrData(byte[] byteArr, int width, int height){
        byte[] bytes = new byte[width * height * 3];
        if (null == outPutMat){
            return bytes;
        }
        Mat rawMat = new MatOfByte(byteArr);
        rawMat.convertTo(rawMat, CvType.CV_8UC1, 1.0D, 0);
        Imgproc.cvtColor(rawMat, outPutMat, Imgproc.COLOR_GRAY2RGB);
        outPutMat.get(0, 0, bytes);
        rawMat.release();
 
        return bytes;
    }
    byte[] mIrBits = null;
    public byte[] convertGrayToRGBA(byte[] data, int width, int height) {
        try {
            int mIrLength = 0;
            int len = data.length * 4;
            if (null == mIrBits || len != mIrLength){
//                mIrBits = new byte[data.length * 4]; // RGBA 数组
                mIrLength = data.length * 4;
            }
            mIrBits = decodeIrData(data , width , height);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mIrBits ;
    }
    int mDeDataByteLenght = 0;
    // 摄像头采集数据
    private volatile byte[] depthGLData;
    public void convertDepthToRGBA(byte[] data, int width, int height) {
        try {
            if (null == depthGLData || mDeDataByteLenght != width * height * 3){
                mDeDataByteLenght = width * height * 3;
            }
            depthGLData = decodeDepthData(data);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    protected byte[] decodeDepthData(byte[] byteArr){
        byte[] bytes = new byte[480 * 768 * 3];
        if (null == outPutMat){
            return bytes;
        }
        byte[] colorImage = DeptrumSdkApi.getApi()
                .drawDepthMapInColor(768, 480, 0, 1500, 6000, byteArr);
        // byte转Mat
        Mat mat = new Mat(768, 480, CvType.CV_8UC3);
        mat.put(0, 0, colorImage);
 
        Imgproc.cvtColor(mat, outPutMat, Imgproc.COLOR_BGR2RGB);
        outPutMat.get(0, 0, bytes);
        mat.release();
        return bytes;
    }
    @Override
    protected void onResume() {
        super.onResume();
 
        if (!OpenCVLoader.initDebug()) {
            Log.d(getClass().getName(), "Internal OpenCV library not found. Using OpenCV manger for initialization");
            OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION, getApplicationContext(), mLoaderCallback);
        } else {
            Log.d(getClass().getName(), "OpenCV library found inside package. Using it!");
            mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
        }
    }
    protected final BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
        @Override
        public void onManagerConnected(int status) {
            switch (status) {
                case LoaderCallbackInterface.SUCCESS: {
                    Log.e("lbc_opencv", "OpenCV loaded successfully");
                    outPutMat = new Mat(RGB_HEIGHT,RGB_WIDTH, CvType.CV_8UC3);
                }
                break;
                default: {
                    super.onManagerConnected(status);
                }
                break;
            }
        }
    };
 
    @Override
    protected void onDestroy() {
        super.onDestroy();
 
        DeptrumSdkApi.getApi().stopStream(StreamType.STREAM_RGB_IR_DEPTH);
        DeptrumSdkApi.getApi().setStreamListener(null);
        mRgbisplay.release();
        mRgbisplay = null;
 
        mIrDisplay.release();
        mIrDisplay = null;
 
        mDepthDisplay.release();
        mDepthDisplay = null;
    }
}