Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
Knightwood committed Apr 26, 2023
1 parent b252cde commit 29a4a1d
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 19 deletions.
44 changes: 34 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

内置了人脸识别,并预留出来了改变分析器使用其他图像分析的方法。

推荐直接把`camerax_lib`集成到项目

* 版本号 [![Tag](https://jitpack.io/v/Knightwood/SimpleCameraX.svg)](https://jitpack.io/#Knightwood/SimpleCameraX)

```
Expand Down Expand Up @@ -99,20 +101,30 @@ fun initPhoto() {
* `NewCameraXFragment`内部创建`CameraHolder`

```
cameraHolder = CameraHolder(
class NewCameraXFragment : Fragment(), CameraCommon {
//activity生成fragment时指定此处的图像分析器提供工具
var outAnalyzer: AnalyzerProvider? = null
........
//相机
cameraHolder = CameraHolder(
page.cameraPreview,
page.graphicOverlayFinder,
cameraConfig,
page.root,
this.captureResultListener
page.root
).apply {
bindLifecycle(requireActivity())//非常重要,绝对不能漏了绑定生命周期
//使用方式 示例代码:
// analyzerProvider=object :AnalyzerProvider{
// override fun provider(verType: VisionType): ImageAnalysis.Analyzer {
// TODO("在这里可以提供其他类型的图像识别器")
// }
// }
if (!this@NewCameraXFragment::faceProcess.isInitialized) {
//初始化默认的面部识别工具
faceProcess =
FaceContourDetectionProcessor(
page.cameraPreview,
page.graphicOverlayFinder,
)
}
//提供图像分析器
analyzerProvider = outAnalyzer
}
//使用changeAnalyzer方法改变camerax使用的图像识别器
// cameraHolder.changeAnalyzer(VisionType.Barcode)
Expand Down Expand Up @@ -153,7 +165,7 @@ fun initPhoto() {
}
override fun onPhotoTaken(filePath: String) {//这里还在测试,参数会
override fun onPhotoTaken(filePath: String) {
Log.d("CameraXFragment", "onPhotoTaken: $filePath")
//图片拍摄后
Expand All @@ -168,6 +180,18 @@ fun initPhoto() {
}
```

人脸检测

`FaceContourDetectionProcessor`文件中配置检测模式。

```
.setPerformanceMode(FaceDetectorOptions.PERFORMANCE_MODE_FAST)//在检测人脸时更注重速度还是准确性,精确模式会检测到比快速模式更少的人脸
.setContourMode(FaceDetectorOptions.CONTOUR_MODE_ALL)//轮廓检测
.setLandmarkMode(FaceDetectorOptions.LANDMARK_MODE_ALL)//面部特征点
.setClassificationMode(FaceDetectorOptions.CLASSIFICATION_MODE_NONE)//是否将人脸分为不同类别(例如“微笑”和“眼睛睁开”)。
.setMinFaceSize(0.6f)//人脸最小占图片的百分比
```



# 示例代码在app目录下。
Expand Down
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

android {
compileSdk 32
compileSdk 33

defaultConfig {
applicationId "com.kiylx.cameraxexample"
Expand Down
7 changes: 4 additions & 3 deletions camerax_lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

android {
compileSdk 32
compileSdk 33

defaultConfig {
minSdk 23
Expand Down Expand Up @@ -70,7 +70,8 @@ dependencies {
implementation 'com.google.android.material:material:1.4.0'

// CameraX core library
def camerax_version = '1.2.0-alpha01'
def camerax_version = '1.2.1'
def mlkit_version = '1.2.0-beta02'

// CameraX Camera2 extensions
api "androidx.camera:camera-camera2:$camerax_version"
Expand All @@ -83,7 +84,7 @@ dependencies {
// If you want to additionally use the CameraX View class
api "androidx.camera:camera-view:$camerax_version"
// If you want to additionally add CameraX ML Kit Vision Integration
api "androidx.camera:camera-mlkit-vision:$camerax_version"
api "androidx.camera:camera-mlkit-vision:$mlkit_version"
// If you want to additionally use the CameraX Extensions library
api "androidx.camera:camera-extensions:$camerax_version"
api 'com.blankj:utilcodex:1.31.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

/**
* 拍照按钮,拍视频动画
* 点击拍照,长按录像
*
*/
public class CaptureButton extends View {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

/**
* 拍照按钮,拍视频动画
* 指定app:captureMode="false" 则点击按钮为录像,否则点击为拍照
*/
public class CaptureButton2 extends View {
// 选择拍照 拍视频 或者都有
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ class FaceContourDetectionProcessor(
* 不会报告检测到的人脸的欧拉 X、欧拉 Y 或欧拉 Z 角度
*/
private val realTimeOpts = FaceDetectorOptions.Builder()
.setPerformanceMode(FaceDetectorOptions.PERFORMANCE_MODE_ACCURATE)//在检测人脸时更注重速度还是准确性,精确模式会检测到比快速模式更少的人脸
.setContourMode(FaceDetectorOptions.CONTOUR_MODE_NONE)//轮廓检测
.setPerformanceMode(FaceDetectorOptions.PERFORMANCE_MODE_FAST)//在检测人脸时更注重速度还是准确性,精确模式会检测到比快速模式更少的人脸
.setContourMode(FaceDetectorOptions.CONTOUR_MODE_ALL)//轮廓检测
.setLandmarkMode(FaceDetectorOptions.LANDMARK_MODE_ALL)//面部特征点
.setClassificationMode(FaceDetectorOptions.CLASSIFICATION_MODE_NONE)//是否将人脸分为不同类别(例如“微笑”和“眼睛睁开”)。
.setMinFaceSize(0.6f)//人脸最小占图片的百分比
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,13 +210,16 @@ abstract class BaseCameraXActivity : BasicActivity(),
page.captureBtn2.visibility = View.GONE
LogUtils.dTag("录制activity", "开始")
cameraXFragment.takeVideo()
//录制视频时隐藏摄像头切换
page.switchBtn.visibility=View.GONE
}

//录制视频结束
override fun recordEnd(time: Long) {
page.captureBtn2.visibility = View.VISIBLE
LogUtils.dTag("录制activity", "停止")
cameraXFragment.stopTakeVideo(time)
page.switchBtn.visibility=View.VISIBLE
}

//长按拍视频的时候,在屏幕滑动可以调整焦距缩放
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class NewCameraXFragment : Fragment(), CameraCommon {
private lateinit var cameraConfig: ManagerConfig
private lateinit var broadcastManager: LocalBroadcastManager

//提供人脸识别
//提供人脸识别,默认的
private lateinit var faceProcess: FaceContourDetectionProcessor

//activity生成fragment时指定此处的图像分析器提供工具
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import android.view.View.OnTouchListener
/**
* 自定义CameraX点击事件
*/
class CameraXPreviewViewTouchListener(context: Context?) : OnTouchListener {
class CameraXPreviewViewTouchListener(context: Context) : OnTouchListener {
private val mGestureDetector: GestureDetector
private var mCustomTouchListener: CustomTouchListener? = null
private val mScaleGestureDetector: ScaleGestureDetector
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@
android:id="@+id/capture_btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:captureMode="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
Expand Down

0 comments on commit 29a4a1d

Please sign in to comment.