Skip to content

Commit

Permalink
refactor camera1 setZoom
Browse files Browse the repository at this point in the history
  • Loading branch information
pedroSG94 committed Sep 14, 2024
1 parent 944f50f commit fb5b490
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ public class Camera1ApiManager implements Camera.PreviewCallback, Camera.FaceDet
private int height = 480;
private int fps = 30;
private int rotation = 0;
private int imageFormat = ImageFormat.NV21;
private final int imageFormat = ImageFormat.NV21;
private byte[] yuvBuffer;
private List<Camera.Size> previewSizeBack;
private List<Camera.Size> previewSizeFront;
private float distance;
private float fingerSpacing;
private CameraCallbacks cameraCallbacks;
private final int focusAreaSize = 100;

Expand Down Expand Up @@ -234,8 +234,7 @@ public void setZoom(int level) {
.isZoomSupported()) {
android.hardware.Camera.Parameters params = camera.getParameters();
int maxZoom = params.getMaxZoom();
if (level > maxZoom) level = maxZoom;
else if (level < getMinZoom()) level = getMinZoom();
level = Math.max(Math.min(maxZoom, level), getMinZoom());
params.setZoom(level);
camera.setParameters(params);
}
Expand Down Expand Up @@ -277,32 +276,18 @@ 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()) {
android.hardware.Camera.Parameters params = camera.getParameters();
int maxZoom = params.getMaxZoom();
int zoom = params.getZoom();
float newDist = CameraHelper.getFingerSpacing(event);

if (newDist > distance) {
if (zoom < maxZoom) {
zoom++;
}
} else if (newDist < distance) {
if (zoom > 0) {
zoom--;
}
}
setZoom(event, 1);
}

distance = newDist;
params.setZoom(zoom);
camera.setParameters(params);
}
} catch (Exception e) {
Log.e(TAG, "Error", e);
public void setZoom(MotionEvent event, int delta) {
if (event.getPointerCount() < 2 || event.getAction() != MotionEvent.ACTION_MOVE) return;
float currentFingerSpacing = CameraHelper.getFingerSpacing(event);
if (currentFingerSpacing > fingerSpacing) {
setZoom(getZoom() + delta);
} else if (currentFingerSpacing < fingerSpacing) {
setZoom(getZoom() - delta);
}
fingerSpacing = currentFingerSpacing;
}

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

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

/**
* Set zoomIn or zoomOut to camera.
* Use this method if you use a zoom slider.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,9 @@ class Camera1Source(context: Context): VideoSource() {
return camera.tapToFocus(view, event)
}

fun setZoom(event: MotionEvent) {
if (isRunning()) camera.setZoom(event)
@JvmOverloads
fun setZoom(event: MotionEvent, delta: Int = 1) {
if (isRunning()) camera.setZoom(event, delta)
}

fun setZoom(level: Int) {
Expand Down

0 comments on commit fb5b490

Please sign in to comment.