-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathCameraActivity.java
226 lines (185 loc) · 7.73 KB
/
CameraActivity.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
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
package com.kbyai.facelivedemo;
import android.Manifest;
import android.annotation.SuppressLint;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.media.Image;
import android.os.Bundle;
import android.util.Log;
import android.util.Size;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.camera.core.Camera;
import androidx.camera.core.CameraSelector;
import androidx.camera.core.ImageAnalysis;
import androidx.camera.core.ImageProxy;
import androidx.camera.core.Preview;
import androidx.camera.lifecycle.ProcessCameraProvider;
import androidx.camera.view.PreviewView;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import com.google.common.util.concurrent.ListenableFuture;
import com.kbyai.facesdk.FaceBox;
import com.kbyai.facesdk.FaceSDK;
import java.nio.ByteBuffer;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import static androidx.camera.core.ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST;
public class CameraActivity extends AppCompatActivity {
static String TAG = CameraActivity.class.getSimpleName();
static int PREVIEW_WIDTH = 720;
static int PREVIEW_HEIGHT = 1280;
static float LIVENESS_THRESHOLD = 0.7f;
private ExecutorService cameraExecutorService;
private PreviewView viewFinder;
private int lensFacing = CameraSelector.LENS_FACING_FRONT;
private Preview preview = null;
private ImageAnalysis imageAnalyzer = null;
private Camera camera = null;
private CameraSelector cameraSelector = null;
private ProcessCameraProvider cameraProvider = null;
private FaceView faceView;
private TextView initView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
viewFinder = findViewById(R.id.preview);
faceView = findViewById(R.id.faceView);
initView = findViewById(R.id.initView);
cameraExecutorService = Executors.newFixedThreadPool(1);
int ret = FaceSDK.setActivation("GcVbBmdTI24WPXz3tBsr87dHU/KzmFuMYWm4SrLoFrFECoUouwdeSkWhAHi+k/AAYQW22jvctX+j\n" +
"oDg+I1VLKJQgnJS2d80bwCKceYFXOc9A7jBi0/BEKx4PeSGz/PEpoPm/fFT9GrXXnLI4jZj8tMRX\n" +
"cYS4MO7ECQkQmFtqU4wx63mV2MP97WCurOGBRUuVm4dK+mObTVA/rBNrgS75ArcjwwIKexMU5YCY\n" +
"qjLsmADlcnEu5vuYCagDeHyCkUIqvl0/KLicM68BEG2ItUDflhOZJkjOof3gWPSmzJveXuoyPCR5\n" +
"aSBeesUcwQv9xdKciBacMX/pDDMsbqv614gQ5A==");
if(ret == FaceSDK.SDK_SUCCESS) {
ret = FaceSDK.init(getAssets());
}
if(ret != FaceSDK.SDK_SUCCESS) {
initView.setVisibility(View.VISIBLE);
if(ret == FaceSDK.SDK_LICENSE_KEY_ERROR) {
initView.setText("Invalid license!");
} else if(ret == FaceSDK.SDK_LICENSE_APPID_ERROR) {
initView.setText("Invalid error!");
} else if(ret == FaceSDK.SDK_LICENSE_EXPIRED) {
initView.setText("License expired!");
} else if(ret == FaceSDK.SDK_NO_ACTIVATED) {
initView.setText("No activated!");
} else if(ret == FaceSDK.SDK_INIT_ERROR) {
initView.setText("Init error!");
}
}
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
== PackageManager.PERMISSION_DENIED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, 1);
} else {
viewFinder.post(() ->
{
setUpCamera();
});
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(requestCode == 1) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
== PackageManager.PERMISSION_GRANTED) {
viewFinder.post(() ->
{
setUpCamera();
});
}
}
}
private void setUpCamera()
{
ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(CameraActivity.this);
cameraProviderFuture.addListener(() -> {
// CameraProvider
try {
cameraProvider = cameraProviderFuture.get();
} catch (ExecutionException e) {
} catch (InterruptedException e) {
}
// Build and bind the camera use cases
bindCameraUseCases();
}, ContextCompat.getMainExecutor(CameraActivity.this));
}
@SuppressLint({"RestrictedApi", "UnsafeExperimentalUsageError"})
private void bindCameraUseCases()
{
int rotation = viewFinder.getDisplay().getRotation();
cameraSelector = new CameraSelector.Builder().requireLensFacing(lensFacing).build();
preview = new Preview.Builder()
.setTargetResolution(new Size(PREVIEW_WIDTH, PREVIEW_HEIGHT))
.setTargetRotation(rotation)
.build();
imageAnalyzer = new ImageAnalysis.Builder()
.setBackpressureStrategy(STRATEGY_KEEP_ONLY_LATEST)
.setTargetResolution(new Size(PREVIEW_WIDTH, PREVIEW_HEIGHT))
// Set initial target rotation, we will have to call this again if rotation changes
// during the lifecycle of this use case
.setTargetRotation(rotation)
.build();
imageAnalyzer.setAnalyzer(cameraExecutorService, new FaceAnalyzer());
cameraProvider.unbindAll();
try {
camera = cameraProvider.bindToLifecycle(
this, cameraSelector, preview, imageAnalyzer);
preview.setSurfaceProvider(viewFinder.getSurfaceProvider());
} catch (Exception exc) {
}
}
class FaceAnalyzer implements ImageAnalysis.Analyzer
{
@SuppressLint("UnsafeExperimentalUsageError")
@Override
public void analyze(@NonNull ImageProxy imageProxy)
{
analyzeImage(imageProxy);
}
}
@SuppressLint("UnsafeExperimentalUsageError")
private void analyzeImage(ImageProxy imageProxy)
{
try
{
Image image = imageProxy.getImage();
Image.Plane[] planes = image.getPlanes();
ByteBuffer yBuffer = planes[0].getBuffer();
ByteBuffer uBuffer = planes[1].getBuffer();
ByteBuffer vBuffer = planes[2].getBuffer();
int ySize = yBuffer.remaining();
int uSize = uBuffer.remaining();
int vSize = vBuffer.remaining();
byte[] nv21 = new byte[ySize + uSize + vSize];
yBuffer.get(nv21, 0, ySize);
vBuffer.get(nv21, ySize, vSize);
uBuffer.get(nv21, ySize + vSize, uSize);
Bitmap bitmap = FaceSDK.yuv2Bitmap(nv21, image.getWidth(), image.getHeight(), 7);
List<FaceBox> faceBoxes = FaceSDK.faceDetection(bitmap);
runOnUiThread(new Runnable() {
@Override
public void run() {
faceView.setFrameSize(new Size(bitmap.getWidth(), bitmap.getHeight()));
faceView.setFaceBoxes(faceBoxes);
}
});
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
imageProxy.close();
}
}
}