imageProxy参数在这个lambda表达式中是如何被定义的?

huangapple go评论73阅读模式
英文:

How does the imageProxy parameter in this lambda expression get defined?

问题

摘要

在Google的MLKit Vision示例代码中,此lambda表达式 中的imageProxy参数是如何定义的?

详细内容

我理解lambda表达式的基本概念,例如w3schools的示例,以及由Oracle示例提供的更详细示例,但我很难理解MLKit中引用的示例,出于以下原因,因为它似乎与其他示例不同:

  1. analysisUseCase.setAnalyzer中没有与变量声明相关联,就像w3schools链接中的最后两个示例一样。因此,我不知道imageProxy如何通过对某个变量的调用在其他地方被定义,因为没有要引用的变量。
  2. 根据setAnalyzer的javadoc注释,analysisUseCase.setAnalyzer的两个参数应为setAnalyzer(@NonNull Executor executor, @NonNull Analyzer analyzer)。除非Analyzer对象是某种Consumer,否则我看不出与w3schools中的第一个示例有任何关系。此外,据我理解,这将意味着lambda表达式应返回这样一个Analyzer对象,但没有返回语句。

此外,Android Studio对其的工具提示显示它解析为androidx.camera.core.ImageProxy imageProxy。linter如何知道如何解析这个?

作为基本问题,我想知道这是因为我正在修改此示例代码,并希望在我的代码中的其他位置访问imageProxy.getWidth()方法。如果不理解lambda表达式,我不确定如何访问此对象和关联的方法。

英文:

Summary

How does the imageProxy parameter in this lambda expression within Google's MLKit Vision example code get defined?

Details

I understand the basic concepts of lamda expressions, such as those given in the w3schools examples, and more detailed examples given by the Oracle examples but I am struggling to understand how the quoted one from the MLKit is resolved for the following reasons as it seemingly differs from the other examples:

  1. There is no variable declaration associated with analysisUseCase.setAnalyzer, like with the two last examples from the w3schools link above. So I don't know how imageProxy would be defined elsewhere via a call to some variable as there is no variable to refer to.
  2. According to the javadoc comments on setAnalyzer, the two parameters for analysisUseCase.setAnalyzer should be setAnalyzer(@NonNull Executor executor, @NonNull Analyzer analyzer). Unless an Analyzer object is some kind of Consumer, I see no relation to the first example in w3schools. Furthermore, it is my understanding that this would imply the lamda expression should return such an Analyzer object, but there is no return statement.

Furthermore, Android Studio's tooltip for it shows that it resolves to androidx.camera.core.ImageProxy imageProxy. How does the linter know how to resolve this?

The underlying question as to why I want to know this is that I am modifying this sample code and want access to the imageProxy.getWidth() method elsewhere in my code. Without understanding the lamda expressions, I am not sure how to access this object and associated method.

答案1

得分: 1

analysisUseCase的类型是 ImageAnalysis,它提供了一个名为 setAnalyzer 的方法,该方法的第二个参数是 ImageAnalysis.Analyzer

这个 Analyzer 是一个接口,只有一个方法,意味着可以使用 lambda 表达式来替代它。唯一定义的方法是 analyze(ImageProxy image),在这种情况下,lambda 表达式实际上就代表了这个方法。正如你所看到的,analyze 的唯一参数是 ImageProxy

英文:

analysisUseCase is of type ImageAnalysis and offers a method setAnalyzer that has a ImageAnalysis.Analyzer as a second argument.

That Analyzer is an interface with one method meaning a lambda expression can be used in its place. The only method defined is analyze(ImageProxy image), and that is what the lambda actually represents in this situation. As you can see the one argument to analyze is ImageProxy.

huangapple
  • 本文由 发表于 2020年9月7日 15:04:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/63772803.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定