@@ -4,54 +4,96 @@ import android.Manifest
4
4
import android.annotation.SuppressLint
5
5
import android.content.Context
6
6
import android.content.pm.PackageManager
7
+ import android.graphics.Rect
7
8
import android.support.v4.app.ActivityCompat
8
9
import android.view.SurfaceHolder
9
- import com.bobekos.bobek.scanner.BarcodeTrackerFactory
10
10
import com.google.android.gms.vision.CameraSource
11
+ import com.google.android.gms.vision.Detector
11
12
import com.google.android.gms.vision.MultiProcessor
13
+ import com.google.android.gms.vision.Tracker
12
14
import com.google.android.gms.vision.barcode.Barcode
13
15
import com.google.android.gms.vision.barcode.BarcodeDetector
14
16
import io.reactivex.Observable
17
+ import io.reactivex.ObservableEmitter
15
18
16
19
17
- class BarcodeScanner (private val context : Context ? , private val holder : SurfaceHolder ) {
20
+ internal class BarcodeScanner (
21
+ private val context : Context ? ,
22
+ private val holder : SurfaceHolder ,
23
+ private val config : BarcodeScannerConfig ) {
18
24
19
25
private val barcodeDetector by lazy {
20
- BarcodeDetector .Builder (context).build()
26
+ BarcodeDetector .Builder (context)
27
+ .setBarcodeFormats(config.barcodeFormat)
28
+ .build()
29
+ }
30
+
31
+ private val cameraSource by lazy {
32
+ getCameraSource(config.previewSize, config.isAutoFocus)
21
33
}
22
34
23
35
@SuppressLint(" MissingPermission" )
24
36
fun getObservable (): Observable <Barcode > {
25
- return Observable .create<Barcode > { emitter ->
26
-
27
- if (context == null ) {
37
+ return Observable .create { emitter ->
38
+ if (context == null && ! emitter.isDisposed) {
28
39
emitter.onError(NullPointerException (" Context is null" ))
29
40
} else {
30
41
if (checkPermission()) {
31
- getCameraSource().start(holder)
42
+ cameraSource.start(holder)
43
+
44
+ val tracker = BarcodeTracker (emitter)
45
+ val processor = MultiProcessor .Builder (BarcodeTrackerFactory (tracker)).build()
46
+ barcodeDetector.setProcessor(processor)
32
47
} else {
33
- emitter.onError(SecurityException (" Permission Denial: Camera" ))
48
+ if (! emitter.isDisposed) {
49
+ emitter.onError(SecurityException (" Permission Denial: Camera" ))
50
+ }
34
51
}
35
52
36
- val processor = MultiProcessor .Builder (BarcodeTrackerFactory ({
37
- if (holder.surface == null || ! holder.surface.isValid) {
38
- emitter.onComplete()
39
- } else if (it != null ) {
40
- emitter.onNext(it)
41
- }
42
- })).build()
53
+ emitter.setCancellable {
54
+ cameraSource.release()
55
+ }
56
+ }
57
+ }
58
+ }
59
+
60
+ inner class BarcodeTracker (private val subscriber : ObservableEmitter <Barcode >) : Tracker<Barcode>() {
61
+
62
+ override fun onNewItem (id : Int , barcode : Barcode ? ) {
63
+ if (barcode != null ) {
64
+ if (config.drawOverLay) {
65
+ BarcodeView .overlaySubject.onNext(barcode.boundingBox)
66
+ }
67
+
68
+ if (! subscriber.isDisposed) {
69
+ subscriber.onNext(barcode)
70
+ }
71
+ }
72
+ }
73
+
74
+ override fun onUpdate (detection : Detector .Detections <Barcode >? , barcode : Barcode ? ) {
75
+ if (barcode != null && config.drawOverLay) {
76
+ BarcodeView .overlaySubject.onNext(barcode.boundingBox)
77
+ }
78
+ }
79
+
80
+ override fun onMissing (p0 : Detector .Detections <Barcode >? ) {
81
+
82
+ }
43
83
44
- barcodeDetector.setProcessor(processor)
84
+ override fun onDone () {
85
+ if (config.drawOverLay) {
86
+ BarcodeView .overlaySubject.onNext(Rect ())
45
87
}
46
88
}
47
89
}
48
90
49
- private fun getCameraSource (): CameraSource {
91
+ private fun getCameraSource (size : Size , isAutoFocus : Boolean ): CameraSource {
50
92
return CameraSource .Builder (context, barcodeDetector)
51
93
.setFacing(CameraSource .CAMERA_FACING_BACK )
52
- .setRequestedPreviewSize(640 , 480 )
94
+ .setRequestedPreviewSize(size.width, size.height )
53
95
.setRequestedFps(15.0f )
54
- .setAutoFocusEnabled(true )
96
+ .setAutoFocusEnabled(isAutoFocus )
55
97
.build()
56
98
}
57
99
0 commit comments