Last active
July 25, 2018 15:35
-
-
Save omarmiatello/4627f9e773aaebea12f2bce357446dc8 to your computer and use it in GitHub Desktop.
Barcode Detector for Mobile Vision API (Add support for inverted color)Benchmark on Pixel 2: Invert color (8ms) + Decode (15ms)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.satispay.customer.profile.visionapi | |
import android.graphics.ImageFormat | |
import android.util.SparseArray | |
import com.google.android.gms.vision.Detector | |
import com.google.android.gms.vision.Frame | |
import com.google.android.gms.vision.barcode.Barcode | |
import java.nio.ByteBuffer | |
/** | |
* Created by omarmiatello on 14/02/18. | |
*/ | |
class BarcodeAdvancedDetector(private val delegate: Detector<Barcode>) : Detector<Barcode>() { | |
override fun isOperational() = delegate.isOperational | |
override fun setFocus(id: Int) = delegate.setFocus(id) | |
var lastFound = 0 | |
override fun detect(originalFrame: Frame): SparseArray<Barcode> { | |
// Benchmark on Pixel 2 XL | |
val frames = listOf({ | |
originalFrame | |
}, { | |
// [Benchmark] frame with inverted colors: 8 ms | |
Frame.Builder() | |
.setImageData( | |
ByteBuffer.wrap(originalFrame.grayscaleImageData.array().let { b -> | |
ByteArray(b.size) { (0xFFFFFFFF - b[it].toInt()).toByte() } | |
}), | |
originalFrame.metadata.width, | |
originalFrame.metadata.height, | |
ImageFormat.NV21 | |
) | |
.setRotation(originalFrame.metadata.rotation) | |
.build() | |
}) | |
// Try first `lastFound` frame | |
val order = (lastFound..lastFound) + ((0 until frames.size) - lastFound) | |
order.forEach { | |
val frame = frames[it]() | |
val detections = delegate.detect(frame) // [Benchmark] each loop, detect: 12-18 ms | |
if (detections.size() != 0) { | |
lastFound = it | |
return detections | |
} | |
} | |
return SparseArray() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment