前言:搞机器视觉,目前一般都有用摄像模组,或者工业相机,可是如果要搞到消费类,手机却不行。因为,手机上有个各种特效,这些都会对视觉效果有影响。所以,要在手机上搞,必须对手机的系统上的摄像头应用能够底层控制比有比较深入的了解。
这系列文章,尝试用谷歌的开源应用包Camera2来实现这个目标。
本节,讲如何进行环境配置,并且让摄像头能够在手机上运行起来。
测试硬件:
WIN10 DeskTop @ Thinkpad 作为编译器
小米CC9Pro作为移动调试器?
?
预备知识:谷歌编译系统:
1 基本概念:
配置 build ?|? Android 开发者 ?|? Android Developers (google.cn)?
Android 构建系统会编译应用资源和源代码,然后将它们打包到 APK 或 Android App Bundle 中,供您测试、部署、签名和分发。Android Studio 会使用高级构建工具包?Gradle?自动执行和管理构建流程,同时也允许您定义灵活的自定义 build 配置。
?Gradle 和 Android 插件独立于 Android Studio 运行。这意味着,您可以在 Android Studio 内、计算机上的命令行或未安装 Android Studio 的计算机(如持续集成服务器)上构建 Android 应用。如果您不使用 Android Studio,可以学习如何从命令行构建和运行应用。无论您是从命令行、在远程计算机上还是使用 Android Studio 构建项目,构建的输出都相同。
【案】Camer2的下载包,带了Gradle,我们用这个方法尝试一下。
?2 编译文件系统结构:
对比一下Camer2,
3 各个配置文件的简要说明
?3.1Gradle 设置文件
?
?【案】确实如此:camer2,里面配置就一个application.
?3.2?顶层构建文件
顶层?build.gradle ?文件位于项目的根目录下,用于定义适用于项目中所有模块的构建配置。默认情况下,顶层构建文件使用?buildscript ?代码块定义项目中所有模块共用的 Gradle 代码库和依赖项。以下代码示例说明了创建新项目后可在顶层?build.gradle ?文件中找到的默认设置和 DSL 元素
【案,有两个build.gradle】
这个是项目下的。谷歌给出的默认设定如下:
/**
* The buildscript block is where you configure the repositories and
* dependencies for Gradle itself—meaning, you should not include dependencies
* for your modules here. For example, this block includes the Android plugin for
* Gradle as a dependency because it provides the additional instructions Gradle
* needs to build Android app modules.
*/
buildscript {
/**
* The repositories block configures the repositories Gradle uses to
* search or download the dependencies. Gradle pre-configures support for remote
* repositories such as JCenter, Maven Central, and Ivy. You can also use local
* repositories or define your own remote repositories. The code below defines
* JCenter as the repository Gradle should use to look for its dependencies.
*
* New projects created using Android Studio 3.0 and higher also include
* Google's Maven repository.
*/
repositories {
google()
jcenter()
}
/**
* The dependencies block configures the dependencies Gradle needs to use
* to build your project. The following line adds Android plugin for Gradle
* version 7.0.0 as a classpath dependency.
*/
dependencies {
classpath 'com.android.tools.build:gradle:7.0.0'
}
}
/**
* The allprojects block is where you configure the repositories and
* dependencies used by all modules in your project, such as third-party plugins
* or libraries. However, you should configure module-specific dependencies in
* each module-level build.gradle file. For new projects, Android Studio
* includes JCenter and Google's Maven repository by default, but it does not
* configure any dependencies (unless you select a template that requires some).
*/
allprojects {
repositories {
google()
jcenter()
}
}
谷歌建议,如果要配置所有模块的共有属性,可以在上面的模块里面加如下配置:
// This block encapsulates custom properties and makes them available to all
// modules in the project.
ext {
// The following are only a few examples of the types of properties you can define.
sdkVersion = 28
// You can also create properties to specify versions for dependencies.
// Having consistent versions between modules can avoid conflicts with behavior.
supportLibVersion = "28.0.0"
...
}
3.3?配置项目全局属性
配置项目全局属性
对于包含多个模块的 Android 项目,可能有必要在项目级别定义某些属性并在所有模块之间共享这些属性。为此,您可以将额外的属性添加到顶层?build.gradle ?文件内的?ext ?代码块中。
然后,如果你的模块要访问这个属性,需要在该模块的?请在该模块的?build.gradle ?文件(您可以在下一部分中详细了解此文件)中使用以下语法。
android {
? ? // Use the following syntax to access properties you defined at the project level:
? ? // rootProject.ext.property_name
? ? compileSdkVersion rootProject.ext.compileSdkVersion
? ? ...
}
...
dependencies {
? ? implementation "com.android.support:appcompat-v7:${rootProject.ext.supportLibVersion}"
? ? ...
}
--------------------------------------------------------------------------------------------------------------------------------?
但是,camer2的包里是空的,啥也有没有,不知道有没有问题啊。【案,也许是】
希望我们把他当一个模块来用??
?3.4?模块级构建文件
模块级?build.gradle ?文件位于每个?project/module/ ?目录下,用于为其所在的特定模块配置构建设置。您可以通过配置这些构建设置提供自定义打包选项(如额外的构建类型和产品变种),以及替换?main/ ?应用清单或顶层?build.gradle ?文件中的设置。
以下 Android 应用模块?build.gradle ?文件示例简要说明了您应该了解的一些基础 DSL 元素和设置。
?在camer2的项目里面,module = Application被看成模块的话,下面有build.gradle的配置文件:
这里面的内容和谷歌官网的文件内容结构比较一致:
但是,感觉多了一些东西,而这些应该是放到根目录下的build.gradle下面的。
下面是google给出的,模组的构建配置举例:
/**
* The first line in the build configuration applies the Android plugin for
* Gradle to this build and makes the android block available to specify
* Android-specific build options.
*/
plugins {
id 'com.android.application'
}
/**
* The android block is where you configure all your Android-specific
* build options.
*/
android {
/**
* compileSdkVersion specifies the Android API level Gradle should use to
* compile your app. This means your app can use the API features included in
* this API level and lower.
*/
compileSdkVersion 28
/**
* buildToolsVersion specifies the version of the SDK build tools, command-line
* utilities, and compiler that Gradle should use to build your app. You need to
* download the build tools using the SDK Manager.
*
* This property is optional because the plugin uses a recommended version of
* the build tools by default.
*/
buildToolsVersion "30.0.2"
/**
* The defaultConfig block encapsulates default settings and entries for all
* build variants, and can override some attributes in main/AndroidManifest.xml
* dynamically from the build system. You can configure product flavors to override
* these values for different versions of your app.
*/
defaultConfig {
/**
* applicationId uniquely identifies the package for publishing.
* However, your source code should still reference the package name
* defined by the package attribute in the main/AndroidManifest.xml file.
*/
applicationId 'com.example.myapp'
// Defines the minimum API level required to run the app.
minSdkVersion 15
// Specifies the API level used to test the app.
targetSdkVersion 28
// Defines the version number of your app.
versionCode 1
// Defines a user-friendly version name for your app.
versionName "1.0"
}
/**
* The buildTypes block is where you can configure multiple build types.
* By default, the build system defines two build types: debug and release. The
* debug build type is not explicitly shown in the default build configuration,
* but it includes debugging tools and is signed with the debug key. The release
* build type applies Proguard settings and is not signed by default.
*/
buildTypes {
/**
* By default, Android Studio configures the release build type to enable code
* shrinking, using minifyEnabled, and specifies the default Proguard rules file.
*/
release {
minifyEnabled true // Enables code shrinking for the release build type.
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
/**
* The productFlavors block is where you can configure multiple product flavors.
* This allows you to create different versions of your app that can
* override the defaultConfig block with their own settings. Product flavors
* are optional, and the build system does not create them by default.
*
* This example creates a free and paid product flavor. Each product flavor
* then specifies its own application ID, so that they can exist on the Google
* Play Store, or an Android device, simultaneously.
*
* If you declare product flavors, you must also declare flavor dimensions
* and assign each flavor to a flavor dimension.
*/
flavorDimensions "tier"
productFlavors {
free {
dimension "tier"
applicationId 'com.example.myapp.free'
}
paid {
dimension "tier"
applicationId 'com.example.myapp.paid'
}
}
}
/**
* The dependencies block in the module-level build configuration file
* specifies dependencies required to build only the module itself.
* To learn more, go to Add build dependencies.
*/
dependencies {
implementation project(":lib")
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation fileTree(dir: 'libs', include: ['*.jar'])
}
?【案】而,这下面是camera2的gradle配置,可以看到和谷歌的标准模块定义还是有差别。【案,多了很多应该放到根目录下的配置项目,不知道会不会有问题啊】
buildscript {
repositories {
jcenter()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:6.9'
}
}
apply plugin: 'com.android.application'
repositories {
jcenter()
google()
}
dependencies {
implementation 'com.android.support:support-v4:28.0.0'
implementation 'com.android.support:support-v13:28.0.0'
implementation 'com.android.support:cardview-v7:28.0.0'
implementation 'com.android.support:appcompat-v7:28.0.0'
}
// The sample build uses multiple directories to
// keep boilerplate and common code separate from
// the main sample code.
List<String> dirs = [
'main', // main sample code; look here for the interesting stuff.
'common', // components that are reused by multiple samples
'template'] // boilerplate code that is generated by the sample template process
android {
compileSdkVersion 27
buildToolsVersion "27.0.2"
defaultConfig {
minSdkVersion 21
targetSdkVersion 27
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
sourceSets {
main {
dirs.each { dir ->
java.srcDirs "src/${dir}/java"
res.srcDirs "src/${dir}/res"
}
}
androidTest.setRoot('tests')
androidTest.java.srcDirs = ['tests/src']
}
}
3.5?Gradle 属性文件
Gradle 还包含两个属性文件,它们位于项目的根目录下,可用于指定 Gradle 构建工具包本身的设置:
gradle.properties
您可以在其中配置项目全局 Gradle 设置,如 Gradle 守护程序的最大堆大小。如需了解详情,请参阅构建环境。
local.properties
为构建系统配置本地环境属性,其中包括:
ndk.dir ?- NDK 的路径。此属性已被弃用。NDK 的所有下载版本都将安装在 Android SDK 目录下的?ndk ?目录中。sdk.dir ?- SDK 的路径。cmake.dir ?- CMake 的路径。ndk.symlinkdir ?- 在 Android Studio 3.5 及更高版本中,创建指向 NDK 的符号链接,该符号链接的路径可比 NDK 安装路径短。
?【案,这两个配置,感觉不是很重要】
3.5.1?将 NDK 重新映射到较短的路径(仅限 Windows)
【案,这部分是上面的再解释】
Windows 长路径最常见的问题就是 NDK 安装文件夹中的工具(如?ld.exe )会产生非常深的路径,但工具对于长路径的支持并不佳。
在?local.properties ?中,您可以设置?ndk.symlinkdir ?属性以请求 Gradle 插件创建指向 NDK 的符号链接。该符号链接的路径可比现有 NDK 文件夹的路径短。例如,ndk.symlinkdir = C:\ ?将生成以下符号链接:C:\ndk\19.0.5232133
?
4 基本操作说明:
将项目与 Gradle 文件同步
当您在项目中对构建配置文件进行更改时,Android Studio 会要求您同步项目文件,以便它导入构建配置更改并执行一些检查以确保您的配置不会造成构建错误。
如需同步项目文件,请点击做出更改后显示的通知栏中的?Sync Now(如图 3 所示),或者点击菜单栏中的?Sync Project?图标?
。如果 Android Studio 发现您的配置有任何错误(例如,您的源代码使用了只有在?compileSdkVersion ?以上的 API 级别中才会提供的 API 功能),就会显示?Messages?窗口,以说明相应的问题。
图 3.?在 Android Studio 中将项目与构建配置文件同步。
【案,感觉谷歌的文案写的很杂乱,反正你要耐着心情一点一点看】
?【现在开始,正式尝试编译camer2的源码】
1 环境准备:
1.1 Andord 环境准备:
Andorid Studio 下载:
Download Android Studio and SDK tools ?|? Android Developers (google.cn)
步骤截图:下载后直接选地方安装:
如果要选择安装目录和安装组件,选这里:?
系统配置选择:
1 选屏幕分辨率
2 选你手机上的版本:
1.2 camer2 源码下载:
mirrors / googlesamples / android-camera2basic · CODE CHINA (gitcode.net)
1.3 将Camer2的源码放到一个没有中文路径的地方
1.4 启动Android Studio开始编译:
1.4.1 Gradle Sync 解决编译问题:
问题列表:
1
Intel? HAXM installation failed. To install Intel? HAXM follow the instructions found at: https://github.com/intel/haxm/wiki/Installation-Instructions-on-Windows
这个是intel的优化,影响不大?
?2?
Caused by: org.gradle.api.tasks.StopExecutionException: Your project path contains non-ASCII characters. This will most likely cause the build to fail on Windows. Please move your project to a different directory. See http://b.android.com/95744 for details. This warning can be disabled by adding the line 'android.overridePathCheck=true' to gradle.properties file in the project directory.
这个是路径里面不要出现中文?
3
Unable to load class 'javax.xml.bind.annotation.XmlSchema'.
This is an unexpected error. Please file a bug containing the idea.log file.
4
Could not initialize class com.android.sdklib.repository.AndroidSdkHandler
改成:4.2.0 【参照了某些博客】,继续报错
Minimum supported Gradle version is 6.7.1. Current version is 4.8.
Please fix the project's Gradle settings.
继续报错:
Could not find com.android.tools.build:gradle:6.7.1. Searched in the following locations: ? - https://jcenter.bintray.com/com/android/tools/build/gradle/6.7.1/gradle-6.7.1.pom ? - https://jcenter.bintray.com/com/android/tools/build/gradle/6.7.1/gradle-6.7.1.jar ? - https://dl.google.com/dl/android/maven2/com/android/tools/build/gradle/6.7.1/gradle-6.7.1.pom ? - https://dl.google.com/dl/android/maven2/com/android/tools/build/gradle/6.7.1/gradle-6.7.1.jar Required by: ? ? project :Application
点击下载:连接不上,继续报错
到官网下载gradle
Gradle | Releaseshttps://gradle.org/releases/我们选一个6.9版本,用andord studio的IDE里面的配置进行路径指定,继续报错:
【调试信息查看】
【我们查看一下logs,看看详细信息,详细的log一般放在下面的路径】
【案,这个Log很重要,要学会看】
C:\Users\frank_sj\AppData\Local\Google\AndroidStudio2020.3\log\idea.log
?然后,在IDE的右下角的Event log窗口,有主要的调试信息:
?我们放大一点:
?调试信息里面有时间轴,这样可以方便我们回顾一下发生了什么否则是一头的懵逼。
11:43?? ?Gradle sync failed: Could not find com.android.tools.build:gradle:6.9. ?? ??? ??? ?Searched in the following locations: ?? ??? ??? ?- https://jcenter.bintray.com/com/android/tools/build/gradle/6.9/gradle-6.9.pom ?? ??? ??? ?- https://dl.google.com/dl/android/maven2/com/android/tools/build/gradle/6.9/gradle-6.9.pom ?? ??? ??? ?Required by: ?? ??? ??? ?project :Application ?? ??? ??? ?Add google Maven repository and sync project ?? ??? ??? ?Open File (18 s 262 ms)
【难道是6.9.0?】
14:07?? ?Gradle sync failed: Could not find com.android.tools.build:gradle:6.9.0. ?? ??? ??? ?Searched in the following locations: ?? ??? ??? ?- https://jcenter.bintray.com/com/android/tools/build/gradle/6.9.0/gradle-6.9.0.pom ?? ??? ??? ?- https://dl.google.com/dl/android/maven2/com/android/tools/build/gradle/6.9.0/gradle-6.9.0.pom ?? ??? ??? ?Required by: ?? ??? ??? ?project :Application ?? ??? ??? ?Add google Maven repository and sync project ?? ??? ??? ?Open File (58 s 995 ms)
然后,我们看一下详细的logs,
?这里报了一个【IllegalStateException】的配置表达错误,【需要再研究下】
看一下谷歌怎么配置这个:
Android Gradle 插件版本说明 ?|? Android 开发者 ?|? Android Developers (google.cn)
感觉里面的配置和camera2的配置有点出入:
然后,参考:
Android 解决Could not find com.android.tools.build:gradle-阿里云开发者社区 (aliyun.com)
而,我们的camer2,现在根目录build.gradle的配置是0啊,
【改】
按照谷歌默认的配置,给根目录的build.gradle添加配置:
/**
* The buildscript block is where you configure the repositories and
* dependencies for Gradle itself—meaning, you should not include dependencies
* for your modules here. For example, this block includes the Android plugin for
* Gradle as a dependency because it provides the additional instructions Gradle
* needs to build Android app modules.
*/
buildscript {
/**
* The repositories block configures the repositories Gradle uses to
* search or download the dependencies. Gradle pre-configures support for remote
* repositories such as JCenter, Maven Central, and Ivy. You can also use local
* repositories or define your own remote repositories. The code below defines
* JCenter as the repository Gradle should use to look for its dependencies.
*
* New projects created using Android Studio 3.0 and higher also include
* Google's Maven repository.
*/
repositories {
google()
jcenter()
}
/**
* The dependencies block configures the dependencies Gradle needs to use
* to build your project. The following line adds Android plugin for Gradle
* version 7.0.0 as a classpath dependency.
*/
dependencies {
classpath 'com.android.tools.build:gradle:7.0.0'
}
}
/**
* The allprojects block is where you configure the repositories and
* dependencies used by all modules in your project, such as third-party plugins
* or libraries. However, you should configure module-specific dependencies in
* each module-level build.gradle file. For new projects, Android Studio
* includes JCenter and Google's Maven repository by default, but it does not
* configure any dependencies (unless you select a template that requires some).
*/
allprojects {
repositories {
google()
jcenter()
}
}
重新同步:
然后解决了之前,语句【IllegalStateException】的错误,然后又报了:
【 Gradle wrapper】的版本错误。
Minimum supported Gradle version is 7.0.2. Current version is 6.9.
Please fix the project's Gradle settings. Change Gradle version in Gradle wrapper to 7.0.2 and re-import project Open Gradle wrapper properties Gradle Settings.
?
改成:
#Thu Dec 09 00:43:29 CST 2021
distributionBase=GRADLE_USER_HOME
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-bin.zip
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
?
?【然后,终于sync成功了】
14:11?? ?Gradle sync started
14:16?? ?Gradle sync finished in 4 m 37 s 391 ms
14:16?? ?Project update recommended: Android Gradle Plugin can be upgraded.
14:16?? ?* daemon not running; starting now at tcp:5037
14:16?? ?* daemon started successfully
?
?1.4.2 build:
继续编译,camera2,出现link的错误:
Android resource linking failed ERROR:H:\sw_prj\android-camera2basic-master\caches\transforms-3\140b3353e84ca5c636c59e1e695edb5b\transformed\appcompat-v7-28.0.0\res\values-v28\values-v28.xml:5:5-8:13: AAPT: error: resource android:attr/dialogCornerRadius not found. ? ?? ERROR:H:\sw_prj\android-camera2basic-master\caches\transforms-3\140b3353e84ca5c636c59e1e695edb5b\transformed\appcompat-v7-28.0.0\res\values-v28\values-v28.xml:9:5-12:13: AAPT: error: resource android:attr/dialogCornerRadius not found. ? ?? ERROR:H:\sw_prj\android-camera2basic-master\caches\transforms-3\7172112ab543536ad4df127264db9ed7\transformed\support-compat-28.0.0\res\values\values.xml:57:5-88:25: AAPT: error: resource android:attr/fontVariationSettings not found. ? ?? ERROR:H:\sw_prj\android-camera2basic-master\caches\transforms-3\7172112ab543536ad4df127264db9ed7\transformed\support-compat-28.0.0\res\values\values.xml:57:5-88:25: AAPT: error: resource android:attr/ttcIndex not found. ? ??
解决办法:
?
(36条消息) Android Studio:resource android:attr/dialogCornerRadius not found 出错解决方案_? F. 。 ?的博客-CSDN博客https://blog.csdn.net/weixin_43465451/article/details/83185112
【终于成功了】
Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0. Use '--warning-mode all' to show the individual deprecation warnings. See https://docs.gradle.org/7.0.2/userguide/command_line_interface.html#sec:command_line_warnings
BUILD SUCCESSFUL in 1s
然后,可以编一个APK,放到手机上拍照,这样可以绕过手机里面的各种配置,可以尝试选自己想要的手机镜头和传感器了。?
小米摄像头,CC9PRO,默认第三个摄像头,亲测可以。?
参考:
配置 build ?|? Android 开发者 ?|? Android Developers (google.cn)
mirrors / googlesamples / android-camera2basic · CODE CHINA (gitcode.net)https://gitcode.net/mirrors/googlesamples/android-camera2basic?utm_source=csdn_github_accelerator
(30条消息) Android Camera2官方demo的学习_天下乌鸦不尽黑的博客-CSDN博客_android camera2 demohttps://blog.csdn.net/qq_38164867/article/details/89298933
|