android开发camera自动人脸,Android:Camera2开发详解(下):实现人脸检测功能并实时显示人脸框...

331af6dc2772?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

android.jpg

前言

本篇文章是在上篇文章的基础之上,在预览的时使用Camera2自带的人脸检测功能实时检测人脸位置,并通过一个自定义view显示在预览画面上

实现思路

布局中使用 AutoFitTextureView 代替 TextureView。AutoFitTextureView 继承自 TextureView,能够根据传入的宽高值调整自身大小。目的是使预览画面不变形,否则在人脸坐标转换的时候会出现比较大的误差,这个后文中会提到

在创建预览会话的时候,开启人脸检测

在预览会话的状态回调中可以得到检测到的人脸信息

将检测到的人脸坐标进行相应的转换,并传递给FaceView

自定义一个FaceView,接收人脸位置并实时绘制出来

具体实现步骤

注: 由于本文是在上篇文章基础之上,故省略了很多相同的代码,完整代码在文末给出

一、定义一个AutoFitTextureView,并在布局中使用

/**

* A {@link TextureView} that can be adjusted to a specified aspect ratio.

*/

public class AutoFitTextureView extends TextureView {

private int mRatioWidth = 0;

private int mRatioHeight = 0;

public AutoFitTextureView(Context context) {

this(context, null);

}

public AutoFitTextureView(Context context, AttributeSet attrs) {

this(context, attrs, 0);

}

public AutoFitTextureView(Context context, AttributeSet attrs, int defStyle) {

super(context, attrs, defStyle);

}

/**

* Sets the aspect ratio for this view. The size of the view will be measured based on the ratio

* calculated from the parameters. Note that the actual sizes of parameters don't matter, that

* is, calling setAspectRatio(2, 3) and setAspectRatio(4, 6) make the same result.

*

* @param width Relative horizontal size

* @param height Relative vertical size

*/

public void setAspectRatio(int width, int height) {

if (width < 0 || height < 0) {

throw new IllegalArgumentException("Size cannot be negative.");

}

mRatioWidth = width;

mRatioHeight = height;

requestLayout();

}

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

super.onMeasure(widthMeasureSpec, heightMeasureSpec);

int width = MeasureSpec.getSize(widthMeasureSpec);

int height = MeasureSpec.getSize(heightMeasureSpec);

if (0 == mRatioWidth || 0 == mRatioHeight) {

setMeasuredDimension(width, height);

} else {

if (width < height * mRatioWidth / mRatioHeight) {

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android Camera2人脸检测是一种基于Android平台的人脸识别技术,在相机应用程序中运用的一种高级功能。 相比于以前的Camera API,Camera2 API为Android设备的相机功能提供了更高级的控制和更丰富的特性。其中一个重要的特性就是人脸检测功能人脸检测是一种计算机图像处理技术,用于在图像或视频中自动检测人脸并识别其特征。Android Camera2 API通过使用人脸检测器(FaceDetector)类来实现这一功能。 使用Camera2 API进行人脸检测的一般步骤如下: 1. 创建一个CameraCaptureSession,用于图像捕获和展示。 2. 创建一个CaptureRequest.Builder对象,配置相机设备的请求参数。 3. 创建一个SurfaceTexture或SurfaceView,用于相机预览。 4. 使用相机设备创建一个CaptureRequest对象,指定捕获的图像格式和目标Surface。 5. 创建一个设置面部检测的FaceDetector对象,并设置相应的参数。 6. 将FaceDetector应用于捕获的图像,并获得检测到的人脸信息。 7. 根据人脸的位置和特征,在相机预览上绘制相应的标记或框。 人脸检测不仅可以用于提高相片的质量,还可以应用于人脸识别、人脸解锁等领域。利用人脸检测功能开发者可以开发出更多有趣和有用的相机应用程序。 需要注意的是,Android Camera2 API中的人脸检测功能可能因设备的不同而有所不同,可能会受到硬件和软件的限制。因此,在开发应用程序或使用该功能时,需要仔细考虑设备的兼容性和性能问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值