添加权限
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera2" />
逻辑实现
import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ImageFormat;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.graphics.SurfaceTexture;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCaptureSession;
import android.hardware.camera2.CameraCharacteristics;
import android.hardware.camera2.CameraDevice;
import android.hardware.camera2.CameraManager;
import android.hardware.camera2.CameraMetadata;
import android.hardware.camera2.CaptureRequest;
import android.hardware.camera2.CaptureResult;
import android.hardware.camera2.TotalCaptureResult;
import android.hardware.camera2.params.Face;
import android.hardware.camera2.params.StreamConfigurationMap;
import android.media.Image;
import android.media.ImageReader;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import android.util.Size;
import android.util.SparseIntArray;
import android.view.Surface;
import android.view.TextureView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.yl.fecedetectdemo.R;
import java.nio.ByteBuffer;
import java.util.Arrays;
public class MainActivity extends Activity {
private static void Log(String message) {
Log.i(MainActivity.class.getName(), message);
}
//为了使照片竖直显示
private static final SparseIntArray ORIENTATIONS = new SparseIntArray();
static {
ORIENTATIONS.append(Surface.ROTATION_0, 90);
ORIENTATIONS.append(Surface.ROTATION_90, 0);
ORIENTATIONS.append(Surface.ROTATION_180, 270);
ORIENTATIONS.append(Surface.ROTATION_270, 180);
}
private TextureView cView;//用于相机预览
private TextureView rView;//用于标注人脸
private ImageView imageView;//拍照照片显示
private TextView textView;
private Button btnFront;
private Button btnBack;
private Button btnClose;
private Button btnCapture;
private Surface previewSurface;//预览Surface
private ImageReader cImageReader;
private Surface captureSurface;//拍照Surface
HandlerThread cHandlerThread;//相机处理线程
Handler cHandler;//相机处理
CameraDevice cDevice;
CameraCaptureSession cSession;
CameraDevice.StateCallback cDeviceOpenCallback = null;//相机开启回调
CaptureRequest.Builder previewRequestBuilder;//预览请求构建
CaptureRequest previewRequest;//预览请求
CameraCaptureSession.CaptureCallback previewCallback;//预览回调
CaptureRequest.Builder captureRequestBuilder;
CaptureRequest captureRequest;
CameraCaptureSession.CaptureCallback captureCallback;
int[] faceDetectModes;
// Rect rRect;//相机成像矩形
Size cPixelSize;//相机成像尺寸
int cOrientation;
Size captureSize;
boolean isFront;
Paint pb;
Bitmap bitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_face);
//GlobalExceptionHandler catchHandler = GlobalExceptionHandler.getInstance();
//catchHandler.init(this.getApplication());
initVIew();
}
/**
* 初始化界面
*/
private void initVIew() {
cView = (TextureView) findViewById(R.id.cView);
rView = (TextureView) findViewById(R.id.rView);
imageView = (ImageView) findViewById(R.id.imageView);
textView = (TextView) findViewById(R.id.textView);
btnFront = (Button) findViewById(R.id.btnFront);
btnBack = (Button) findViewById(R.id.btnBack);
btnClose = (Button) findViewById(R.id.btnClose);
btnCapture = (Button) findViewById(R.id.btnCapture);
//隐藏背景色,以免标注人脸时挡住预览画面
rView.setAlpha(0.9f);
btnFront.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openCamera(true);
}
});
btnBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openCamera(false);
}
});
btnClose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
closeCamera();
}
});
btnCapture.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
executeCapture();
}
});
//TODO 摄像头静音尝试
// try {
// Class<?> cClass = Class.forName("android.hardware.Camera");
// Method mOpen = cClass.getDeclaredMethod("open");
// Object nCamera = mOpen.invoke(null);
// Method mDisableShutterSound = cClass.getDeclaredMethod("dis