Fix QR scanning issues.
This commit is contained in:
parent
019025ab8a
commit
eefd7bd37a
5 changed files with 56 additions and 2 deletions
|
@ -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
|
||||
}
|
|
@ -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)
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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>
|
|
@ -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"))
|
||||
|
||||
|
@ -65,5 +67,7 @@ class QrProcessor {
|
|||
|
||||
companion object {
|
||||
private val TAG = Log.tag(QrProcessor::class.java)
|
||||
|
||||
var listener: ((LuminanceSource) -> Unit)? = null
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue