Skip to content

Correctly handle supported image formats on Android APIs < 23 #622

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions wrappers/android/zxingcpp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ android {
}
}

kotlin {
explicitApi()
}

dependencies {
implementation(libs.androidx.camera.core)
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,39 +20,51 @@ import android.graphics.Bitmap
import android.graphics.ImageFormat
import android.graphics.Point
import android.graphics.Rect
import android.os.Build
import androidx.camera.core.ImageProxy
import java.lang.RuntimeException
import java.nio.ByteBuffer

class BarcodeReader {
public class BarcodeReader {
private val supportedYUVFormats: List<Int> =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
listOf(ImageFormat.YUV_420_888, ImageFormat.YUV_422_888, ImageFormat.YUV_444_888)
} else {
listOf(ImageFormat.YUV_420_888)
}

init {
System.loadLibrary("zxing_android")
}

// Enumerates barcode formats known to this package.
// Note that this has to be kept synchronized with native (C++/JNI) side.
enum class Format {
public enum class Format {
NONE, AZTEC, CODABAR, CODE_39, CODE_93, CODE_128, DATA_BAR, DATA_BAR_EXPANDED,
DATA_MATRIX, EAN_8, EAN_13, ITF, MAXICODE, PDF_417, QR_CODE, MICRO_QR_CODE, UPC_A, UPC_E
}
enum class ContentType {

public enum class ContentType {
TEXT, BINARY, MIXED, GS1, ISO15434, UNKNOWN_ECI
}

data class Options(
public data class Options(
val formats: Set<Format> = setOf(),
val tryHarder: Boolean = false,
val tryRotate: Boolean = false,
val tryInvert: Boolean = false,
val tryDownscale: Boolean = false
)

data class Position(
public data class Position(
val topLeft: Point,
val topRight: Point,
val bottomLeft: Point,
val bottomRight: Point,
val orientation: Double
)

data class Result(
public data class Result(
val format: Format = Format.NONE,
val bytes: ByteArray? = null,
val text: String? = null,
Expand All @@ -64,12 +76,11 @@ class BarcodeReader {
val symbologyIdentifier: String? = null
)

var options : Options = Options()
public var options : Options = Options()

fun read(image: ImageProxy): Result? {
val supportedYUVFormats = arrayOf(ImageFormat.YUV_420_888, ImageFormat.YUV_422_888, ImageFormat.YUV_444_888)
if (image.format !in supportedYUVFormats) {
error("invalid image format")
public fun read(image: ImageProxy): Result? {
check(image.format in supportedYUVFormats) {
"Invalid image format: ${image.format}. Must be one of: $supportedYUVFormats"
}

var result = Result()
Expand Down Expand Up @@ -97,11 +108,11 @@ class BarcodeReader {
}
}

fun read(bitmap: Bitmap, cropRect: Rect = Rect(), rotation: Int = 0): Result? {
public fun read(bitmap: Bitmap, cropRect: Rect = Rect(), rotation: Int = 0): Result? {
return read(bitmap, options, cropRect, rotation)
}

fun read(bitmap: Bitmap, options: Options, cropRect: Rect = Rect(), rotation: Int = 0): Result? {
public fun read(bitmap: Bitmap, options: Options, cropRect: Rect = Rect(), rotation: Int = 0): Result? {
var result = Result()
val status = with(options) {
readBitmap(
Expand All @@ -128,8 +139,4 @@ class BarcodeReader {
formats: String, tryHarder: Boolean, tryRotate: Boolean, tryInvert: Boolean, tryDownscale: Boolean,
result: Result,
): String?

init {
System.loadLibrary("zxing_android")
}
}
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy