|
| 1 | +/* |
| 2 | + * Copyright © 2017-2022 WireGuard LLC. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package com.wireguard.android.util |
| 7 | + |
| 8 | +import android.content.ContentResolver |
| 9 | +import android.graphics.Bitmap |
| 10 | +import android.graphics.BitmapFactory |
| 11 | +import android.net.Uri |
| 12 | +import android.util.Log |
| 13 | +import com.google.zxing.BinaryBitmap |
| 14 | +import com.google.zxing.DecodeHintType |
| 15 | +import com.google.zxing.NotFoundException |
| 16 | +import com.google.zxing.RGBLuminanceSource |
| 17 | +import com.google.zxing.Reader |
| 18 | +import com.google.zxing.Result |
| 19 | +import com.google.zxing.common.HybridBinarizer |
| 20 | +import kotlinx.coroutines.Dispatchers |
| 21 | +import kotlinx.coroutines.withContext |
| 22 | + |
| 23 | +/** |
| 24 | + * Encapsulates the logic of scanning a barcode from a file, |
| 25 | + * @property contentResolver - Resolver to read the incoming data |
| 26 | + * @property reader - An instance of zxing's [Reader] class to parse the image |
| 27 | + */ |
| 28 | +class QrCodeFromFileScanner( |
| 29 | + private val contentResolver: ContentResolver, |
| 30 | + private val reader: Reader, |
| 31 | +) { |
| 32 | + |
| 33 | + private fun scanBitmapForResult(source: Bitmap): Result { |
| 34 | + val width = source.width |
| 35 | + val height = source.height |
| 36 | + val pixels = IntArray(width * height) |
| 37 | + source.getPixels(pixels, 0, width, 0, 0, width, height) |
| 38 | + |
| 39 | + val bBitmap = BinaryBitmap(HybridBinarizer(RGBLuminanceSource(width, height, pixels))) |
| 40 | + return reader.decode(bBitmap, mapOf(DecodeHintType.TRY_HARDER to true)) |
| 41 | + } |
| 42 | + |
| 43 | + private fun downscaleBitmap(source: Bitmap, scaledSize: Int): Bitmap { |
| 44 | + |
| 45 | + val originalWidth = source.width |
| 46 | + val originalHeight = source.height |
| 47 | + |
| 48 | + var newWidth = -1 |
| 49 | + var newHeight = -1 |
| 50 | + val multFactor: Float |
| 51 | + |
| 52 | + when { |
| 53 | + originalHeight > originalWidth -> { |
| 54 | + newHeight = scaledSize |
| 55 | + multFactor = originalWidth.toFloat() / originalHeight.toFloat() |
| 56 | + newWidth = (newHeight * multFactor).toInt() |
| 57 | + } |
| 58 | + originalWidth > originalHeight -> { |
| 59 | + newWidth = scaledSize |
| 60 | + multFactor = originalHeight.toFloat() / originalWidth.toFloat() |
| 61 | + newHeight = (newWidth * multFactor).toInt() |
| 62 | + } |
| 63 | + originalHeight == originalWidth -> { |
| 64 | + newHeight = scaledSize |
| 65 | + newWidth = scaledSize |
| 66 | + } |
| 67 | + } |
| 68 | + return Bitmap.createScaledBitmap(source, newWidth, newHeight, false) |
| 69 | + } |
| 70 | + |
| 71 | + private fun doScan(data: Uri): Result { |
| 72 | + Log.d(TAG, "Starting to scan an image: $data") |
| 73 | + contentResolver.openInputStream(data).use { inputStream -> |
| 74 | + val originalBitmap = BitmapFactory.decodeStream(inputStream) |
| 75 | + ?: throw IllegalArgumentException("Can't decode stream to Bitmap") |
| 76 | + |
| 77 | + return try { |
| 78 | + scanBitmapForResult(originalBitmap).also { |
| 79 | + Log.d(TAG, "Found result in original image") |
| 80 | + } |
| 81 | + } catch (e: Exception) { |
| 82 | + Log.e(TAG, "Original image scan finished with error: $e, will try downscaled image") |
| 83 | + val scaleBitmap = downscaleBitmap(originalBitmap, 500) |
| 84 | + scanBitmapForResult(originalBitmap).also { scaleBitmap.recycle() } |
| 85 | + } finally { |
| 86 | + originalBitmap.recycle() |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Attempts to parse incoming data |
| 94 | + * @return result of the decoding operation |
| 95 | + * @throws NotFoundException when parser didn't find QR code in the image |
| 96 | + */ |
| 97 | + suspend fun scan(data: Uri) = withContext(Dispatchers.Default) { doScan(data) } |
| 98 | + |
| 99 | + /** |
| 100 | + * Given a reference to a file, check if this file could be parsed by this class |
| 101 | + * @return true if the file can be parsed, false if not |
| 102 | + */ |
| 103 | + fun validContentType(data: Uri): Boolean { |
| 104 | + return contentResolver.getType(data)?.startsWith("image/") == true |
| 105 | + } |
| 106 | + |
| 107 | + companion object { |
| 108 | + private const val TAG = "QrCodeFromFileScanner" |
| 109 | + } |
| 110 | +} |
0 commit comments