Barcode Detection
iOS Barcode Detection
- Initialize and call barcode handler inside GPUImage Custom Filter.
let barcodeRequest = VNDetectBarcodesRequest(completionHandler: { request, error in
guard let results = request.results else { return }
for result in results {
if let barcode = result as? VNBarcodeObservation {
if let barcodeData = barcode.payloadStringValue {
print("Data is \(barcodeData)")
let symbology = barcode.symbology.rawValue
print("Symbology: \(symbology)")
if symbology == "VNBarcodeSymbologyDataMatrix" {
if let dmDetectCallback = self.dataDMDetectCallback {
dmDetectCallback(barcodeData)
}
} else if symbology == "VNBarcodeSymbologyQR" {
if let qrDetectCallback = self.dataQRDetectCallback {
qrDetectCallback(barcodeData)
}
}
}
}
}
})
// Perform the barcode-request. This will call the completionHandler of the barcode-request.
guard let _ = try? handler.perform([barcodeRequest]) else {
return print("Could not perform barcode-request!")
}
Android Barcode Detection
- Initialize Barcode Detector - BarcodeCaptureActivity.kt
@SuppressLint("InlinedApi")
private fun createCameraSource(autoFocus: Boolean, useFlash: Boolean) {
val context = applicationContext
val barcodeDetector = BarcodeDetector.Builder(context)
.setBarcodeFormats(Barcode.DATA_MATRIX or Barcode.QR_CODE)
.build()
val barcodeFactory = BarcodeTrackerFactory(this)
barcodeDetector.setProcessor(MultiProcessor.Builder(barcodeFactory).build())
...
}
- Detect barcodes when image frames are available - CameraSource.kt
mDetector!!.receiveFrame(lfrmBarcode)
private inner class FrameProcessingRunnable internal constructor(private var mDetector:Detector<*>?) : Runnable {
private val mStartTimeMillis = SystemClock.elapsedRealtime()
...
override fun run() {
var outputFrame: Frame
var data: ByteBuffer?
while (true) {
synchronized(mLock) {
while (mActive && mPendingFrameData == null) {
try {
mLock.wait()
} catch (e: InterruptedException) {
Log.d(TAG, "Frame processing loop terminated.", e)
return
}
}
if (!mActive) {
return
}
outputFrame = Frame.Builder()
.setImageData(
mPendingFrameData, mPreviewSize!!.width,
mPreviewSize!!.height, ImageFormat.NV21
)
.setId(mPendingFrameId)
.setTimestampMillis(mPendingTimeMillis)
.setRotation(mRotation)
.build()
data = mPendingFrameData
mPendingFrameData = null
}
try {
val lfrmBarcode = GetBarcodeFrame_OpenCV(outputFrame, cBcdCallback)
// Call Detector
mDetector!!.receiveFrame(lfrmBarcode)
//
} catch (t: Throwable) {
Log.e(TAG, "Exception thrown from receiver.", t)
} finally {
mCamera!!.addCallbackBuffer(data!!.array())
}
}
}
...
}