0
0
mirror of https://github.com/markusfisch/BinaryEye.git synced 2024-09-20 03:52:16 +02:00

Show latest possible result point in camera view

This commit is contained in:
Markus Fisch 2020-03-30 19:57:57 +02:00
parent 1e82e8bd53
commit 59d40b944b
4 changed files with 43 additions and 24 deletions

View File

@ -17,6 +17,7 @@ import android.widget.SeekBar
import android.widget.Toast
import com.google.zxing.Result
import com.google.zxing.ResultMetadataType
import com.google.zxing.ResultPointCallback
import de.markusfisch.android.binaryeye.R
import de.markusfisch.android.binaryeye.app.db
import de.markusfisch.android.binaryeye.app.hasCameraPermission
@ -33,7 +34,11 @@ import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
class CameraActivity : AppCompatActivity() {
private val zxing = Zxing()
private val zxing = Zxing(ResultPointCallback { point ->
detectorView.post {
mapping?.map(point)?.let { detectorView.mark(listOf(it)) }
}
})
private lateinit var vibrator: Vibrator
private lateinit var cameraView: CameraView
@ -42,6 +47,7 @@ class CameraActivity : AppCompatActivity() {
private lateinit var flashFab: View
private var preprocessor: Preprocessor? = null
private var mapping: Mapping? = null
private var invert = false
private var flash = false
private var returnResult = false
@ -278,7 +284,6 @@ class CameraActivity : AppCompatActivity() {
val frameWidth = cameraView.frameWidth
val frameHeight = cameraView.frameHeight
val frameOrientation = cameraView.frameOrientation
var mapping: Mapping? = null
var decoding = true
camera.setPreviewCallback { frameData, _ ->
if (decoding) {
@ -289,13 +294,6 @@ class CameraActivity : AppCompatActivity() {
frameOrientation
)
result?.let {
preprocessor?.let {
mapping = mapping ?: frameToView(
it.outWidth,
it.outHeight,
cameraView.previewRect
)
}
cameraView.post {
val rp = result.resultPoints
val m = mapping
@ -403,8 +401,7 @@ class CameraActivity : AppCompatActivity() {
): Result? {
frameData ?: return null
invert = invert xor true
val pp = preprocessor ?: Preprocessor(
this,
val pp = preprocessor ?: createPreprocessorAndMapping(
frameWidth,
frameHeight,
frameOrientation
@ -419,6 +416,25 @@ class CameraActivity : AppCompatActivity() {
)
}
private fun createPreprocessorAndMapping(
frameWidth: Int,
frameHeight: Int,
frameOrientation: Int
): Preprocessor {
val pp = Preprocessor(
this,
frameWidth,
frameHeight,
frameOrientation
)
mapping = frameToView(
pp.outWidth,
pp.outHeight,
cameraView.previewRect
)
return pp
}
companion object {
private const val REQUEST_CAMERA = 1
private const val PICK_FILE_RESULT_CODE = 1

View File

@ -55,12 +55,10 @@ data class Mapping(
val offsetX: Int,
val offsetY: Int
) {
fun map(points: Array<ResultPoint>): List<Point> {
return points.map {
Point(
(it.x * ratioX).roundToInt() + offsetX,
(it.y * ratioY).roundToInt() + offsetY
)
}
}
fun map(point: ResultPoint) = Point(
(point.x * ratioX).roundToInt() + offsetX,
(point.y * ratioY).roundToInt() + offsetY
)
fun map(points: Array<ResultPoint>): List<Point> = points.map { map(it) }
}

View File

@ -10,6 +10,10 @@ import de.markusfisch.android.binaryeye.graphics.Candidates
class DetectorView : View {
private val candidates = Candidates(context)
private val invalidateRunnable: Runnable = Runnable {
marks = null
invalidate()
}
private var marks: List<Point>? = null
constructor(context: Context, attrs: AttributeSet) :
@ -21,10 +25,8 @@ class DetectorView : View {
fun mark(points: List<Point>) {
marks = points
invalidate()
postDelayed({
marks = null
invalidate()
}, 500)
removeCallbacks(invalidateRunnable)
postDelayed(invalidateRunnable, 500)
}
override fun onDraw(canvas: Canvas) {

View File

@ -5,7 +5,7 @@ import com.google.zxing.*
import com.google.zxing.common.HybridBinarizer
import java.util.*
class Zxing {
class Zxing(possibleResultPoint: ResultPointCallback? = null) {
private val multiFormatReader = MultiFormatReader()
init {
@ -38,6 +38,9 @@ class Zxing {
val hints = EnumMap<DecodeHintType, Any>(DecodeHintType::class.java)
hints[DecodeHintType.POSSIBLE_FORMATS] = decodeFormats
possibleResultPoint?.let {
hints[DecodeHintType.NEED_RESULT_POINT_CALLBACK] = it
}
multiFormatReader.setHints(hints)
}