-
Notifications
You must be signed in to change notification settings - Fork 0
/
CameraActivity.java
185 lines (160 loc) · 5.89 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
package org.faceit.demo;
import android.app.Activity;
import android.content.Intent;
import android.media.Image.Plane;
import android.media.ImageReader.OnImageAvailableListener;
import android.os.Build.VERSION;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.util.Size;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Toast;
import java.nio.ByteBuffer;
import org.faceit.demo.CameraConnectionFragment.ConnectionCallback;
import org.faceit.demo.OverlayView.DrawCallback;
import org.faceit.demo.env.Logger;
public abstract class CameraActivity extends Activity implements OnImageAvailableListener {
private static final Logger LOGGER = new Logger();
private static final int PERMISSIONS_REQUEST = 1;
private static final String PERMISSION_CAMERA = "android.permission.CAMERA";
private static final String PERMISSION_STORAGE = "android.permission.WRITE_EXTERNAL_STORAGE";
private boolean debug = false;
private Handler handler;
private HandlerThread handlerThread;
class C02611 implements ConnectionCallback {
C02611() {
}
public void onPreviewSizeChosen(Size size, int rotation) {
CameraActivity.this.onPreviewSizeChosen(size, rotation);
}
}
protected abstract Size getDesiredPreviewFrameSize();
protected abstract int getLayoutId();
protected abstract void onPreviewSizeChosen(Size size, int i);
protected void onCreate(Bundle savedInstanceState) {
LOGGER.m9d("onCreate " + this, new Object[0]);
super.onCreate(null);
getWindow().addFlags(128);
setContentView(C0196R.layout.activity_camera);
if (hasPermission()) {
setFragment();
} else {
requestPermission();
}
}
public void goPreferences(View view) {
startActivity(new Intent(this, Preferences.class));
}
public synchronized void onStart() {
LOGGER.m9d("onStart " + this, new Object[0]);
super.onStart();
}
public synchronized void onResume() {
LOGGER.m9d("onResume " + this, new Object[0]);
super.onResume();
this.handlerThread = new HandlerThread("inference");
this.handlerThread.start();
this.handler = new Handler(this.handlerThread.getLooper());
}
public synchronized void onPause() {
LOGGER.m9d("onPause " + this, new Object[0]);
if (!isFinishing()) {
LOGGER.m9d("Requesting finish", new Object[0]);
finish();
}
this.handlerThread.quitSafely();
try {
this.handlerThread.join();
this.handlerThread = null;
this.handler = null;
} catch (InterruptedException e) {
LOGGER.m12e(e, "Exception!", new Object[0]);
}
super.onPause();
}
public synchronized void onStop() {
LOGGER.m9d("onStop " + this, new Object[0]);
super.onStop();
}
public synchronized void onDestroy() {
LOGGER.m9d("onDestroy " + this, new Object[0]);
super.onDestroy();
}
protected synchronized void runInBackground(Runnable r) {
if (this.handler != null) {
this.handler.post(r);
}
}
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case 1:
if (grantResults.length > 0 && grantResults[0] == 0 && grantResults[1] == 0) {
setFragment();
return;
} else {
requestPermission();
return;
}
default:
return;
}
}
private boolean hasPermission() {
if (VERSION.SDK_INT < 23) {
return true;
}
if (checkSelfPermission(PERMISSION_CAMERA) == 0 && checkSelfPermission(PERMISSION_STORAGE) == 0) {
return true;
}
return false;
}
private void requestPermission() {
if (VERSION.SDK_INT >= 23) {
if (shouldShowRequestPermissionRationale(PERMISSION_CAMERA) || shouldShowRequestPermissionRationale(PERMISSION_STORAGE)) {
Toast.makeText(this, "Camera AND storage permission are required for this demo", 1).show();
}
requestPermissions(new String[]{PERMISSION_CAMERA, PERMISSION_STORAGE}, 1);
}
}
protected void setFragment() {
getFragmentManager().beginTransaction().replace(C0196R.id.container, CameraConnectionFragment.newInstance(new C02611(), this, getLayoutId(), getDesiredPreviewFrameSize())).commit();
}
protected void fillBytes(Plane[] planes, byte[][] yuvBytes) {
for (int i = 0; i < planes.length; i++) {
ByteBuffer buffer = planes[i].getBuffer();
if (yuvBytes[i] == null) {
LOGGER.m9d("Initializing buffer %d at size %d", Integer.valueOf(i), Integer.valueOf(buffer.capacity()));
yuvBytes[i] = new byte[buffer.capacity()];
}
buffer.get(yuvBytes[i]);
}
}
public boolean isDebug() {
return this.debug;
}
public void requestRender() {
OverlayView overlay = (OverlayView) findViewById(C0196R.id.debug_overlay);
if (overlay != null) {
overlay.postInvalidate();
}
}
public void addCallback(DrawCallback callback) {
OverlayView overlay = (OverlayView) findViewById(C0196R.id.debug_overlay);
if (overlay != null) {
overlay.addCallback(callback);
}
}
public void onSetDebug(boolean debug) {
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode != 25 && keyCode != 24) {
return super.onKeyDown(keyCode, event);
}
this.debug = !this.debug;
requestRender();
onSetDebug(this.debug);
return true;
}
}