Navaron Bracke

check flash unit before toggling it in start(); safe access camera instead of forced casts

@@ -235,10 +235,12 @@ class MobileScanner( @@ -235,10 +235,12 @@ class MobileScanner(
235 235
236 cameraProviderFuture.addListener({ 236 cameraProviderFuture.addListener({
237 cameraProvider = cameraProviderFuture.get() 237 cameraProvider = cameraProviderFuture.get()
  238 +
238 if (cameraProvider == null) { 239 if (cameraProvider == null) {
239 throw CameraError() 240 throw CameraError()
240 } 241 }
241 - cameraProvider!!.unbindAll() 242 +
  243 + cameraProvider?.unbindAll()
242 textureEntry = textureRegistry.createSurfaceTexture() 244 textureEntry = textureRegistry.createSurfaceTexture()
243 245
244 // Preview 246 // Preview
@@ -297,31 +299,34 @@ class MobileScanner( @@ -297,31 +299,34 @@ class MobileScanner(
297 analysis 299 analysis
298 ) 300 )
299 301
  302 + camera?.let {
300 // Register the torch listener 303 // Register the torch listener
301 - camera!!.cameraInfo.torchState.observe(activity) { state -> 304 + it.cameraInfo.torchState.observe(activity as LifecycleOwner) { state ->
302 // TorchState.OFF = 0; TorchState.ON = 1 305 // TorchState.OFF = 0; TorchState.ON = 1
303 torchStateCallback(state) 306 torchStateCallback(state)
304 } 307 }
305 308
306 // Register the zoom scale listener 309 // Register the zoom scale listener
307 - camera!!.cameraInfo.zoomState.observe(activity) { state -> 310 + it.cameraInfo.zoomState.observe(activity) { state ->
308 zoomScaleStateCallback(state.linearZoom.toDouble()) 311 zoomScaleStateCallback(state.linearZoom.toDouble())
309 } 312 }
310 313
311 -  
312 // Enable torch if provided 314 // Enable torch if provided
313 - camera!!.cameraControl.enableTorch(torch) 315 + if (it.cameraInfo.hasFlashUnit()) {
  316 + it.cameraControl.enableTorch(torch)
  317 + }
  318 + }
314 319
315 val resolution = analysis.resolutionInfo!!.resolution 320 val resolution = analysis.resolutionInfo!!.resolution
316 - val portrait = camera!!.cameraInfo.sensorRotationDegrees % 180 == 0  
317 val width = resolution.width.toDouble() 321 val width = resolution.width.toDouble()
318 val height = resolution.height.toDouble() 322 val height = resolution.height.toDouble()
  323 + val portrait = (camera?.cameraInfo?.sensorRotationDegrees ?: 0) % 180 == 0
319 324
320 mobileScannerStartedCallback( 325 mobileScannerStartedCallback(
321 MobileScannerStartParameters( 326 MobileScannerStartParameters(
322 if (portrait) width else height, 327 if (portrait) width else height,
323 if (portrait) height else width, 328 if (portrait) height else width,
324 - camera!!.cameraInfo.hasFlashUnit(), 329 + camera?.cameraInfo?.hasFlashUnit() ?: false,
325 textureEntry!!.id() 330 textureEntry!!.id()
326 ) 331 )
327 ) 332 )
@@ -363,7 +368,10 @@ class MobileScanner( @@ -363,7 +368,10 @@ class MobileScanner(
363 if (camera == null) { 368 if (camera == null) {
364 throw TorchWhenStopped() 369 throw TorchWhenStopped()
365 } 370 }
366 - camera!!.cameraControl.enableTorch(enableTorch) 371 +
  372 + if (camera?.cameraInfo?.hasFlashUnit() == true) {
  373 + camera?.cameraControl?.enableTorch(enableTorch)
  374 + }
367 } 375 }
368 376
369 /** 377 /**
@@ -393,9 +401,9 @@ class MobileScanner( @@ -393,9 +401,9 @@ class MobileScanner(
393 * Set the zoom rate of the camera. 401 * Set the zoom rate of the camera.
394 */ 402 */
395 fun setScale(scale: Double) { 403 fun setScale(scale: Double) {
396 - if (camera == null) throw ZoomWhenStopped()  
397 if (scale > 1.0 || scale < 0) throw ZoomNotInRange() 404 if (scale > 1.0 || scale < 0) throw ZoomNotInRange()
398 - camera!!.cameraControl.setLinearZoom(scale.toFloat()) 405 + if (camera == null) throw ZoomWhenStopped()
  406 + camera?.cameraControl?.setLinearZoom(scale.toFloat())
399 } 407 }
400 408
401 /** 409 /**
@@ -403,7 +411,7 @@ class MobileScanner( @@ -403,7 +411,7 @@ class MobileScanner(
403 */ 411 */
404 fun resetScale() { 412 fun resetScale() {
405 if (camera == null) throw ZoomWhenStopped() 413 if (camera == null) throw ZoomWhenStopped()
406 - camera!!.cameraControl.setZoomRatio(1f) 414 + camera?.cameraControl?.setZoomRatio(1f)
407 } 415 }
408 416
409 } 417 }