This file contains 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
private var camera: CameraDevice? = null | |
private var imgReader: ImageReader? = null | |
private var session: CameraCaptureSession? = null | |
private var imgBkgThread: HandlerThread? = null | |
private var cameraBkgThread: HandlerThread? = null | |
private var sessionBkgThread: HandlerThread? = null | |
override fun onStop() { | |
super.onStop() |
This file contains 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
GlobalScope.launch(CoroutineExceptionHandler { _, err -> | |
when (err) { | |
is SecurityException -> err.let { | |
// TODO: - Camera permission required | |
Log.d(TAG, "SecurityException") | |
} | |
is CameraAccessException -> err.let { | |
Log.d(TAG, "CameraAccessException") | |
} | |
is CameraDeviceClosedException -> err.let { |
This file contains 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
fun ImageReader.getAvailableImageChannel(handler: Handler): Channel<ImageReader> { | |
val channel = Channel<ImageReader>() | |
val callback = ImageReader.OnImageAvailableListener { GlobalScope.launch { channel.send(it) } } | |
setOnImageAvailableListener(callback, handler) | |
return channel | |
} |
This file contains 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
GlobalScope.launch(CoroutineExceptionHandler { _, err -> | |
when (err) { | |
is SecurityException -> err.let { | |
// TODO: - Camera permission required | |
Log.d(TAG, "SecurityException") | |
} | |
is CameraAccessException -> err.let { | |
Log.d(TAG, "CameraAccessException") | |
} | |
is CameraDeviceClosedException -> err.let { |
This file contains 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 CameraCaptureSessionClosedException(val session: CameraCaptureSession) : Exception() | |
class CameraCaptureSessionConfigureFailedException(val session: CameraCaptureSession) : Exception() | |
fun CameraDevice.createCaptureSessionChannel(outputs: List<Surface>, handler: Handler): Channel<CameraCaptureSession> { | |
val channel = Channel<CameraCaptureSession>() | |
val callback = object : CameraCaptureSession.StateCallback() { | |
override fun onConfigured(session: CameraCaptureSession) { | |
GlobalScope.launch { channel.send(session) } | |
} |
This file contains 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
GlobalScope.launch(CoroutineExceptionHandler { _, err -> | |
when (err) { | |
is SecurityException -> err.let { | |
// TODO: - Camera permission required | |
Log.d(TAG, "SecurityException") | |
} | |
is CameraAccessException -> err.let { | |
Log.d(TAG, "CameraAccessException") | |
} | |
is CameraDeviceClosedException -> err.let { |
This file contains 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 CameraDeviceClosedException(val camera: CameraDevice) : Exception() | |
class CameraDeviceDisconnectedException(val camera: CameraDevice) : Exception() | |
class CameraDeviceErrorException(val camera: CameraDevice, val error: Int) : Exception() | |
@SuppressLint("MissingPermission") | |
fun CameraManager.openCameraChannel(cameraID: String, handler: Handler): Channel<CameraDevice> { | |
val channel = Channel<CameraDevice>() | |
val callback = object : CameraDevice.StateCallback() { | |
override fun onOpened(camera: CameraDevice) { |
This file contains 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
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | |
binding.cameraPreview.holder.addCallback(object : SurfaceHolder.Callback { | |
override fun surfaceCreated(holder: SurfaceHolder?) {} | |
override fun surfaceDestroyed(holder: SurfaceHolder?) {} | |
override fun surfaceChanged(holder: SurfaceHolder?, format: Int, width: Int, height: Int) { | |
startCameraPreview(width, height) | |
} | |
}) | |
} |
This file contains 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
override fun onOpened(camera: CameraDevice) { | |
val barcodeDetector = BarcodeDetector.Builder(context!!) | |
.setBarcodeFormats(Barcode.QR_CODE or Barcode.DATA_MATRIX) | |
.build() | |
if (!barcodeDetector.isOperational) { | |
// TODO | |
} | |
val imgReader = ImageReader.newInstance(desiredWidth, desiredHeight, ImageFormat.JPEG, 1) |
This file contains 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
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | |
binding.cameraPreview.holder.addCallback(object : SurfaceHolder.Callback { | |
override fun surfaceCreated(holder: SurfaceHolder?) {} | |
override fun surfaceDestroyed(holder: SurfaceHolder?) {} | |
override fun surfaceChanged(holder: SurfaceHolder?, format: Int, width: Int, height: Int) { | |
startCameraPreview() | |
} | |
}) | |
} |
NewerOlder