Skip to content
This repository was archived by the owner on Apr 18, 2024. It is now read-only.

Commit 3ddcff2

Browse files
author
Mateusz Maziec
committed
Merge branch 'feature/code_clean_up' into develop
2 parents beae4db + d6d125d commit 3ddcff2

File tree

5 files changed

+86
-43
lines changed

5 files changed

+86
-43
lines changed

app/src/main/java/com/bobekos/bobek/simplebarcodescanner/MainActivity.kt

+15-2
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,22 @@ import android.support.v7.app.AppCompatActivity
55
import android.widget.Toast
66
import com.google.android.gms.vision.barcode.Barcode
77
import io.reactivex.android.schedulers.AndroidSchedulers
8+
import io.reactivex.disposables.Disposable
89
import kotlinx.android.synthetic.main.activity_main.*
910

1011
class MainActivity : AppCompatActivity() {
1112

13+
private var disposable: Disposable? = null
14+
1215
override fun onCreate(savedInstanceState: Bundle?) {
1316
super.onCreate(savedInstanceState)
1417
setContentView(R.layout.activity_main)
18+
}
19+
20+
override fun onStart() {
21+
super.onStart()
1522

16-
barcodeView
23+
disposable = barcodeView
1724
.setBarcodeFormats(Barcode.QR_CODE)
1825
.drawOverlay()
1926
.getObservable()
@@ -26,4 +33,10 @@ class MainActivity : AppCompatActivity() {
2633
Toast.makeText(this@MainActivity, it.message, Toast.LENGTH_LONG).show()
2734
})
2835
}
29-
}
36+
37+
override fun onStop() {
38+
super.onStop()
39+
40+
disposable?.dispose()
41+
}
42+
}

build.gradle

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
// Top-level build file where you can add configuration options common to all sub-projects/modules.
22

