taler-android

Android apps for GNU Taler (wallet, PoS, cashier)
Log | Files | Refs | README | LICENSE

qr-config-tab-crashes-the-terminal.diff (7115B)


      1 diff --git a/merchant-terminal/src/main/java/net/taler/merchantpos/config/ConfigFragment.kt b/merchant-terminal/src/main/java/net/taler/merchantpos/config/ConfigFragment.kt
      2 index 42247056..0bc3fdb5 100644
      3 --- a/merchant-terminal/src/main/java/net/taler/merchantpos/config/ConfigFragment.kt
      4 +++ b/merchant-terminal/src/main/java/net/taler/merchantpos/config/ConfigFragment.kt
      5 @@ -393,25 +393,30 @@ class ConfigFragment : Fragment() {
      6                      useCase.setAnalyzer(cameraExecutor) { proxy ->
      7                          val mediaImage = proxy.image
      8                          if (mediaImage != null) {
      9 -                            val nv21 = yuv420888ToNv21(mediaImage)
     10 -                            val width = mediaImage.width
     11 -                            val height = mediaImage.height
     12 -                            val source = PlanarYUVLuminanceSource(
     13 -                                nv21,
     14 -                                width,
     15 -                                height,
     16 -                                0,
     17 -                                0,
     18 -                                width,
     19 -                                height,
     20 -                                false,
     21 -                            )
     22 -                            val rotated = when (proxy.imageInfo.rotationDegrees) {
     23 -                                90, 270 -> source.rotateCounterClockwise()
     24 -                                else -> source
     25 -                            }
     26 -                            val bitmap = BinaryBitmap(HybridBinarizer(rotated))
     27                              try {
     28 +                                val degrees = proxy.imageInfo.rotationDegrees
     29 +                                val upright = rotateLuminance(
     30 +                                    luminanceOf(mediaImage),
     31 +                                    mediaImage.width,
     32 +                                    mediaImage.height,
     33 +                                    degrees,
     34 +                                )
     35 +                                val sideways = degrees % 180 != 0
     36 +                                val width =
     37 +                                    if (sideways) mediaImage.height else mediaImage.width
     38 +                                val height =
     39 +                                    if (sideways) mediaImage.width else mediaImage.height
     40 +                                val source = PlanarYUVLuminanceSource(
     41 +                                    upright,
     42 +                                    width,
     43 +                                    height,
     44 +                                    0,
     45 +                                    0,
     46 +                                    width,
     47 +                                    height,
     48 +                                    false,
     49 +                                )
     50 +                                val bitmap = BinaryBitmap(HybridBinarizer(source))
     51                                  val result = qrReader.decodeWithState(bitmap)
     52                                  onQrDecoded(result.text)
     53                              } catch (_: NotFoundException) {
     54 @@ -433,18 +438,94 @@ class ConfigFragment : Fragment() {
     55          }, cameraExecutor)
     56      }
     57  
     58 -    private fun yuv420888ToNv21(image: Image): ByteArray {
     59 -        val yPlane = image.planes[0].buffer
     60 -        val uPlane = image.planes[1].buffer
     61 -        val vPlane = image.planes[2].buffer
     62 -        val ySize = yPlane.remaining()
     63 -        val uSize = uPlane.remaining()
     64 -        val vSize = vPlane.remaining()
     65 -        val nv21 = ByteArray(ySize + uSize + vSize)
     66 -        yPlane.get(nv21, 0, ySize)
     67 -        vPlane.get(nv21, ySize, vSize)
     68 -        uPlane.get(nv21, ySize + vSize, uSize)
     69 -        return nv21
     70 +    /**
     71 +     * The image's luminance plane, densely packed.
     72 +     *
     73 +     * A YUV_420_888 plane is only densely packed when its row stride equals
     74 +     * the image width and its pixel stride is one. Neither is guaranteed:
     75 +     * hardware commonly aligns rows to 16 or 32 bytes. Copying the buffer
     76 +     * wholesale where they differ shears every row after the first, and the
     77 +     * only symptom is that nothing ever decodes.
     78 +     *
     79 +     * Only this plane is read. [PlanarYUVLuminanceSource] looks at luminance
     80 +     * and nothing else, so the chroma planes need not be copied at all.
     81 +     */
     82 +    private fun luminanceOf(image: Image): ByteArray {
     83 +        val plane = image.planes[0]
     84 +        val buffer = plane.buffer
     85 +        val width = image.width
     86 +        val height = image.height
     87 +        val out = ByteArray(width * height)
     88 +        val rowStride = plane.rowStride
     89 +        val pixelStride = plane.pixelStride
     90 +        if (rowStride == width && pixelStride == 1) {
     91 +            buffer.get(out, 0, minOf(out.size, buffer.remaining()))
     92 +            return out
     93 +        }
     94 +        val row = ByteArray(rowStride)
     95 +        var offset = 0
     96 +        for (y in 0 until height) {
     97 +            val length = minOf(rowStride, buffer.remaining())
     98 +            if (length <= 0) break
     99 +            buffer.get(row, 0, length)
    100 +            if (pixelStride == 1) {
    101 +                System.arraycopy(row, 0, out, offset, minOf(width, length))
    102 +            } else {
    103 +                var x = 0
    104 +                var i = 0
    105 +                while (x < width && i < length) {
    106 +                    out[offset + x] = row[i]
    107 +                    x++
    108 +                    i += pixelStride
    109 +                }
    110 +            }
    111 +            offset += width
    112 +        }
    113 +        return out
    114 +    }
    115 +
    116 +    /**
    117 +     * A densely packed luminance plane, rotated clockwise by [degrees].
    118 +     *
    119 +     * The rotation is done on the bytes rather than by asking the
    120 +     * [PlanarYUVLuminanceSource] to rotate itself: that source does not
    121 +     * support rotation, and its inherited [LuminanceSource.rotateCounterClockwise]
    122 +     * throws rather than returning anything -- which crashed the app on the
    123 +     * first frame of every device whose sensor is a quarter turn from the
    124 +     * display, which is to say most of them.
    125 +     */
    126 +    private fun rotateLuminance(
    127 +        data: ByteArray,
    128 +        width: Int,
    129 +        height: Int,
    130 +        degrees: Int,
    131 +    ): ByteArray {
    132 +        val turns = ((degrees % 360) + 360) % 360
    133 +        if (turns == 0) return data
    134 +        val out = ByteArray(width * height)
    135 +        when (turns) {
    136 +            90 -> for (y in 0 until height) {
    137 +                val src = y * width
    138 +                for (x in 0 until width) {
    139 +                    out[x * height + (height - 1 - y)] = data[src + x]
    140 +                }
    141 +            }
    142 +            180 -> for (y in 0 until height) {
    143 +                val src = y * width
    144 +                val dst = (height - 1 - y) * width
    145 +                for (x in 0 until width) {
    146 +                    out[dst + (width - 1 - x)] = data[src + x]
    147 +                }
    148 +            }
    149 +            270 -> for (y in 0 until height) {
    150 +                val src = y * width
    151 +                for (x in 0 until width) {
    152 +                    out[(width - 1 - x) * height + y] = data[src + x]
    153 +                }
    154 +            }
    155 +            else -> return data
    156 +        }
    157 +        return out
    158      }
    159  
    160      private fun onQrDecoded(raw: String) {