Skip to content

Commit

Permalink
Fix QR scanning issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
cody-signal authored and alex-signal committed Aug 10, 2022
1 parent 019025a commit eefd7bd
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 2 deletions.
3 changes: 3 additions & 0 deletions qr/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@ dependencies {
implementation libs.rxjava3.rxjava
implementation libs.rxjava3.rxandroid
implementation libs.rxjava3.rxkotlin

implementation libs.google.zxing.android.integration
implementation libs.google.zxing.core
}
34 changes: 33 additions & 1 deletion qr/app/src/main/java/org/signal/qrtest/QrMainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
package org.signal.qrtest

import android.annotation.SuppressLint
import android.graphics.Bitmap
import android.os.Build
import android.os.Bundle
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.AppCompatImageView
import com.google.zxing.PlanarYUVLuminanceSource
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.kotlin.subscribeBy
import org.signal.core.util.ThreadUtil
import org.signal.qr.ImageProxyLuminanceSource
import org.signal.qr.QrProcessor
import org.signal.qr.QrScannerView

class QrMainActivity : AppCompatActivity() {
@SuppressLint("NewApi", "SetTextI18n")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Expand All @@ -20,9 +29,32 @@ class QrMainActivity : AppCompatActivity() {
val scanner = findViewById<QrScannerView>(R.id.scanner)
scanner.start(this)

val qrText = findViewById<TextView>(R.id.text_qr_data)

scanner.qrData
.distinctUntilChanged()
.observeOn(AndroidSchedulers.mainThread())
.subscribeBy { Toast.makeText(this, it, Toast.LENGTH_SHORT).show() }
.subscribeBy {
qrText.text = it
Toast.makeText(this, it, Toast.LENGTH_SHORT).show()
}

val sourceView = findViewById<AppCompatImageView>(R.id.scanner_source)
val qrSize = findViewById<TextView>(R.id.text_size)

QrProcessor.listener = { source ->
val bitmap = when (source) {
is ImageProxyLuminanceSource -> Bitmap.createBitmap(source.render(), 0, source.width, source.width, source.height, Bitmap.Config.ARGB_8888)
is PlanarYUVLuminanceSource -> Bitmap.createBitmap(source.renderThumbnail(), 0, source.thumbnailWidth, source.thumbnailWidth, source.thumbnailHeight, Bitmap.Config.ARGB_8888)
else -> null
}

if (bitmap != null) {
ThreadUtil.runOnMain {
qrSize.text = "${bitmap.width} x ${bitmap.height}"
sourceView.setImageBitmap(bitmap)
}
}
}
}
}
15 changes: 15 additions & 0 deletions qr/app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,26 @@
android:gravity="center_horizontal"
android:orientation="vertical">

<TextView
android:id="@+id/text_qr_data"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

<TextView
android:id="@+id/text_size"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

<org.signal.qr.QrScannerView
android:id="@+id/scanner"
android:layout_width="240dp"
android:layout_height="240dp" />

<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/scanner_source"
android:layout_width="240dp"
android:layout_height="240dp" />

</LinearLayout>

</FrameLayout>
4 changes: 4 additions & 0 deletions qr/lib/src/main/java/org/signal/qr/QrProcessor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ class QrProcessor {
previousHeight = source.height
}

listener?.invoke(source)

val bitmap = BinaryBitmap(HybridBinarizer(source))
val result: Result? = reader.decode(bitmap, mapOf(DecodeHintType.TRY_HARDER to true, DecodeHintType.CHARACTER_SET to "ISO-8859-1"))

Expand All @@ -65,5 +67,7 @@ class QrProcessor {

companion object {
private val TAG = Log.tag(QrProcessor::class.java)

var listener: ((LuminanceSource) -> Unit)? = null
}
}
2 changes: 1 addition & 1 deletion qr/lib/src/main/java/org/signal/qr/ScannerView21.kt
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ internal class ScannerView21 constructor(
val preview = Preview.Builder().build()

val imageAnalysis = ImageAnalysis.Builder()
.setTargetResolution(Size(1920, 1080))
.setTargetResolution(Size(1280, 960))
.setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST)
.build()

Expand Down

0 comments on commit eefd7bd

Please sign in to comment.