安卓实现surfaceview包含圆形采集人脸的框
时间: 2023-06-05 16:01:27 浏览: 594
Android圆形相机预览窗口,圆形SurfaceView
4星 · 用户满意度95%
在安卓中实现一个包含圆形采集人脸的框的SurfaceView可以通过以下步骤实现:
第一步,引入OpenCV库,因为OpenCV库提供了丰富的人脸检测、处理等相关函数。需要在build.gradle中进行导入。
第二步,创建自定义SurfaceView,并重写其surfaceCreated()方法。在这个方法中,指定摄像头预览画面的格式、宽、高等参数,并打开摄像头进行预览渲染。
第三步,使用OpenCV提供的人脸检测算法,重写SurfaceView的onDraw()方法,在该方法中绘制圆形框,并将人脸部分截取下来。
第四步,添加适当的控件,如Button、TextView等来控制人脸采集的触发和展示。
具体步骤如下:
1.在build.gradle文件中引入OpenCV库
```
dependencies {
implementation 'org.opencv:opencv-android:4.5.3'
}
```
2.创建自定义SurfaceView并打开摄像头
```
public class CameraSurfaceView extends SurfaceView implements SurfaceHolder.Callback {
...
public CameraSurfaceView(Context context) {
super(context);
init(context);
}
public CameraSurfaceView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
private void init(Context context) {
...
getHolder().addCallback(this);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
camera = Camera.open();
Camera.Parameters parameters = camera.getParameters();
Camera.Size previewSize = parameters.getPreviewSize(); // 获取预览画面的尺寸
parameters.setPreviewFormat(ImageFormat.NV21); // 设置预览画面的格式
camera.setParameters(parameters);
try {
camera.setPreviewDisplay(holder);
camera.setPreviewCallback(new Camera.PreviewCallback() {
@Override
public void onPreviewFrame(byte[] data, Camera camera) {
...
}
});
camera.startPreview();
} catch (IOException e) {
e.printStackTrace();
}
}
...
}
```
3.使用OpenCV检测人脸并绘制圆形框
```
public class CameraSurfaceView extends SurfaceView implements SurfaceHolder.Callback {
...
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (cameraData != null) {
Mat yuvMat = new Mat(height + height / 2, width, CvType.CV_8UC1);
yuvMat.put(0, 0, cameraData);
Mat bgrMat = new Mat();
Imgproc.cvtColor(yuvMat, bgrMat, Imgproc.COLOR_YUV2BGR_NV21);
Mat grayMat = new Mat();
Imgproc.cvtColor(bgrMat, grayMat, Imgproc.COLOR_BGR2GRAY);
MatOfRect faces = new MatOfRect();
CascadeClassifier cascadeClassifier = new CascadeClassifier(haarcascadeFile.getAbsolutePath());
cascadeClassifier.detectMultiScale(grayMat, faces, 1.1, 2, 0, new Size(), new Size());
Rect[] facesArray = faces.toArray();
if (facesArray.length > 0) {
Rect faceRect = facesArray[0];
Point center = new Point(faceRect.x + faceRect.width / 2, faceRect.y + faceRect.height / 2);
canvas.drawCircle((float) center.x, (float) center.y, faceRect.width / 2, paint);
}
}
}
...
}
```
4.添加控件进行人脸采集和展示
```
public class MainActivity extends AppCompatActivity {
...
private CameraSurfaceView cameraSurfaceView;
private Button captureBtn;
private ImageView faceView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cameraSurfaceView = findViewById(R.id.camera_surface_view);
captureBtn = findViewById(R.id.capture_btn);
faceView = findViewById(R.id.face_view);
captureBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bitmap faceBitmap = cameraSurfaceView.getFaceBitmap();
if (faceBitmap != null) {
faceView.setImageBitmap(faceBitmap);
}
}
});
}
...
}
```
阅读全文