33
buildscript {
4-
ext.kotlin_version = '1.2.30'
4+
ext.kotlin_version = '1.2.31'
55
repositories {
66
google()
77
jcenter()
88
mavenCentral()
99
}
1010
dependencies {
11-
classpath 'com.android.tools.build:gradle:3.2.0-alpha06'
11+
classpath 'com.android.tools.build:gradle:3.2.0-alpha07'
1212
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
1313
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.0'
1414

scanner/src/main/java/com/bobekos/bobek/scanner/BarcodeRectOverlay.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class BarcodeRectOverlay : View, BarcodeOverlay {
2424

2525
private val paint by lazy {
2626
Paint().apply {
27-
color = Color.RED
27+
color = Color.WHITE
2828
style = Paint.Style.STROKE
2929
strokeWidth = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 3f, context?.resources?.displayMetrics)
3030
}

scanner/src/main/java/com/bobekos/bobek/scanner/BarcodeScanner.kt

+22-11
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import com.google.android.gms.vision.Tracker
1414
import com.google.android.gms.vision.barcode.Barcode
1515
import com.google.android.gms.vision.barcode.BarcodeDetector
1616
import io.reactivex.Observable
17-
import org.reactivestreams.Subscriber
17+
import io.reactivex.ObservableEmitter
1818

1919

2020
internal class BarcodeScanner(
@@ -28,35 +28,46 @@ internal class BarcodeScanner(
2828
.build()
2929
}
3030

31+
private val cameraSource by lazy {
32+
getCameraSource(config.previewSize, config.isAutoFocus)
33+
}
34+
3135
@SuppressLint("MissingPermission")
3236
fun getObservable(): Observable<Barcode> {
33-
return Observable.fromPublisher<Barcode> {
34-
if (context == null) {
35-
it.onError(NullPointerException("Context is null"))
37+
return Observable.create { emitter ->
38+
if (context == null && !emitter.isDisposed) {
39+
emitter.onError(NullPointerException("Context is null"))
3640
} else {
3741
if (checkPermission()) {
38-
getCameraSource(config.previewSize, config.isAutoFocus).start(holder)
42+
cameraSource.start(holder)
3943

40-
val tracker = BarcodeTracker(it)
44+
val tracker = BarcodeTracker(emitter)
4145
val processor = MultiProcessor.Builder(BarcodeTrackerFactory(tracker)).build()
42-
4346
barcodeDetector.setProcessor(processor)
4447
} else {
45-
it.onError(SecurityException("Permission Denial: Camera"))
48+
if (!emitter.isDisposed) {
49+
emitter.onError(SecurityException("Permission Denial: Camera"))
50+
}
51+
}
52+
53+
emitter.setCancellable {
54+
cameraSource.release()
4655
}
4756
}
4857
}
4958
}
5059

51-
inner class BarcodeTracker(private val subscriber: Subscriber<in Barcode>) : Tracker<Barcode>() {
60+
inner class BarcodeTracker(private val subscriber: ObservableEmitter<Barcode>) : Tracker<Barcode>() {
5261

5362
override fun onNewItem(id: Int, barcode: Barcode?) {
5463
if (barcode != null) {
55-
subscriber.onNext(barcode)
56-
5764
if (config.drawOverLay) {
5865
BarcodeView.overlaySubject.onNext(barcode.boundingBox)
5966
}
67+
68+
if (!subscriber.isDisposed) {
69+
subscriber.onNext(barcode)
70+
}
6071
}
6172
}
6273

scanner/src/main/java/com/bobekos/bobek/scanner/BarcodeView.kt

+46-27
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import android.view.View
99
import android.widget.FrameLayout
1010
import com.google.android.gms.vision.barcode.Barcode
1111
import io.reactivex.Observable
12+
import io.reactivex.Single
1213
import io.reactivex.android.schedulers.AndroidSchedulers
1314
import io.reactivex.disposables.Disposable
1415
import io.reactivex.schedulers.Schedulers
@@ -57,29 +58,11 @@ class BarcodeView : FrameLayout {
5758
addView(cameraView)
5859
}
5960

61+
//region public
6062
fun getObservable(): Observable<Barcode> {
61-
return Observable.fromPublisher<SurfaceHolder> {
62-
cameraView.holder.addCallback(object : SurfaceHolder.Callback {
63-
override fun surfaceChanged(p0: SurfaceHolder?, p1: Int, p2: Int, p3: Int) {
64-
65-
}
66-
67-
override fun surfaceDestroyed(p0: SurfaceHolder?) {
68-
overlayDisposable?.dispose()
69-
it.onComplete()
70-
}
71-
72-
override fun surfaceCreated(holder: SurfaceHolder?) {
73-
if (drawOverlay != null) {
74-
startOverlay()
75-
}
76-
77-
it.onNext(holder)
78-
}
79-
})
80-
}.flatMap {
81-
BarcodeScanner(context, it, config).getObservable()
82-
}.subscribeOn(Schedulers.io())
63+
return getSurfaceObservable()
64+
.flatMap { BarcodeScanner(context, it, config).getObservable() }
65+
.subscribeOn(Schedulers.io())
8366
}
8467

8568
fun setPreviewSize(width: Int, height: Int): BarcodeView {
@@ -106,17 +89,52 @@ class BarcodeView : FrameLayout {
10689

10790
return this
10891
}
92+
//endregion
93+
94+
//region private
95+
private fun getSurfaceObservable(): Observable<SurfaceHolder> {
96+
return Observable.create<SurfaceHolder> { emitter ->
97+
cameraView.holder.addCallback(object : SurfaceHolder.Callback {
98+
override fun surfaceChanged(p0: SurfaceHolder?, p1: Int, p2: Int, p3: Int) {
99+
100+
}
101+
102+
override fun surfaceDestroyed(p0: SurfaceHolder?) {
103+
overlayDisposable?.dispose()
104+
105+
if (!emitter.isDisposed) {
106+
emitter.onComplete()
107+
}
108+
}
109+
110+
override fun surfaceCreated(holder: SurfaceHolder?) {
111+
if (drawOverlay != null) {
112+
startOverlay()
113+
}
114+
115+
if (holder != null && !emitter.isDisposed) {
116+
emitter.onNext(holder)
117+
}
118+
}
119+
})
120+
}
121+
}
122+
109123

110124
private fun startOverlay() {
125+
removeView(drawOverlay as View)
111126
addView(drawOverlay as View, FrameLayout.LayoutParams(width, height))
112127

113128
overlayDisposable = overlaySubject
114129
.observeOn(AndroidSchedulers.mainThread())
115-
.subscribe {
116-
if (drawOverlay != null) {
117-
drawOverlay?.onUpdate(calculateOverlayView(it))
118-
}
119-
}
130+
.filter { drawOverlay != null }
131+
.subscribe(
132+
{
133+
drawOverlay?.onUpdate(calculateOverlayView(it))
134+
},
135+
{
136+
drawOverlay?.onUpdate(Rect())
137+
})
120138
}
121139

122140
private fun calculateOverlayView(barcodeRect: Rect): Rect {
@@ -137,4 +155,5 @@ class BarcodeView : FrameLayout {
137155
private fun translateY(y: Int): Int {
138156
return (y * yScaleFactor).toInt()
139157
}
158+
//endregion
140159
}

0 commit comments

Comments
 (0)