前言
我们使用OpenCV+自己的人脸检测算法的时候,在视频图像中绘制出人脸框,会方便我们的开发和调试过程。
绘制人脸框的过程有:
- 打开摄像头,循环取帧数据
- 转为jpg格式,方便传输
- 人脸检测得到人脸框数据
- 按坐标填涂到Mat上
- 将Mat绘制到窗口
视频采集线程
private class ProcessThread extends Thread {
private VideoCapture mVideoCapture = null;
public static final int CAMERA_WIDTH = 640;
public static final int CAMERA_HEIGHT = 480;
// 存放jpg格式的数据
private MatOfByte mJpegMatOfByte = null;
// 人脸框着色,红色
private static final byte[] DRAW_COLOR = new byte[] {
(byte) 0,
(byte) 0,
(byte) 255
};
public ProcessThread() {
// 打开摄像头
mVideoCapture = new VideoCapture(0);
//mVideoCapture.set(Videoio.CAP_PROP_FRAME_WIDTH, 1280);
//mVideoCapture.set(Videoio.CAP_PROP_FRAME_HEIGHT, 720);
HighGui.namedWindow("采集");
}
@Override
public void run() {
// TODO Auto-generated method stub
while (true) {
Mat _imgMat = new Mat();
// 读取摄像头数据
if (!mVideoCapture.read(_imgMat)) {
Log.e(TAG, "ProcessThread: read ERROR");
_imgMat.release();
continue;
}
// 转为jpg格式,便于传输
mJpegMatOfByte = new MatOfByte();
Imgcodecs.imencode(".jpg", _imgMat, mJpegMatOfByte);
byte[] _jpegData = mJpegMatOfByte.toArray();
if (false) {
try (BufferedOutputStream _outputStream = new BufferedOutputStream(new FileOutputStream