-
Notifications
You must be signed in to change notification settings - Fork 69
/
MainActivity.kt
127 lines (103 loc) · 4.04 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
package com.example.nativeopencvandroidtemplate
import android.Manifest
import android.app.Activity
import android.content.pm.PackageManager
import android.os.Bundle
import androidx.core.app.ActivityCompat
import android.util.Log
import android.view.SurfaceView
import android.view.WindowManager
import android.widget.Toast
import org.opencv.android.BaseLoaderCallback
import org.opencv.android.CameraBridgeViewBase
import org.opencv.android.LoaderCallbackInterface
import org.opencv.android.OpenCVLoader
import org.opencv.core.Mat
class MainActivity : Activity(), CameraBridgeViewBase.CvCameraViewListener2 {
private var mOpenCvCameraView: CameraBridgeViewBase? = null
private val mLoaderCallback = object : BaseLoaderCallback(this) {
override fun onManagerConnected(status: Int) {
when (status) {
LoaderCallbackInterface.SUCCESS -> {
Log.i(TAG, "OpenCV loaded successfully")
// Load native library after(!) OpenCV initialization
System.loadLibrary("native-lib")
mOpenCvCameraView!!.enableView()
}
else -> {
super.onManagerConnected(status)
}
}
}
}
override fun onCreate(savedInstanceState: Bundle?) {
Log.i(TAG, "called onCreate")
super.onCreate(savedInstanceState)
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
// Permissions for Android 6+
ActivityCompat.requestPermissions(
this@MainActivity,
arrayOf(Manifest.permission.CAMERA),
CAMERA_PERMISSION_REQUEST
)
setContentView(R.layout.activity_main)
mOpenCvCameraView = findViewById<CameraBridgeViewBase>(R.id.main_surface)
mOpenCvCameraView!!.visibility = SurfaceView.VISIBLE
mOpenCvCameraView!!.setCvCameraViewListener(this)
}
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<String>,
grantResults: IntArray
) {
when (requestCode) {
CAMERA_PERMISSION_REQUEST -> {
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
mOpenCvCameraView!!.setCameraPermissionGranted()
} else {
val message = "Camera permission was not granted"
Log.e(TAG, message)
Toast.makeText(this, message, Toast.LENGTH_LONG).show()
}
}
else -> {
Log.e(TAG, "Unexpected permission request")
}
}
}
override fun onPause() {
super.onPause()
if (mOpenCvCameraView != null)
mOpenCvCameraView!!.disableView()
}
override fun onResume() {
super.onResume()
if (!OpenCVLoader.initDebug()) {
Log.d(TAG, "Internal OpenCV library not found. Using OpenCV Manager for initialization")
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION, this, mLoaderCallback)
} else {
Log.d(TAG, "OpenCV library found inside package. Using it!")
mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS)
}
}
override fun onDestroy() {
super.onDestroy()
if (mOpenCvCameraView != null)
mOpenCvCameraView!!.disableView()
}
override fun onCameraViewStarted(width: Int, height: Int) {}
override fun onCameraViewStopped() {}
override fun onCameraFrame(frame: CameraBridgeViewBase.CvCameraViewFrame): Mat {
// get current camera frame as OpenCV Mat object
val mat = frame.gray()
// native call to process current camera frame
adaptiveThresholdFromJNI(mat.nativeObjAddr)
// return processed frame for live preview
return mat
}
private external fun adaptiveThresholdFromJNI(matAddr: Long)
companion object {
private const val TAG = "MainActivity"
private const val CAMERA_PERMISSION_REQUEST = 1
}
}