Julian Steenbakker
Committed by GitHub

Merge pull request #51 from juliansteenbakker/hot-reload

bug: fixed hot reload not working
@@ -24,7 +24,6 @@ import io.flutter.plugin.common.MethodChannel @@ -24,7 +24,6 @@ import io.flutter.plugin.common.MethodChannel
24 import io.flutter.plugin.common.PluginRegistry 24 import io.flutter.plugin.common.PluginRegistry
25 import io.flutter.view.TextureRegistry 25 import io.flutter.view.TextureRegistry
26 import java.io.File 26 import java.io.File
27 -import java.net.URI  
28 27
29 28
30 class MobileScanner(private val activity: Activity, private val textureRegistry: TextureRegistry) 29 class MobileScanner(private val activity: Activity, private val textureRegistry: TextureRegistry)
@@ -39,6 +38,7 @@ class MobileScanner(private val activity: Activity, private val textureRegistry: @@ -39,6 +38,7 @@ class MobileScanner(private val activity: Activity, private val textureRegistry:
39 38
40 private var cameraProvider: ProcessCameraProvider? = null 39 private var cameraProvider: ProcessCameraProvider? = null
41 private var camera: Camera? = null 40 private var camera: Camera? = null
  41 + private var preview: Preview? = null
42 private var textureEntry: TextureRegistry.SurfaceTextureEntry? = null 42 private var textureEntry: TextureRegistry.SurfaceTextureEntry? = null
43 43
44 // @AnalyzeMode 44 // @AnalyzeMode
@@ -120,11 +120,15 @@ class MobileScanner(private val activity: Activity, private val textureRegistry: @@ -120,11 +120,15 @@ class MobileScanner(private val activity: Activity, private val textureRegistry:
120 120
121 @ExperimentalGetImage 121 @ExperimentalGetImage
122 private fun start(call: MethodCall, result: MethodChannel.Result) { 122 private fun start(call: MethodCall, result: MethodChannel.Result) {
123 - if (camera != null) {  
124 - result.error(TAG, "Called start() while already started!", null)  
125 - return  
126 - }  
127 - 123 + if (camera != null && preview != null) {
  124 + val resolution = preview!!.resolutionInfo!!.resolution
  125 + val portrait = camera!!.cameraInfo.sensorRotationDegrees % 180 == 0
  126 + val width = resolution.width.toDouble()
  127 + val height = resolution.height.toDouble()
  128 + val size = if (portrait) mapOf("width" to width, "height" to height) else mapOf("width" to height, "height" to width)
  129 + val answer = mapOf("textureId" to textureEntry!!.id(), "size" to size, "torchable" to camera!!.cameraInfo.hasFlashUnit())
  130 + result.success(answer)
  131 + } else {
128 val facing: Int = call.argument<Int>("facing") ?: 0 132 val facing: Int = call.argument<Int>("facing") ?: 0
129 val ratio: Int? = call.argument<Int>("ratio") 133 val ratio: Int? = call.argument<Int>("ratio")
130 val torch: Boolean = call.argument<Boolean>("torch") ?: false 134 val torch: Boolean = call.argument<Boolean>("torch") ?: false
@@ -136,9 +140,9 @@ class MobileScanner(private val activity: Activity, private val textureRegistry: @@ -136,9 +140,9 @@ class MobileScanner(private val activity: Activity, private val textureRegistry:
136 formatsList.add(BarcodeFormats.values()[index].intValue) 140 formatsList.add(BarcodeFormats.values()[index].intValue)
137 } 141 }
138 scanner = if (formatsList.size == 1) { 142 scanner = if (formatsList.size == 1) {
139 - BarcodeScanning.getClient( BarcodeScannerOptions.Builder().setBarcodeFormats(formatsList.first()).build()); 143 + BarcodeScanning.getClient(BarcodeScannerOptions.Builder().setBarcodeFormats(formatsList.first()).build())
140 } else { 144 } else {
141 - BarcodeScanning.getClient( BarcodeScannerOptions.Builder().setBarcodeFormats(formatsList.first(), *formatsList.subList(1, formatsList.size).toIntArray() ).build()); 145 + BarcodeScanning.getClient(BarcodeScannerOptions.Builder().setBarcodeFormats(formatsList.first(), *formatsList.subList(1, formatsList.size).toIntArray()).build())
142 } 146 }
143 } 147 }
144 148
@@ -163,7 +167,7 @@ class MobileScanner(private val activity: Activity, private val textureRegistry: @@ -163,7 +167,7 @@ class MobileScanner(private val activity: Activity, private val textureRegistry:
163 if (ratio != null) { 167 if (ratio != null) {
164 previewBuilder.setTargetAspectRatio(ratio) 168 previewBuilder.setTargetAspectRatio(ratio)
165 } 169 }
166 - val preview = previewBuilder.build().apply { setSurfaceProvider(surfaceProvider) } 170 + preview = previewBuilder.build().apply { setSurfaceProvider(surfaceProvider) }
167 171
168 // Build the analyzer to be passed on to MLKit 172 // Build the analyzer to be passed on to MLKit
169 val analysisBuilder = ImageAnalysis.Builder() 173 val analysisBuilder = ImageAnalysis.Builder()
@@ -179,7 +183,7 @@ class MobileScanner(private val activity: Activity, private val textureRegistry: @@ -179,7 +183,7 @@ class MobileScanner(private val activity: Activity, private val textureRegistry:
179 camera = cameraProvider!!.bindToLifecycle(activity as LifecycleOwner, selector, preview, analysis) 183 camera = cameraProvider!!.bindToLifecycle(activity as LifecycleOwner, selector, preview, analysis)
180 184
181 val analysisSize = analysis.resolutionInfo?.resolution ?: Size(0, 0) 185 val analysisSize = analysis.resolutionInfo?.resolution ?: Size(0, 0)
182 - val previewSize = preview.resolutionInfo?.resolution ?: Size(0, 0) 186 + val previewSize = preview!!.resolutionInfo?.resolution ?: Size(0, 0)
183 Log.i("LOG", "Analyzer: $analysisSize") 187 Log.i("LOG", "Analyzer: $analysisSize")
184 Log.i("LOG", "Preview: $previewSize") 188 Log.i("LOG", "Preview: $previewSize")
185 189
@@ -192,7 +196,7 @@ class MobileScanner(private val activity: Activity, private val textureRegistry: @@ -192,7 +196,7 @@ class MobileScanner(private val activity: Activity, private val textureRegistry:
192 // Enable torch if provided 196 // Enable torch if provided
193 camera!!.cameraControl.enableTorch(torch) 197 camera!!.cameraControl.enableTorch(torch)
194 198
195 - val resolution = preview.resolutionInfo!!.resolution 199 + val resolution = preview!!.resolutionInfo!!.resolution
196 val portrait = camera!!.cameraInfo.sensorRotationDegrees % 180 == 0 200 val portrait = camera!!.cameraInfo.sensorRotationDegrees % 180 == 0
197 val width = resolution.width.toDouble() 201 val width = resolution.width.toDouble()
198 val height = resolution.height.toDouble() 202 val height = resolution.height.toDouble()
@@ -201,6 +205,7 @@ class MobileScanner(private val activity: Activity, private val textureRegistry: @@ -201,6 +205,7 @@ class MobileScanner(private val activity: Activity, private val textureRegistry:
201 result.success(answer) 205 result.success(answer)
202 }, executor) 206 }, executor)
203 } 207 }
  208 + }
204 209
205 private fun toggleTorch(call: MethodCall, result: MethodChannel.Result) { 210 private fun toggleTorch(call: MethodCall, result: MethodChannel.Result) {
206 if (camera == null) { 211 if (camera == null) {
@@ -235,18 +240,19 @@ class MobileScanner(private val activity: Activity, private val textureRegistry: @@ -235,18 +240,19 @@ class MobileScanner(private val activity: Activity, private val textureRegistry:
235 } 240 }
236 241
237 private fun stop(result: MethodChannel.Result) { 242 private fun stop(result: MethodChannel.Result) {
238 - if (camera == null) { 243 + if (camera == null && preview == null) {
239 result.error(TAG,"Called stop() while already stopped!", null) 244 result.error(TAG,"Called stop() while already stopped!", null)
240 return 245 return
241 } 246 }
242 247
243 val owner = activity as LifecycleOwner 248 val owner = activity as LifecycleOwner
244 - camera!!.cameraInfo.torchState.removeObservers(owner)  
245 - cameraProvider!!.unbindAll()  
246 - textureEntry!!.release() 249 + camera?.cameraInfo?.torchState?.removeObservers(owner)
  250 + cameraProvider?.unbindAll()
  251 + textureEntry?.release()
247 252
248 // analyzeMode = AnalyzeMode.NONE 253 // analyzeMode = AnalyzeMode.NONE
249 camera = null 254 camera = null
  255 + preview = null
250 textureEntry = null 256 textureEntry = null
251 cameraProvider = null 257 cameraProvider = null
252 258