Я хочу освободить значения из ImageAnalyzer.analyze как необработанные значения и отправить его в другое действие. Это стало не так просто, как кажется, потому что я не могу переопределить функцию с возвратом. Я также не могу сделать Toast или открыть другую функцию Activity внутри, потому что я не могу вставить в нее Context. Пожалуйста, помогите.
private class ImageAnalyzer : ImageAnalysis.Analyzer {
private fun degreesToFirebaseRotation(degrees: Int): Int = when (degrees) {
0 -> FirebaseVisionImageMetadata.ROTATION_0
90 -> FirebaseVisionImageMetadata.ROTATION_90
180 -> FirebaseVisionImageMetadata.ROTATION_180
270 -> FirebaseVisionImageMetadata.ROTATION_270
else -> throw Exception("Rotation must be 0, 90, 180, or 270.")
}
override fun analyze(imageProxy: ImageProxy?, degrees: Int){
val mediaImage = imageProxy?.image
val imageRotation = degreesToFirebaseRotation(degrees)
if (mediaImage != null) {
val image = FirebaseVisionImage.fromMediaImage(mediaImage, imageRotation)
val options = FirebaseVisionBarcodeDetectorOptions.Builder().setBarcodeFormats(FirebaseVisionBarcode.FORMAT_ALL_FORMATS).build()
val detector = FirebaseVision.getInstance().getVisionBarcodeDetector(options)
val result= detector.detectInImage(image) .addOnSuccessListener(){barcodes->
for (barcode in barcodes) {
val bounds = barcode.boundingBox
val corners = barcode.cornerPoints
val rawValue = barcode.rawValue
val valueType = barcode.valueType
// See API reference for complete list of supported types
when (valueType) {
FirebaseVisionBarcode.TYPE_WIFI -> {
val ssid = barcode.wifi!!.ssid
val password = barcode.wifi!!.password
val type = barcode.wifi!!.encryptionType
}
FirebaseVisionBarcode.TYPE_URL -> {
val title = barcode.url!!.title
val url = barcode.url!!.url
}
}
}
}
.addOnFailureListener() {}
}
}
}
Я сделал следующее. Если я пытаюсь сделать тост с этим контекстным приложением, происходит сбой. Попробуйте открыть другое действие, функция startActivity () хочет какие-то странные параметры. Я не знаю, какой пакет ему нужен
private class ImageAnalyzer : ImageAnalysis.Analyzer {
val liveScan=LiveScan()
val contex=liveScan.applicationContext
private fun degreesToFirebaseRotation(degrees: Int): Int = when (degrees) {
0 -> FirebaseVisionImageMetadata.ROTATION_0
90 -> FirebaseVisionImageMetadata.ROTATION_90
180 -> FirebaseVisionImageMetadata.ROTATION_180
270 -> FirebaseVisionImageMetadata.ROTATION_270
else -> throw Exception("Rotation must be 0, 90, 180, or 270.")
}
override fun analyze(imageProxy: ImageProxy?, degrees: Int){
val mediaImage = imageProxy?.image
val imageRotation = degreesToFirebaseRotation(degrees)
if (mediaImage != null) {
val image = FirebaseVisionImage.fromMediaImage(mediaImage, imageRotation)
val options = FirebaseVisionBarcodeDetectorOptions.Builder().setBarcodeFormats(FirebaseVisionBarcode.FORMAT_ALL_FORMATS).build()
val detector = FirebaseVision.getInstance().getVisionBarcodeDetector(options)
val result= detector.detectInImage(image)
.addOnSuccessListener(){ barcodes->
for (barcode in barcodes) {
val bounds = barcode.boundingBox
val corners = barcode.cornerPoints
val rawValue = barcode.rawValue
val intent= Intent(contex,DataAnalyse::class.java).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
startActivity(contex,intent,)
}
}
.addOnFailureListener() {Log.d("CameraXApp", "no_barcode")}
}
}
}