【发布时间】:2020-01-28 12:32:03
【问题描述】:
我想从 ImageAnalyzer.analyze 中释放值作为原始值并将其发送到另一个 Activity。它变得不像看起来那么简单,因为我不能用返回覆盖函数。我也无法制作 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() {}
}
}
}
接下来我做了。如果我尝试使用此上下文制作 Toast,应用程序将崩溃。尝试打开另一个活动,函数 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")}
}
}
}
【问题讨论】:
-
我不会说 Kotlin,但在 Java 中
ImageAnalyzer可以有一个ImageAnalyzerClient接口,另一个类将实现该接口并将其自身提供给ImageAnalyzer作为ImageAnalyzerClient的实例。该接口将有一个从ImageAnalyzer接收数据的方法。看起来这里有一个简短的教程,首先在“Java 方式”中在 Kotlin 中执行此操作,然后以“Kotlin 方式”:https://proandroiddev.com/kotlin-functions-an-alternative-to-interfaces-7cfb3c435900。 -
听起来很有趣。我明天试试。谢谢
-
即使它是接收数据的方法,我也不知道在哪里调用它。从 addOnSuccesListener 获取数据到字符串或数组似乎是不可能的。在示例中,我看不到他们如何提取数据。这一切都那么悲伤(((
-
我只有一个想法:尝试在片段中使用 camerax。当我记得将数据从片段返回到活动时,我不需要上下文?
-
“我不知道在哪里调用它。”你想从
ImageAnalyzer.analyze()发送rawValue,所以你在ImageAnalyzer.analyze()调用它一次rawValue有被设置为一个值。除了你写了“将它发送到另一个活动” 并尝试使用Intent来启动另一个Activity,所以我想我不太确定确切的用例了。在 for 循环中包含startActivity()绝对是个坏主意。那么,您是否想发送rawValue对象的集合?DataAnalyse类甚至是Activity吗?您可以编辑问题并澄清。
标签: android firebase firebase-mlkit