Skip to content

Commit 45e2070

Browse files
committed
android: replace Result.time with BarcodeReader.lastReadTime
Does not clutter Result struct and also works when no barcode was found.
1 parent 55573f7 commit 45e2070

File tree

3 files changed

+12
-11
lines changed

3 files changed

+12
-11
lines changed

wrappers/android/app/src/main/java/zxingcpp/app/MainActivity.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,6 @@ class MainActivity : AppCompatActivity() {
250250
resultText = try {
251251
image.use {
252252
readerCpp.read(it)
253-
}.apply {
254-
runtime2 += firstOrNull()?.time ?: 0
255253
}.joinToString("\n") { result ->
256254
result.position.let {
257255
resultPoints.add(listOf(
@@ -274,6 +272,7 @@ class MainActivity : AppCompatActivity() {
274272
}
275273

276274
runtimes += System.currentTimeMillis() - startTime
275+
runtime2 += readerCpp.lastReadTime
277276

278277
var infoText: String? = null
279278
if (++frameCounter == 15) {

wrappers/android/zxingcpp/src/main/cpp/ZXingCpp.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ static jobject NewError(JNIEnv* env, const Error& error)
194194
return env->NewObject(cls, midInit, NewEnum(env, JavaErrorTypeName(error.type()), "ErrorType"), C2JString(env, error.msg()));
195195
}
196196

197-
static jobject NewResult(JNIEnv* env, const Result& result, int time)
197+
static jobject NewResult(JNIEnv* env, const Result& result)
198198
{
199199
jclass cls = env->FindClass(PACKAGE "Result");
200200
jmethodID midInit = env->GetMethodID(
@@ -213,7 +213,7 @@ static jobject NewResult(JNIEnv* env, const Result& result, int time)
213213
"Z"
214214
"I"
215215
"L" PACKAGE "Error;"
216-
"I)V");
216+
")V");
217217
bool valid = result.isValid();
218218
return env->NewObject(cls, midInit,
219219
NewEnum(env, JavaBarcodeFormatName(result.format()), "Format"),
@@ -229,12 +229,11 @@ static jobject NewResult(JNIEnv* env, const Result& result, int time)
229229
valid ? C2JString(env, result.sequenceId()) : nullptr,
230230
result.readerInit(),
231231
result.lineCount(),
232-
result.error() ? NewError(env, result.error()) : nullptr,
233-
time
232+
result.error() ? NewError(env, result.error()) : nullptr
234233
);
235234
}
236235

237-
static jobject Read(JNIEnv *env, ImageView image, const DecodeHints& hints)
236+
static jobject Read(JNIEnv *env, jobject thiz, ImageView image, const DecodeHints& hints)
238237
{
239238
try {
240239
auto startTime = std::chrono::high_resolution_clock::now();
@@ -243,12 +242,14 @@ static jobject Read(JNIEnv *env, ImageView image, const DecodeHints& hints)
243242
// LOGD("time: %4d ms\n", (int)std::chrono::duration_cast<std::chrono::milliseconds>(duration).count());
244243
auto time = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
245244

245+
env->SetIntField(thiz, env->GetFieldID(env->GetObjectClass(thiz), "lastReadTime", "I"), time);
246+
246247
jclass clsList = env->FindClass("java/util/ArrayList");
247248
jobject objList = env->NewObject(clsList, env->GetMethodID(clsList, "<init>", "()V"));
248249
if (!results.empty()) {
249250
jmethodID midAdd = env->GetMethodID(clsList, "add", "(Ljava/lang/Object;)Z");
250251
for (const auto& result: results)
251-
env->CallBooleanMethod(objList, midAdd, NewResult(env, result, time));
252+
env->CallBooleanMethod(objList, midAdd, NewResult(env, result));
252253
}
253254
return objList;
254255
} catch (const std::exception& e) {
@@ -329,7 +330,7 @@ Java_zxingcpp_BarcodeReader_readYBuffer(
329330
ImageView{pixels + top * rowStride + left, width, height, ImageFormat::Lum, rowStride}
330331
.rotated(rotation);
331332

332-
return Read(env, image, CreateDecodeHints(env, options));
333+
return Read(env, thiz, image, CreateDecodeHints(env, options));
333334
}
334335

335336
struct LockedPixels
@@ -376,5 +377,5 @@ Java_zxingcpp_BarcodeReader_readBitmap(
376377
.cropped(left, top, width, height)
377378
.rotated(rotation);
378379

379-
return Read(env, image, CreateDecodeHints(env, options));
380+
return Read(env, thiz, image, CreateDecodeHints(env, options));
380381
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,10 @@ public class BarcodeReader(public var options: Options = Options()) {
112112
val readerInit: Boolean,
113113
val lineCount: Int,
114114
val error: Error?,
115-
val time: Int // for development/debug purposes only
116115
)
117116

117+
public val lastReadTime : Int = 0 // runtime of last read call in ms (for debugging purposes only)
118+
118119
public fun read(image: ImageProxy): List<Result> {
119120
check(image.format in supportedYUVFormats) {
120121
"Invalid image format: ${image.format}. Must be one of: $supportedYUVFormats"

0 commit comments

Comments
 (0)