Last active
May 16, 2020 09:24
-
-
Save kongkip/649c038fdad42d2b7fe08a5292ed97b4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ClassicficationActivity : AppCompatActivity() { | |
private val REQUEST_CAMERA_CODE = 13 | |
private val PERMISSIONS = arrayOf(Manifest.permission.CAMERA) | |
private val executor = Executors.newSingleThreadExecutor() | |
private var textResult1: TextView? = null | |
private var textResult2: TextView? = null | |
private var textResult3: TextView? = null | |
private lateinit var viewFinder: TextureView | |
private var imageClassifier:ImageClassifier? = null | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
viewFinder = findViewById(R.id.view_finder) | |
imageClassifier = ImageClassifier(this) | |
if (allPermissionsGranted()) { | |
viewFinder.post { startCamera() } | |
}else { | |
ActivityCompat.requestPermissions( | |
this, PERMISSIONS, REQUEST_CAMERA_CODE | |
) | |
} | |
// Process result from permission request dialog and | |
// if request has been granted start camera | |
// else show permission not granted | |
override fun onRequestPermissionsResult( | |
requestCode: Int, | |
permissions: Array<out String>, | |
grantResults: IntArray | |
) { | |
if (requestCode == REQUEST_CAMERA_CODE && grantResults.isNotEmpty() | |
&& grantResults[0] == PackageManager.PERMISSION_GRANTED | |
) { | |
if (allPermissionsGranted()) { | |
viewFinder.post { startCamera() } | |
} else { | |
Toast.makeText(this, "Permissions not granted", Toast.LENGTH_LONG) | |
.show() | |
finish() | |
} | |
} | |
} | |
// check if camera permission specified in the manifest has been granted | |
private fun allPermissionsGranted() = PERMISSIONS.all { | |
ContextCompat.checkSelfPermission( | |
baseContext, it | |
) == PackageManager.PERMISSION_GRANTED | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment