https://github.com/CJT2325/CameraView
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependencies {
implementation 'com.github.xionger0520:flowcamera:V1.0.6'
}
CameraX需要java8环境
android {
...
compileOptions {
sourceCompatibility = 1.8
targetCompatibility = 1.8
}
}
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-feature android:name="android.hardware.location.gps" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
鉴于当前CameraX是alpha版本 测试发现在小米手机上运行有不少兼容性问题
目前建议先使用CustomCameraView
<com.hbzhou.open.flowcamera.CustomCameraView
android:id="@+id/customCamera"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
待CameraX库稳定后可使用以下flow版本
<com.hbzhou.open.flowcamera.FlowCameraView
android:id="@+id/flowCamera"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
Application中实现此接口
若使用CustomCameraView 不需要此配置
class MyApplication : Application(), CameraXConfig.Provider {
override fun onCreate() {
super.onCreate()
}
override fun getCameraXConfig(): CameraXConfig {
return Camera2Config.defaultConfig()
}
}
在fragment或者activity调用 Android6.0以上系统需要自行动态申请 存储 相机和麦克风权限
若使用CustomCameraView注意类型改变 其他初始化操作不变
val flowCamera = findViewById<FlowCameraView>(R.id.flowCamera)
// 绑定生命周期 您就不用关心Camera的开启和关闭了 不绑定无法预览
flowCamera.setBindToLifecycle(this)
// 设置最大可拍摄小视频时长
flowCamera.setRecordVideoMaxTime(10)
// 设置拍照或拍视频回调监听
flowCamera.setFlowCameraListener(object : FlowCameraListener {
// 录制完成视频文件返回
override fun recordSuccess(file: File) {
ToastUtils.showLong(file.absolutePath)
finish()
}
// 操作拍照或录视频出错
override fun onError(videoCaptureError: Int, message: String, cause: Throwable?) {
}
// 拍照返回
override fun captureSuccess(file: File) {
ToastUtils.showLong(file.absolutePath)
finish()
}
})
//左边按钮点击事件
flowCamera.setLeftClickListener {
finish()
}
如果有其他的定制需求可以联系我
wechat: zhouhaibin8357
Copyright 2019 xionger0520
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.