forked from IntelRealSense/librealsense
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainActivity.java_
120 lines (96 loc) · 3.58 KB
/
MainActivity.java_
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
package com.example.realsense_app;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Toast;
import java.util.concurrent.atomic.AtomicBoolean;
public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback {
// Used to load the 'native-lib' library on application startup.
static {
System.loadLibrary("native-lib");
}
static String className;
static SurfaceView mSurfaceView;
static SurfaceHolder mSurfaceHolder;
static Paint paint = new Paint();
static AtomicBoolean surfaceExists = new AtomicBoolean(false);
public static void onFrame(Object frameBuff, int width, int height)
{
if (!surfaceExists.get())
return;
byte[] data = (byte[])frameBuff;
int intByteCount = data.length;
int[] intColors = new int[intByteCount / 3];
final int intAlpha = 255;
if ((intByteCount / 3) != (width * height)) {
throw new ArrayStoreException();
}
for (int intIndex = 0; intIndex < intByteCount - 2; intIndex = intIndex + 3) {
intColors[intIndex / 3] = (intAlpha << 24) | (data[intIndex] << 16) | (data[intIndex + 1] << 8) | data[intIndex + 2];
}
Bitmap bitmap = Bitmap.createBitmap(intColors, width, height, Bitmap.Config.ARGB_8888);
bitmap = Bitmap.createScaledBitmap(bitmap, mSurfaceView.getWidth(),
mSurfaceView.getHeight(),
true);
Canvas rCanvas = mSurfaceHolder.lockCanvas();
rCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
// draw the bitmap
rCanvas.drawBitmap(bitmap, 0, 0, paint);
mSurfaceHolder.unlockCanvasAndPost(rCanvas);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
try{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
className = getClass().getName().replace('.', '/');
paint.setColor(0xFFFFFFFF);
paint.setStyle(Paint.Style.STROKE);
mSurfaceView = (SurfaceView) this.findViewById(R.id.camera_surface_view);
mSurfaceHolder = mSurfaceView.getHolder();
mSurfaceHolder.addCallback(this);
startStreaming(className);
}
catch (Exception ex)
{
Toast.makeText(getBaseContext(), ex.getMessage(),
Toast.LENGTH_LONG).show();
}
}
@Override
protected void onDestroy() {
try {
mSurfaceHolder.removeCallback(this);
stopStreaming();
super.onDestroy();
}
catch (Exception ex)
{
Toast.makeText(getBaseContext(), ex.getMessage(),
Toast.LENGTH_LONG).show();
}
}
/**
* A native method that is implemented by the 'native-lib' native library,
* which is packaged with this application.
*/
public native void startStreaming(String className);
public native void stopStreaming();
@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
surfaceExists.set(true);
}
@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
}
@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
surfaceExists.set(false);
}
}