Skip to content
This repository was archived by the owner on Sep 13, 2024. It is now read-only.

Commit 4bdb2cb

Browse files
First implemention of face recognition #27
1 parent 55f1d1c commit 4bdb2cb

6 files changed

Lines changed: 215 additions & 6 deletions

File tree

0 Bytes
Binary file not shown.

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<!-- Give permission to use Internet -->
66
<uses-permission android:name="android.permission.INTERNET" />
77
<uses-permission android:name="android.permission.REORDER_TASKS" />
8-
8+
<uses-permission android:name="android.permission.CAMERA" />
99

1010
<application
1111
android:allowBackup="false"

app/src/main/java/com/coderbunker/kioskapp/KioskActivity.java

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import android.content.Intent;
88
import android.content.SharedPreferences;
99
import android.content.pm.ActivityInfo;
10+
import android.content.pm.PackageManager;
11+
import android.hardware.Camera;
1012
import android.net.http.SslError;
1113
import android.os.Bundle;
1214
import android.view.KeyEvent;
@@ -18,22 +20,29 @@
1820
import android.webkit.WebView;
1921
import android.webkit.WebViewClient;
2022
import android.widget.Button;
23+
import android.widget.FrameLayout;
24+
import android.widget.TextView;
2125
import android.widget.Toast;
2226

27+
import com.coderbunker.kioskapp.facerecognition.CameraPreview;
28+
import com.coderbunker.kioskapp.facerecognition.FaceDetectionListener;
2329
import com.coderbunker.kioskapp.lib.HOTP;
2430
import com.coderbunker.kioskapp.lib.TOTP;
2531

2632
import java.util.ArrayList;
2733
import java.util.Arrays;
2834
import java.util.List;
35+
import java.util.Observable;
36+
import java.util.Observer;
2937
import java.util.Timer;
3038
import java.util.TimerTask;
3139

3240

