Skip to content

Commit

Permalink
set autofocus enabled on taptofocus and return result in taptofocus, …
Browse files Browse the repository at this point in the history
…enable and disable autofocus
  • Loading branch information
pedroSG94 committed Jul 10, 2024
1 parent 1f64bc0 commit 77bac3c
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -585,18 +585,16 @@ public void disableLantern() {
}
}

private Camera.AutoFocusCallback autoFocusTakePictureCallback = new Camera.AutoFocusCallback() {
@Override
public void onAutoFocus(boolean success, Camera camera) {
if (success) {
Log.i(TAG, "tapToFocus success");
} else {
Log.e(TAG, "tapToFocus failed");
}
private final Camera.AutoFocusCallback autoFocusTakePictureCallback = (success, camera) -> {
if (success) {
Log.i(TAG, "tapToFocus success");
} else {
Log.e(TAG, "tapToFocus failed");
}
};

public void tapToFocus(View view, MotionEvent event) {
public boolean tapToFocus(View view, MotionEvent event) {
boolean result = false;
if (camera != null && camera.getParameters() != null) {
Camera.Parameters parameters = camera.getParameters();
if (parameters.getMaxNumMeteringAreas() > 0) {
Expand All @@ -607,15 +605,19 @@ public void tapToFocus(View view, MotionEvent event) {
parameters.setFocusAreas(meteringAreas);
try {
camera.setParameters(parameters);
autoFocusEnabled = true;
result = true;
}catch (Exception e) {
Log.i(TAG, "tapToFocus error: " + e.getMessage());
}
}
camera.autoFocus(autoFocusTakePictureCallback);
}
return result;
}

public void enableAutoFocus() {
public boolean enableAutoFocus() {
boolean result = false;
if (camera != null) {
Camera.Parameters parameters = camera.getParameters();
List<String> supportedFocusModes = parameters.getSupportedFocusModes();
Expand All @@ -632,10 +634,13 @@ public void enableAutoFocus() {
}
}
camera.setParameters(parameters);
result = autoFocusEnabled;
}
return result;
}

public void disableAutoFocus() {
public boolean disableAutoFocus() {
boolean result = false;
if (camera != null) {
Camera.Parameters parameters = camera.getParameters();
List<String> supportedFocusModes = parameters.getSupportedFocusModes();
Expand All @@ -648,7 +653,9 @@ public void disableAutoFocus() {
}
autoFocusEnabled = false;
camera.setParameters(parameters);
result = true;
}
return result;
}

public boolean isAutoFocusEnabled() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -555,15 +555,16 @@ public int getMinExposure() {
return 0;
}

public void tapToFocus(MotionEvent event) {
public boolean tapToFocus(MotionEvent event) {
boolean result = false;
CameraCharacteristics characteristics = getCameraCharacteristics();
if (characteristics == null) return;
if (characteristics == null) return false;
int pointerId = event.getPointerId(0);
int pointerIndex = event.findPointerIndex(pointerId);
// Get the pointer's current position
float x = event.getX(pointerIndex);
float y = event.getY(pointerIndex);
if (x < 100 || y < 100) return;
if (x < 100 || y < 100) return false;

Rect touchRect = new Rect((int) (x - 100), (int) (y - 100),
(int) (x + 100), (int) (y + 100));
Expand All @@ -581,10 +582,13 @@ public void tapToFocus(MotionEvent event) {
builderInputSurface.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_START);
cameraCaptureSession.setRepeatingRequest(builderInputSurface.build(),
faceDetectionEnabled ? cb : null, null);
autoFocusEnabled = true;
result = true;
} catch (Exception e) {
Log.e(TAG, "Error", e);
}
}
return result;
}

/**
Expand Down Expand Up @@ -662,9 +666,10 @@ public void disableLantern() {
}
}

public void enableAutoFocus() {
public boolean enableAutoFocus() {
boolean result = false;
CameraCharacteristics characteristics = getCameraCharacteristics();
if (characteristics == null) return;
if (characteristics == null) return false;
int[] supportedFocusModes =
characteristics.get(CameraCharacteristics.CONTROL_AF_AVAILABLE_MODES);
if (supportedFocusModes != null) {
Expand Down Expand Up @@ -697,16 +702,19 @@ public void enableAutoFocus() {
autoFocusEnabled = false;
}
}
result = autoFocusEnabled;
} catch (Exception e) {
Log.e(TAG, "Error", e);
}
}
}
return result;
}

public void disableAutoFocus() {
public boolean disableAutoFocus() {
boolean result = false;
CameraCharacteristics characteristics = getCameraCharacteristics();
if (characteristics == null) return;
if (characteristics == null) return false;
int[] supportedFocusModes =
characteristics.get(CameraCharacteristics.CONTROL_AF_AVAILABLE_MODES);
if (supportedFocusModes != null) {
Expand All @@ -719,14 +727,15 @@ public void disableAutoFocus() {
cameraCaptureSession.setRepeatingRequest(builderInputSurface.build(),
faceDetectionEnabled ? cb : null, null);
autoFocusEnabled = false;
return;
return true;
}
} catch (Exception e) {
Log.e(TAG, "Error", e);
}
}
}
}
return result;
}

public boolean isAutoFocusEnabled() {
Expand Down
12 changes: 6 additions & 6 deletions library/src/main/java/com/pedro/library/base/Camera1Base.java
Original file line number Diff line number Diff line change
Expand Up @@ -240,12 +240,12 @@ public boolean isLanternEnabled() {
return cameraManager.isLanternEnabled();
}

public void enableAutoFocus() {
cameraManager.enableAutoFocus();
public boolean enableAutoFocus() {
return cameraManager.enableAutoFocus();
}

public void disableAutoFocus() {
cameraManager.disableAutoFocus();
public boolean disableAutoFocus() {
return cameraManager.disableAutoFocus();
}

public boolean isAutoFocusEnabled() {
Expand Down Expand Up @@ -866,8 +866,8 @@ public int getMinExposure() {
return cameraManager.getMinExposure();
}

public void tapToFocus(View view, MotionEvent event) {
cameraManager.tapToFocus(view, event);
public boolean tapToFocus(View view, MotionEvent event) {
return cameraManager.tapToFocus(view, event);
}

public GlInterface getGlInterface() {
Expand Down
12 changes: 6 additions & 6 deletions library/src/main/java/com/pedro/library/base/Camera2Base.java
Original file line number Diff line number Diff line change
Expand Up @@ -278,12 +278,12 @@ public boolean isLanternSupported() {
return cameraManager.isLanternSupported();
}

public void enableAutoFocus() {
cameraManager.enableAutoFocus();
public boolean enableAutoFocus() {
return cameraManager.enableAutoFocus();
}

public void disableAutoFocus() {
cameraManager.disableAutoFocus();
public boolean disableAutoFocus() {
return cameraManager.disableAutoFocus();
}

public boolean isAutoFocusEnabled() {
Expand Down Expand Up @@ -904,8 +904,8 @@ public int getMinExposure() {
return cameraManager.getMinExposure();
}

public void tapToFocus(MotionEvent event) {
cameraManager.tapToFocus(event);
public boolean tapToFocus(MotionEvent event) {
return cameraManager.tapToFocus(event);
}

public GlInterface getGlInterface() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,22 +130,22 @@ class Camera1Source(context: Context): VideoSource() {
return if (isRunning()) camera.isLanternEnabled else false
}

fun enableAutoFocus() {
if (isRunning()) {
camera.enableAutoFocus()
}
fun enableAutoFocus(): Boolean {
if (isRunning()) return camera.enableAutoFocus()
return false
}

fun disableAutoFocus() {
if (isRunning()) camera.disableAutoFocus()
fun disableAutoFocus(): Boolean {
if (isRunning()) return camera.disableAutoFocus()
return false
}

fun isAutoFocusEnabled(): Boolean {
return if (isRunning()) camera.isAutoFocusEnabled else false
}

fun tapToFocus(view: View, event: MotionEvent) {
camera.tapToFocus(view, event)
fun tapToFocus(view: View, event: MotionEvent): Boolean {
return camera.tapToFocus(view, event)
}

fun setZoom(event: MotionEvent) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,22 +129,22 @@ class Camera2Source(context: Context): VideoSource() {
return if (isRunning()) camera.isLanternEnabled else false
}

fun enableAutoFocus() {
if (isRunning()) {
camera.enableAutoFocus()
}
fun enableAutoFocus(): Boolean {
if (isRunning()) return camera.enableAutoFocus()
return false
}

fun disableAutoFocus() {
if (isRunning()) camera.disableAutoFocus()
fun disableAutoFocus(): Boolean {
if (isRunning()) return camera.disableAutoFocus()
return false
}

fun isAutoFocusEnabled(): Boolean {
return if (isRunning()) camera.isAutoFocusEnabled else false
}

fun tapToFocus(event: MotionEvent) {
camera.tapToFocus(event)
fun tapToFocus(event: MotionEvent): Boolean {
return camera.tapToFocus(event)
}

fun setZoom(event: MotionEvent) {
Expand Down

0 comments on commit 77bac3c

Please sign in to comment.