Reputation: 55
I need to hide the camera shooting button if no face has been detected. I have been trying this for three days. I couldn't find a way to take action when face is detected.
Note: this code is working just fine I just need to add face detector how and where? And then use this face detector to take action on the camera shooting button
btn_shot.setVisibility(view.GONE)
Here is creating the Camera preview:
private void creatCameraPreview() throws CameraAccessException {
SurfaceTexture texture = textureView.getSurfaceTexture();
texture.setDefaultBufferSize(imageDimensions.getWidth(),
imageDimensions.getHeight());
Surface surface = new Surface(texture);
captureRequestBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
captureRequestBuilder.addTarget(surface);
cameraDevice.createCaptureSession(Arrays.asList(surface), new CameraCaptureSession.StateCallback() {
@Override
public void onConfigured(@NonNull CameraCaptureSession session) {
if (cameraDevice == null)
return;
cameraCaptureSession = session;
try {
updatePreview();
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
@Override
public void onConfigureFailed(@NonNull CameraCaptureSession session) {
Toast.makeText(CameraTaken.this, "Configuration Changed", Toast.LENGTH_SHORT).show();
}
}, null);
}
Here is opening the Camera:
private void openCamera() throws CameraAccessException {
CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
assert manager != null;
cameraId = manager.getCameraIdList()[0]; //[0] for the back facing camera [1] for the front facing camera
CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);
StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
assert map != null;
imageDimensions = map.getOutputSizes(SurfaceTexture.class)[0];
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.INTERNET) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(CameraTaken.this, new String[]{
Manifest.permission.CAMERA,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.INTERNET}, 101);
return;
}
manager.openCamera(cameraId, stateCallBack, null);
}
Upvotes: 1
Views: 152
Reputation: 18147
You can try using the face detector in the camera API, available on most devices: https://developer.android.com/reference/android/hardware/camera2/CaptureRequest#STATISTICS_FACE_DETECT_MODE to turn it on, https://developer.android.com/reference/android/hardware/camera2/CaptureResult#STATISTICS_FACES to read if any faces are detected.
Upvotes: 1