3

我正在尝试在干净的 android 应用程序中包含一个 CameraView 组件(来自 CameraX jetpack),如下所示:

https://medium.com/@hitherejoe/exploring-camerax-on-android-camera-view-daae6dfaa4ec https://developer.android.google.cn/jetpack/androidx/releases/camera

CameraView 非常简单,但是将其绑定到活动生命周期时出现问题。活动已授予相机权限。我尝试使用没有 CameraView 的 CameraX(使用 SurfaceView + 所有设置),但以相同的结果和异常结束(并且 CameraView 更易于使用)。在三星 S10 和 Redmi Note 7 上测试。似乎组件尺寸/测量存在问题:

例外

java.lang.BootstrapMethodError: Exception from call site #2 bootstrap method
        at androidx.camera.core.Preview.updateListener(Preview.java:368)
        at androidx.camera.core.Preview.updateOutput(Preview.java:586)
        at androidx.camera.core.Preview.updateConfigAndOutput(Preview.java:363)
        at androidx.camera.core.Preview.onSuggestedResolutionUpdated(Preview.java:524)
        at androidx.camera.core.UseCase.updateSuggestedResolution(UseCase.java:408)
        at androidx.camera.core.CameraX.calculateSuggestedResolutions(CameraX.java:684)
        at androidx.camera.core.CameraX.bindToLifecycle(CameraX.java:195)
        at androidx.camera.view.CameraXModule.bindToLifecycleAfterViewMeasured(CameraXModule.java:268)
        at androidx.camera.view.CameraView.onMeasure(CameraView.java:364)
        at android.view.View.measure(View.java:24973)
        at androidx.constraintlayout.widget.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:1227)
        at androidx.constraintlayout.widget.ConstraintLayout.onMeasure(ConstraintLayout.java:1572)
        at android.view.View.measure(View.java:24973)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:7139)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
        at androidx.appcompat.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:143)
        at android.view.View.measure(View.java:24973)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:7139)
        at androidx.appcompat.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:403)
        at android.view.View.measure(View.java:24973)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:7139)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
        at android.view.View.measure(View.java:24973)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:7139)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1535)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:825)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:704)
        at android.view.View.measure(View.java:24973)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:7139)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
        at com.android.internal.policy.DecorView.onMeasure(DecorView.java:992)
        at android.view.View.measure(View.java:24973)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:3289)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:2016)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2318)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1876)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8499)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:949)
        at android.view.Choreographer.doCallbacks(Choreographer.java:761)
        at android.view.Choreographer.doFrame(Choreographer.java:696)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:935)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7037)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)
     Caused by: java.lang.ClassCastException: Bootstrap method returned null
        at androidx.camera.core.Preview.updateListener(Preview.java:368) 
        at androidx.camera.core.Preview.updateOutput(Preview.java:586) 
        at androidx.camera.core.Preview.updateConfigAndOutput(Preview.java:363) 
        at androidx.camera.core.Preview.onSuggestedResolutionUpdated(Preview.java:524) 
        at androidx.camera.core.UseCase.updateSuggestedResolution(UseCase.java:408) 
        at androidx.camera.core.CameraX.calculateSuggestedResolutions(CameraX.java:684) 
        at androidx.camera.core.CameraX.bindToLifecycle(CameraX.java:195) 
        at androidx.camera.view.CameraXModule.bindToLifecycleAfterViewMeasured(CameraXModule.java:268) 
        at androidx.camera.view.CameraView.onMeasure(CameraView.java:364) 
        at android.view.View.measure(View.java:24973) 
        at androidx.constraintlayout.widget.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:1227) 
        at androidx.constraintlayout.widget.ConstraintLayout.onMeasure(ConstraintLayout.java:1572) 
        at android.view.View.measure(View.java:24973) 
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:7139) 
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185) 
        at androidx.appcompat.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:143) 
        at android.view.View.measure(View.java:24973) 
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:7139) 
        at androidx.appcompat.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:403) 
        at android.view.View.measure(View.java:24973) 
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:7139) 
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185) 
        at android.view.View.measure(View.java:24973) 
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:7139) 
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1535) 
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:825) 
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:704) 
        at android.view.View.measure(View.java:24973) 
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:7139) 
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185) 
        at com.android.internal.policy.DecorView.onMeasure(DecorView.java:992) 
        at android.view.View.measure(View.java:24973) 
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:3289) 
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:2016) 
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2318) 
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1876) 
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8499) 
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:949) 
        at android.view.Choreographer.doCallbacks(Choreographer.java:761) 
        at android.view.Choreographer.doFrame(Choreographer.java:696) 
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:935) 
        at android.os.Handler.handleCallback(Handler.java:873) 
        at android.os.Handler.dispatchMessage(Handler.java:99) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7037) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965) 

依赖项

implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.core:core-ktx:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

    def camerax_version = "1.0.0-alpha06"
    def camerax_view_version = "1.0.0-alpha03"
    def camerax_ext_version = "1.0.0-alpha03"

    implementation "androidx.camera:camera-core:$camerax_version"
    implementation "androidx.camera:camera-camera2:$camerax_version"
    implementation "androidx.camera:camera-view:$camerax_view_version"
    implementation "androidx.camera:camera-extensions:$camerax_ext_version"

    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

布局

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.camera.view.CameraView
        android:id="@+id/view_camera"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

主要的

class MainActivity : AppCompatActivity(), LifecycleOwner {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        view_camera.bindToLifecycle(this)
    }
}

我觉得我错过了一些简单的东西,但我在这方面很新,而且由于 CameraX 很新,所以没有太多关于它的问题。

4

1 回答 1

6

问题已解决:我必须启用 Java 8 兼容性:

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
    jvmTarget = "1.8"
}
于 2019-10-18T12:31:27.727 回答