Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
did you say youts? or utes?
class DetectorActivity : AppCompatActivity() {
private var cameraExecutor: ExecutorService = Executors.newSingleThreadExecutor()
private lateinit var cameraView: PreviewView
private val imageCapture = ImageCapture.Builder().build()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_detector)
cameraView = findViewById(R.id.CameraView)
val captureButton = findViewById<ImageView>(R.id.CameraButton)
startCamera()
captureButton.setOnClickListener {
val imageFile = File(externalMediaDirs.first(), "${System.currentTimeMillis()}.jpg")
val outputFileOptions = ImageCapture.OutputFileOptions.Builder(imageFile).build()
imageCapture.takePicture(outputFileOptions, cameraExecutor,
object : ImageCapture.OnImageSavedCallback {
override fun onImageSaved(outputFileResults: ImageCapture.OutputFileResults) {
// Image saved successfully
Log.d("DetectorActivity", "$imageFile saved!")
}
override fun onError(exception: ImageCaptureException) {
Log.e("DetectorActivity", "Image capture error: ${exception.message}", exception)
}
})
}
}
private fun startCamera() {
val cameraProviderFuture = ProcessCameraProvider.getInstance(this)
cameraProviderFuture.addListener({
val cameraProvider = cameraProviderFuture.get()
val preview = Preview.Builder()
.build()
.also {
it.setSurfaceProvider(cameraView.surfaceProvider)
}
val imageCapture = ImageCapture.Builder()
.setCaptureMode(ImageCapture.CAPTURE_MODE_MINIMIZE_LATENCY)
.build()
val cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA
try {
cameraProvider.unbindAll()
cameraProvider.bindToLifecycle(this, cameraSelector, preview, imageCapture)
} catch (exc: Exception) {
// Handle exception
}
}, ContextCompat.getMainExecutor(this))
}
override fun onDestroy() {
super.onDestroy()
cameraExecutor.shutdown()
}
}