-
Notifications
You must be signed in to change notification settings - Fork 71
/
MainActivity.kt
130 lines (107 loc) 路 5.03 KB
/
MainActivity.kt
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
package com.bio.facerecognition
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.Camera
import android.graphics.Rect
import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.widget.*
import android.util.Log
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.preference.PreferenceManager
import com.bio.facesdk.FaceBox
import com.bio.facesdk.FaceDetectionParam
import com.bio.facesdk.FaceSDK
import androidx.camera.core.CameraSelector
import kotlin.random.Random
import java.util.Calendar
class MainActivity : AppCompatActivity() {
companion object {
private val SELECT_PHOTO_REQUEST_CODE = 1
}
private lateinit var dbManager: DBManager
private lateinit var textWarning: TextView
private lateinit var personAdapter: PersonAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
textWarning = findViewById<TextView>(R.id.textWarning)
val license_str = application.assets.open("license").bufferedReader().use{
it.readText()
}
var ret = FaceSDK.setActivation(license_str)
if (ret == FaceSDK.SDK_SUCCESS) {
ret = FaceSDK.init(assets)
}
if (ret != FaceSDK.SDK_SUCCESS) {
textWarning.setVisibility(View.VISIBLE)
if (ret == FaceSDK.SDK_LICENSE_KEY_ERROR) {
textWarning.setText("License key error!")
} else if (ret == FaceSDK.SDK_LICENSE_APPID_ERROR) {
textWarning.setText("App ID error!")
} else if (ret == FaceSDK.SDK_LICENSE_EXPIRED) {
textWarning.setText("License key expired!")
} else if (ret == FaceSDK.SDK_NO_ACTIVATED) {
textWarning.setText("Activation failed!")
} else if (ret == FaceSDK.SDK_INIT_ERROR) {
textWarning.setText("Engine init error!")
}
}
dbManager = DBManager(this)
dbManager.loadPerson()
personAdapter = PersonAdapter(this, DBManager.personList)
val listView: ListView = findViewById<View>(R.id.listPerson) as ListView
listView.setAdapter(personAdapter)
findViewById<Button>(R.id.buttonEnroll).setOnClickListener {
val intent = Intent()
intent.setType("image/*")
intent.setAction(Intent.ACTION_PICK)
startActivityForResult(Intent.createChooser(intent, getString(R.string.choose_photo)), SELECT_PHOTO_REQUEST_CODE)
}
findViewById<Button>(R.id.buttonIdentify).setOnClickListener {
startActivity(Intent(this, CameraActivityKt::class.java))
}
findViewById<ImageButton>(R.id.buttonSetting).setOnClickListener {
startActivity(Intent(this, SettingsActivity::class.java))
}
}
override fun onResume() {
super.onResume()
personAdapter.notifyDataSetChanged()
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (requestCode == SELECT_PHOTO_REQUEST_CODE && resultCode == RESULT_OK) {
try {
var bitmap: Bitmap = Utils.getCorrectlyOrientedImage(this, data?.data!!)
val faceDetectionParam = FaceDetectionParam()
faceDetectionParam.check_liveness = true
faceDetectionParam.check_liveness_level = SettingsActivity.getLivenessModelType(this)
var faceBoxes: List<FaceBox>? = FaceSDK.faceDetection(bitmap, faceDetectionParam)
if(faceBoxes.isNullOrEmpty()) {
Toast.makeText(this, getString(R.string.no_face), Toast.LENGTH_SHORT).show()
} else if (faceBoxes.size > 1) {
Toast.makeText(this, getString(R.string.multiple_face_detected), Toast.LENGTH_SHORT).show()
} else {
val faceImage = Utils.cropFace(bitmap, faceBoxes[0])
val templates = FaceSDK.templateExtraction(bitmap, faceBoxes[0])
val calendar = Calendar.getInstance()
val year = calendar.get(Calendar.YEAR)
val month = calendar.get(Calendar.MONTH) + 1 // Month starts from 0
val dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH)
val hour = calendar.get(Calendar.HOUR_OF_DAY) // 24-hour format
val minute = calendar.get(Calendar.MINUTE)
val second = calendar.get(Calendar.SECOND)
dbManager.insertPerson("User " + year + month + dayOfMonth + hour + minute + second, faceImage, templates)
personAdapter.notifyDataSetChanged()
Toast.makeText(this, getString(R.string.user_registered), Toast.LENGTH_SHORT).show()
}
} catch (e: java.lang.Exception) {
//handle exception
e.printStackTrace()
}
}
}
}