-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathMainActivity.kt
More file actions
108 lines (87 loc) · 3.2 KB
/
MainActivity.kt
File metadata and controls
108 lines (87 loc) · 3.2 KB
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
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.CameraBridgeViewBase
import org.opencv.android.OpenCVLoader
import org.opencv.core.Mat
class MainActivity : Activity(), CameraBridgeViewBase.CvCameraViewListener2 {
private var mOpenCvCameraView: CameraBridgeViewBase? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
// Request camera permission
ActivityCompat.requestPermissions(
this,
arrayOf(Manifest.permission.CAMERA),
CAMERA_PERMISSION_REQUEST
)
setContentView(R.layout.activity_main)
mOpenCvCameraView = findViewById<CameraBridgeViewBase>(R.id.main_surface)
mOpenCvCameraView?.apply {
visibility = SurfaceView.VISIBLE
setCvCameraViewListener(this@MainActivity)
}
}
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 {
Toast.makeText(this, "Camera permission required", Toast.LENGTH_LONG).show()
}
}
}
}
override fun onResume() {
super.onResume()
// Initialize OpenCV
if (!OpenCVLoader.initLocal()) {
Log.e(TAG, "OpenCV initialization failed")
Toast.makeText(this, "OpenCV initialization failed", Toast.LENGTH_LONG).show()
return
}
// Load native library
try {
System.loadLibrary("native-lib")
} catch (e: UnsatisfiedLinkError) {
Log.e(TAG, "Failed to load native library: ${e.message}")
Toast.makeText(this, "Failed to load native library", Toast.LENGTH_LONG).show()
return
}
mOpenCvCameraView?.enableView()
}
override fun onPause() {
super.onPause()
mOpenCvCameraView?.disableView()
}
override fun onDestroy() {
super.onDestroy()
mOpenCvCameraView?.disableView()
}
override fun onCameraViewStarted(width: Int, height: Int) {}
override fun onCameraViewStopped() {}
override fun onCameraFrame(frame: CameraBridgeViewBase.CvCameraViewFrame): Mat {
// Get current camera frame as grayscale
val mat = frame.gray()
// Process frame with native code
adaptiveThresholdFromJNI(mat.nativeObjAddr)
return mat
}
private external fun adaptiveThresholdFromJNI(matAddr: Long)
companion object {
private const val TAG = "MainActivity"
private const val CAMERA_PERMISSION_REQUEST = 1
}
}