Can't import import android.hardware.camera2 c

2019-01-19 10:40发布

问题:

I imported the opencv android library to my android studio and the Camera2Renderer class has a lot of compiler errors because the android.hardware.camera2 classes can't be imported.

回答1:

I solved the problem. Jim was right, i did not have the correct target API. For the next person who has this problem and finds this thread, the solution is that you have to make sure that the build.gradle files for your project and your openCV match. Thanks to everyone who helped me, and Merry Christmas!



回答2:

i am working with openCVLibrary3.2.0 and trying to run its sample and faced same issue but i changed gradle files for both mysampleApp and openCVLibrary320 module as below

This is my app build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "org.opencv.face"
        minSdkVersion 9
        targetSdkVersion 21
        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_5
            targetCompatibility JavaVersion.VERSION_1_5
        }

        ndk {
            moduleName "native_sample"
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
    externalNativeBuild {
        ndkBuild {
            path 'src/main/jni/Android.mk'
        }
    }
}

dependencies {
    compile project(path: ':openCVLibrary320')
}

and this my OpenCV library module build.gradle file

apply plugin: 'com.android.library'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        minSdkVersion 9
        targetSdkVersion 21
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
} 

Note: things to notice are that compileSdkVersion,buildToolsVersion,minSdkVersion, targetSdkVersion these must be same for all gradle files

i changed the compileSdkVersion from 14 to 23 and buildToolsVersion to "23.0.2" this solved the camera2 import related issue



回答3:

I am working on openCVLibrary330 trying to run its sample and faced same issue but i changed gradle files for openCVLibrary330 module as below

This is my app build.gradle

apply plugin: 'com.android.library'

android {
  compileSdkVersion 23

  buildToolsVersion "26.0.2"

  defaultConfig {
    minSdkVersion 8
    targetSdkVersion 21
  }

  buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
    }
  }
}


回答4:

I had the same problem. But, as many people have suggested I didn't had to change the gradle files to match exactly the same. I changed my compileSdkVersion and buildToolsVersion to the same on both gradle files, the rest are different. Its working fine now.