33-
public class KioskActivity extends Activity {
41+
public class KioskActivity extends Activity implements Observer {
3442

3543
private final Context context = this;
3644
private WebView webView;
45+
private TextView face_detection_score;
3746
private static String password = "1234";
3847
private static String URL = "";
3948

@@ -55,6 +64,9 @@ public class KioskActivity extends Activity {
5564

5665
private SharedPreferences prefs;
5766

67+
private Camera mCamera;
68+
private CameraPreview mCameraPreview;
69+
5870
@Override
5971
public void onBackPressed() {
6072
//Do nothing...
@@ -92,6 +104,7 @@ protected void onCreate(Bundle savedInstanceState) {
92104

93105
//Get the webView and load the URL
94106
webView = findViewById(R.id.webview);
107+
face_detection_score = findViewById(R.id.face_detection_score);
95108
webView.setWebViewClient(new WebViewClient() {
96109
@Override
97110
public void onPageFinished(final WebView view, String url) {
@@ -185,6 +198,28 @@ public void run() {
185198
});
186199

187200
numbers = new ArrayList<>();
201+
202+
203+
if (checkCameraHardware(this)) {
204+
mCamera = getCameraInstance();
205+
if (mCamera != null) {
206+
207+
FaceDetectionListener faceDetectionListener = new FaceDetectionListener();
208+
faceDetectionListener.addObserver(this);
209+
mCamera.setFaceDetectionListener(faceDetectionListener);
210+
211+
mCameraPreview = new CameraPreview(this, mCamera);
212+
213+
FrameLayout preview = findViewById(R.id.camera_preview);
214+
preview.addView(mCameraPreview);
215+
216+
mCamera.startPreview();
217+
} else {
218+
219+
}
220+
}
221+
222+
188223
}
189224

190225

@@ -398,4 +433,40 @@ public void onClick(View v) {
398433
dialog.show();
399434
}
400435

436+
public static Camera getCameraInstance() {
437+
Camera c = null;
438+
try {
439+
c = Camera.open(Camera.CameraInfo.CAMERA_FACING_FRONT); // attempt to get a Camera instance
440+
} catch (Exception e) {
441+
// Camera is not available (in use or does not exist)
442+
e.printStackTrace();
443+
}
444+
return c; // returns null if camera is unavailable
445+
}
446+
447+
private boolean checkCameraHardware(Context context) {
448+
if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
449+
// this device has a camera
450+
return true;
451+
} else {
452+
// no camera on this device
453+
return false;
454+
}
455+
}
456+
457+
private long last_detected = 0;
458+
459+
@Override
460+
public void update(Observable o, Object arg) {
461+
if (o instanceof FaceDetectionListener) {
462+
Camera.Face face = ((Camera.Face) arg);
463+
464+
face_detection_score.setText("Score:" + face.score);
465+
466+
last_detected = System.currentTimeMillis();
467+
if (face.score >= 85 && last_detected <= System.currentTimeMillis() + 5000) {
468+
Toast.makeText(context, "Hello face +1", Toast.LENGTH_SHORT).show();
469+
}
470+
}
471+
}
401472
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package com.coderbunker.kioskapp.facerecognition;
2+
3+
import android.content.Context;
4+
import android.hardware.Camera;
5+
import android.util.Log;
6+
import android.view.SurfaceHolder;
7+
import android.view.SurfaceView;
8+
9+
import java.io.IOException;
10+
11+
import static android.content.ContentValues.TAG;
12+
13+
/**
14+
* A basic Camera preview class
15+
*/
16+
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
17+
private SurfaceHolder mHolder;
18+
private Camera mCamera;
19+
20+
public CameraPreview(Context context, Camera camera) {
21+
super(context);
22+
mCamera = camera;
23+
24+
// Install a SurfaceHolder.Callback so we get notified when the
25+
// underlying surface is created and destroyed.
26+
mHolder = getHolder();
27+
mHolder.addCallback(this);
28+
// deprecated setting, but required on Android versions prior to 3.0
29+
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
30+
}
31+
32+
public void surfaceCreated(SurfaceHolder holder) {
33+
try {
34+
mCamera.setPreviewDisplay(holder);
35+
mCamera.startPreview();
36+
37+
startFaceDetection(); // start face detection feature
38+
39+
} catch (IOException e) {
40+
Log.d(TAG, "Error setting camera preview: " + e.getMessage());
41+
}
42+
}
43+
44+
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
45+
46+
if (holder.getSurface() == null) {
47+
// preview surface does not exist
48+
Log.d(TAG, "holder.getSurface() == null");
49+
return;
50+
}
51+
52+
try {
53+
mCamera.stopPreview();
54+
55+
} catch (Exception e) {
56+
// ignore: tried to stop a non-existent preview
57+
Log.d(TAG, "Error stopping camera preview: " + e.getMessage());
58+
}
59+
60+
try {
61+
mCamera.setPreviewDisplay(holder);
62+
mCamera.startPreview();
63+
64+
startFaceDetection(); // re-start face detection feature
65+
66+
} catch (Exception e) {
67+
// ignore: tried to stop a non-existent preview
68+
Log.d(TAG, "Error starting camera preview: " + e.getMessage());
69+
}
70+
}
71+
72+
public void surfaceDestroyed(SurfaceHolder holder) {
73+
// empty. Take care of releasing the Camera preview in your activity.
74+
}
75+
76+
public void startFaceDetection() {
77+
// Try starting Face Detection
78+
Camera.Parameters params = mCamera.getParameters();
79+
80+
// start face detection only *after* preview has started
81+
if (params.getMaxNumDetectedFaces() > 0) {
82+
// camera supports face detection, so can start it:
83+
mCamera.startFaceDetection();
84+
}
85+
}
86+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.coderbunker.kioskapp.facerecognition;
2+
3+
import android.hardware.Camera;
4+
5+
import java.util.Observable;
6+
7+
public class FaceDetectionListener extends Observable implements Camera.FaceDetectionListener {
8+
9+
@Override
10+
public void onFaceDetection(Camera.Face[] faces, Camera camera) {
11+
if (faces.length > 0) {
12+
for (Camera.Face face : faces) {
13+
try {
14+
/*System.out.println("--------------------------");
15+
System.out.println(face.score);*/
16+
setChanged();
17+
notifyObservers(face);
18+
} catch (Exception e) {
19+
e.printStackTrace();
20+
}
21+
}
22+
}
23+
}
24+
}
Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,39 @@
11
<?xml version="1.0" encoding="utf-8"?>
22

3+
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
4+
xmlns:tools="http://schemas.android.com/tools"
5+
android:layout_width="match_parent"
6+
android:layout_height="match_parent">
37

4-
<WebView android:id="@+id/webview"
5-
android:layout_width="fill_parent"
6-
android:layout_height="fill_parent"
7-
xmlns:android="http://schemas.android.com/apk/res/android">
8+
<WebView
9+
android:id="@+id/webview"
10+
android:layout_width="fill_parent"
11+
android:layout_height="fill_parent">
812

913
</WebView>
1014

1115

16+
<FrameLayout
17+
android:id="@+id/camera_preview"
18+
android:layout_width="1dp"
19+
android:layout_height="1dp"
20+
android:visibility="visible">
21+
22+
</FrameLayout>
23+
24+
<TextView
25+
android:id="@+id/face_detection_score"
26+
android:layout_width="wrap_content"
27+
android:layout_height="wrap_content"
28+
android:layout_alignParentBottom="true"
29+
android:layout_alignParentEnd="true"
30+
android:layout_marginBottom="21dp"
31+
android:layout_marginEnd="30dp"
32+
android:text="TextView"
33+
android:visibility="gone"
34+
tools:layout_editor_absoluteX="758dp"
35+
tools:layout_editor_absoluteY="1123dp" />
36+
37+
</RelativeLayout>
38+
39+

0 commit comments

Comments
 (0)