Skip to content

Commit

Permalink
fix setZoom motion with camera2
Browse files Browse the repository at this point in the history
  • Loading branch information
pedroSG94 committed Sep 11, 2024
1 parent 626c2da commit 168ce4b
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ public int getMaxZoom() {
public int getMinZoom() { return 0; }

public void setZoom(MotionEvent event) {
if (event.getPointerCount() < 2 || event.getAction() != MotionEvent.ACTION_MOVE) return;
try {
if (camera != null && running && camera.getParameters() != null && camera.getParameters()
.isZoomSupported()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -723,23 +723,16 @@ class Camera2ApiManager(context: Context) : CameraDevice.StateCallback() {
}
}

fun setZoom(event: MotionEvent) {
val currentFingerSpacing: Float
if (event.pointerCount > 1) {
currentFingerSpacing = CameraHelper.getFingerSpacing(event)
val delta = 0.1f
if (fingerSpacing != 0f) {
var newLevel = zoomLevel
if (currentFingerSpacing > fingerSpacing) {
newLevel += delta
} else if (currentFingerSpacing < fingerSpacing) {
newLevel -= delta
}
//This method avoid out of range
zoomLevel = newLevel
}
fingerSpacing = currentFingerSpacing
}
@JvmOverloads
fun setZoom(event: MotionEvent, delta: Float = 0.1f) {
if (event.pointerCount < 2 || event.action != MotionEvent.ACTION_MOVE) return
val currentFingerSpacing = CameraHelper.getFingerSpacing(event)
if (currentFingerSpacing > fingerSpacing) {
zoom += delta
} else if (currentFingerSpacing < fingerSpacing) {
zoom -= delta
}
fingerSpacing = currentFingerSpacing
}

fun stopRepeatingEncoder() {
Expand Down
4 changes: 4 additions & 0 deletions library/src/main/java/com/pedro/library/base/Camera2Base.java
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,10 @@ public void setZoom(MotionEvent event) {
cameraManager.setZoom(event);
}

public void setZoom(MotionEvent event, float delta) {
cameraManager.setZoom(event, delta);
}

/**
* @Experimental
* @return optical zoom values available
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,9 @@ class Camera2Source(context: Context): VideoSource() {
return camera.tapToFocus(event)
}

fun setZoom(event: MotionEvent) {
if (isRunning()) camera.setZoom(event)
@JvmOverloads
fun setZoom(event: MotionEvent, delta: Float = 0.1f) {
if (isRunning()) camera.setZoom(event, delta)
}

fun setZoom(level: Float) {
Expand Down

0 comments on commit 168ce4b

Please sign in to comment.