Created
March 2, 2022 01:12
-
-
Save Astroa7m/19a4556ad7009a109aec15285f87078f to your computer and use it in GitHub Desktop.
This file contains 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
class QRCodeWIFIAnalyzer( | |
private val onSuccess: (ssid: String, password: String) -> Unit, | |
private val onFailure: (e: Exception) -> Unit | |
) : ImageAnalysis.Analyzer { | |
private val scanner: BarcodeScanner = BarcodeScanning.getClient(BarcodeScannerOptions.Builder().setBarcodeFormats(Barcode.FORMAT_QR_CODE).build()) | |
@SuppressLint("UnsafeOptInUsageError") | |
override fun analyze(imageProxy: ImageProxy) { | |
val mediaImage = imageProxy.image | |
if (mediaImage != null){ | |
val image = InputImage.fromMediaImage(mediaImage, imageProxy.imageInfo.rotationDegrees) | |
scanner.process(image) | |
.addOnSuccessListener {barcodes-> | |
for (barcode in barcodes){ | |
val valueType = barcode.valueType | |
if(valueType == Barcode.TYPE_WIFI){ | |
val ssid = barcode.wifi!!.ssid | |
val password = barcode.wifi!!.password | |
if(ssid !=null && password!= null){ | |
onSuccess(ssid, password) | |
}else{ | |
onFailure(NullPointerException("WIFI credentials must not be null")) | |
} | |
}else{ | |
onFailure(IllegalArgumentException("Barcode is not resembling a WIFI network")) | |
} | |
} | |
} | |
.addOnFailureListener{ | |
onFailure(it) | |
} | |
.addOnCompleteListener { | |
imageProxy.close() | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment