Skip to content

Commit a6a181f

Browse files
authored
Merge pull request #622 from okarmazin/android-api23-fix
Correctly handle supported image formats on Android APIs < 23
2 parents 2d314dc + 5dd9c8a commit a6a181f

File tree

2 files changed

+28
-17
lines changed

2 files changed

+28
-17
lines changed

wrappers/android/zxingcpp/build.gradle.kts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ android {
4444
}
4545
}
4646

47+
kotlin {
48+
explicitApi()
49+
}
50+
4751
dependencies {
4852
implementation(libs.androidx.camera.core)
4953
}

wrappers/android/zxingcpp/src/main/java/com/zxingcpp/BarcodeReader.kt

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,39 +20,51 @@ import android.graphics.Bitmap
2020
import android.graphics.ImageFormat
2121
import android.graphics.Point
2222
import android.graphics.Rect
23+
import android.os.Build
2324
import androidx.camera.core.ImageProxy
2425
import java.lang.RuntimeException
2526
import java.nio.ByteBuffer
2627

27-
class BarcodeReader {
28+
public class BarcodeReader {
29+
private val supportedYUVFormats: List<Int> =
30+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
31+
listOf(ImageFormat.YUV_420_888, ImageFormat.YUV_422_888, ImageFormat.YUV_444_888)
32+
} else {
33+
listOf(ImageFormat.YUV_420_888)
34+
}
35+
36+
init {
37+
System.loadLibrary("zxing_android")
38+
}
2839

2940
// Enumerates barcode formats known to this package.
3041
// Note that this has to be kept synchronized with native (C++/JNI) side.
31-
enum class Format {
42+
public enum class Format {
3243
NONE, AZTEC, CODABAR, CODE_39, CODE_93, CODE_128, DATA_BAR, DATA_BAR_EXPANDED,
3344
DATA_MATRIX, EAN_8, EAN_13, ITF, MAXICODE, PDF_417, QR_CODE, MICRO_QR_CODE, UPC_A, UPC_E
3445
}
35-
enum class ContentType {
46+
47+
public enum class ContentType {
3648
TEXT, BINARY, MIXED, GS1, ISO15434, UNKNOWN_ECI
3749
}
3850

39-
data class Options(
51+
public data class Options(
4052
val formats: Set<Format> = setOf(),
4153
val tryHarder: Boolean = false,
4254
val tryRotate: Boolean = false,
4355
val tryInvert: Boolean = false,
4456
val tryDownscale: Boolean = false
4557
)
4658

47-
data class Position(
59+
public data class Position(
4860
val topLeft: Point,
4961
val topRight: Point,
5062
val bottomLeft: Point,
5163
val bottomRight: Point,
5264
val orientation: Double
5365
)
5466

55-
data class Result(
67+
public data class Result(
5668
val format: Format = Format.NONE,
5769
val bytes: ByteArray? = null,
5870
val text: String? = null,
@@ -64,12 +76,11 @@ class BarcodeReader {
6476
val symbologyIdentifier: String? = null
6577
)
6678

67-
var options : Options = Options()
79+
public var options : Options = Options()
6880

69-
fun read(image: ImageProxy): Result? {
70-
val supportedYUVFormats = arrayOf(ImageFormat.YUV_420_888, ImageFormat.YUV_422_888, ImageFormat.YUV_444_888)
71-
if (image.format !in supportedYUVFormats) {
72-
error("invalid image format")
81+
public fun read(image: ImageProxy): Result? {
82+
check(image.format in supportedYUVFormats) {
83+
"Invalid image format: ${image.format}. Must be one of: $supportedYUVFormats"
7384
}
7485

7586
var result = Result()
@@ -97,11 +108,11 @@ class BarcodeReader {
97108
}
98109
}
99110

100-
fun read(bitmap: Bitmap, cropRect: Rect = Rect(), rotation: Int = 0): Result? {
111+
public fun read(bitmap: Bitmap, cropRect: Rect = Rect(), rotation: Int = 0): Result? {
101112
return read(bitmap, options, cropRect, rotation)
102113
}
103114

104-
fun read(bitmap: Bitmap, options: Options, cropRect: Rect = Rect(), rotation: Int = 0): Result? {
115+
public fun read(bitmap: Bitmap, options: Options, cropRect: Rect = Rect(), rotation: Int = 0): Result? {
105116
var result = Result()
106117
val status = with(options) {
107118
readBitmap(
@@ -128,8 +139,4 @@ class BarcodeReader {
128139
formats: String, tryHarder: Boolean, tryRotate: Boolean, tryInvert: Boolean, tryDownscale: Boolean,
129140
result: Result,
130141
): String?
131-
132-
init {
133-
System.loadLibrary("zxing_android")
134-
}
135142
}

0 commit comments

Comments
 (0)