使用camera1调试相机预览+获取每一帧数据,
https://www.jianshu.com/p/3440d82545f6
https://www.jianshu.com/p/705d4792e836
主要学习代码,在这里
MainActivity
public class MainActivity extends AppCompatActivity implements Handler.Callback {
private static final int MSG_OPEN_CAMERA = 1;
private static final int MSG_CLOSE_CAMERA = 2;
private static final int MSG_SET_PREVIEW_SIZE = 3;
private static final int MSG_SET_PREVIEW_SURFACE = 4;
private static final int MSG_START_PREVIEW = 5;
private static final int MSG_STOP_PREVIEW = 6;
private static final int MSG_SET_PICTURE_SIZE = 7;
private static final int MSG_TAKE_PICTURE = 8;
private static final String TAG = "MainActivity";
private static final int REQUEST_PERMISSIONS_CODE = 1;
private static final String[] REQUIRED_PERMISSIONS = {Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE};
private static final int PREVIEW_FORMAT = ImageFormat.NV21;
@Nullable
private HandlerThread mCameraThread = null;
@Nullable
private Handler mCameraHandler = null;
@Nullable
private Camera.CameraInfo mFrontCameraInfo = null;
private int mFrontCameraId = -1;
@Nullable
private Camera.CameraInfo mBackCameraInfo = null;
private int mBackCameraId = -1;
@Nullable
private Camera mCamera;
private int mCameraId = -1;
private Camera.CameraInfo mCameraInfo;
@Nullable
private SurfaceHolder mPreviewSurface;
private int mPreviewSurfaceWidth;
private int mPreviewSurfaceHeight;
@Nullable
private DeviceOrientationListener mDeviceOrientationListener;
@Override
public boolean handleMessage(Message msg) {
switch (msg.what) {
case MSG_OPEN_CAMERA: {
openCamera(msg.arg1);
break;
}
case MSG_CLOSE_CAMERA: {
closeCamera();
break;
}
case MSG_SET_PREVIEW_SIZE: {
int shortSide = msg.arg1;
int longSide = msg.arg2;
setPreviewSize(shortSide, longSide);
break;
}
case MSG_SET_PREVIEW_SURFACE: {
SurfaceHolder previewSurface = (SurfaceHolder) msg.obj;
setPreviewSurface(previewSurface);
break;
}
case MSG_START_PREVIEW: {
startPreview();
break;
}
case MSG_STOP_PREVIEW: {
stopPreview();
break;
}
case MSG_SET_PICTURE_SIZE: {
int shortSide = msg.arg1;
int longSide = msg.arg2;
setPictureSize(shortSide, longSide);
break;
}
case MSG_TAKE_PICTURE: {
takePicture();
}
default:
throw new IllegalArgumentException("Illegal message: " + msg.what);
}
return false;
}
private ImageView iv1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDeviceOrientationListener = new DeviceOrientationListener(this);
startCameraThread();
initCameraInfo();
iv1=findViewById(R.id.imageView);
SurfaceView cameraPreview = findViewById(R.id.camera_preview);
cameraPreview.getHolder().addCallback(new PreviewSurfaceCallback());
Button switchCameraButton = findViewById(R.id.switch_camera);
switchCameraButton.setOnClickListener(new OnSwitchCameraButtonClickListener());
Button takePictureButton = findViewById(R.id.take_picture);
takePictureButton.setOnClickListener(new OnTakePictureButtonClickListener());
}
@Override
protected void onStart() {
super.onStart();
DeviceOrientationListener deviceOrientationListener = mDeviceOrientationListener;
if (deviceOrientationListener != null) {
deviceOrientationListener.enable();
}
}
@Override
protected void onResume() {
super.onResume();
// 动态权限检查
if (!isRequiredPermissionsGranted() && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(REQUIRED_PERMISSIONS, REQUEST_PERMISSIONS_CODE);
} else if (mCameraHandler != null) {
mCameraHandler.obtainMessage(MSG_OPEN_CAMERA, getCameraId(), 0).sendToTarget();
}
}
@Override
protected void onPause() {
super.onPause();
if (mCameraHandler != null) {
mCameraHandler.removeMessages(MSG_OPEN_CAMERA);
mCameraHandler.sendEmptyMessage(MSG_CLOSE_CAMERA);
}
}
@Override
protected void onStop() {
super.onStop();
DeviceOrientationListener deviceOrientationListe