Skip to content
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

App Crash Due to High RAM Consumption When Using ML Kit #31

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ buildscript {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:8.1.1'
classpath 'com.android.tools.build:gradle:8.1.4'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.jacoco:org.jacoco.core:$jacocoVersion"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ class OSBARCBarcodeAnalyzer(
private val scanLibrary: OSBARCScanLibraryInterface,
private val imageHelper: OSBARCImageHelperInterface,
private val onBarcodeScanned: (String) -> Unit,
private val onScanningError: (OSBARCError) -> Unit
private val onScanningError: (OSBARCError) -> Unit,
private val delayMillis: Long? = 500L
): ImageAnalysis.Analyzer {

var isPortrait = true
private var lastAnalyzedTimestamp = 0L

companion object {
private const val LOG_TAG = "OSBARCBarcodeAnalyzer"
Expand All @@ -34,20 +36,30 @@ class OSBARCBarcodeAnalyzer(
* @param image - ImageProxy object that represents the image to be analyzed.
*/
override fun analyze(image: ImageProxy) {
try {
scanLibrary.scanBarcode(
image,
cropBitmap(image.toBitmap()),
{
onBarcodeScanned(it)
},
{
onScanningError(it)
}
)
} catch (e: Exception) {
e.message?.let { Log.e(LOG_TAG, it) }
onScanningError(OSBARCError.SCANNING_GENERAL_ERROR)
val currentTimestamp = System.currentTimeMillis()

// Use the delayMillis value, defaulting to 500 milliseconds if it is null
val effectiveDelayMillis = delayMillis ?: 500L

// Only analyze the image if the desired delay has passed
if (currentTimestamp - lastAnalyzedTimestamp >= effectiveDelayMillis) {
try {
scanLibrary.scanBarcode(
image,
cropBitmap(image.toBitmap()),
{
onBarcodeScanned(it)
},
{
onScanningError(it)
}
)
} catch (e: Exception) {
e.message?.let { Log.e(LOG_TAG, it) }
onScanningError(OSBARCError.SCANNING_GENERAL_ERROR)
}
// Update the timestamp of the last analyzed frame
lastAnalyzedTimestamp = currentTimestamp
}
image.close()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ data class OSBARCScanParameters(
@SerializedName("scanButton") val scanButton: Boolean,
@SerializedName("scanText") val scanText: String,
@SerializedName("hint") val hint: Int?,
@SerializedName("androidScanningLibrary") val androidScanningLibrary: String?
@SerializedName("androidScanningLibrary") val androidScanningLibrary: String?,
@SerializedName("scanInterval") val scanInterval: Long?
) : Serializable
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,8 @@ class OSBARCScannerActivity : ComponentActivity() {
},
{
processReadError(it)
}
},
parameters.scanInterval
)

setContent {
Expand Down
Loading