Skip to content

Commit

Permalink
Auto focus and exposure settings
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidGillsjo committed Sep 23, 2020
1 parent a4a2f34 commit 08a44fa
Show file tree
Hide file tree
Showing 8 changed files with 551 additions and 134 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,6 @@ lint/tmp/

#Python
__pycache__

#Release
android_app/app/release
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,9 @@

import androidx.preference.PreferenceManager;
import android.util.Log;
import android.util.Range;
import android.util.Size;
import android.view.Surface;

import java.util.ArrayList;
import java.util.Arrays;

import static java.lang.Math.abs;
Expand Down Expand Up @@ -181,63 +179,6 @@ public void setPreviewSurfaceTexture(SurfaceTexture surfaceTexture) {
mPreviewSurfaceTexture = surfaceTexture;
}


private class NumExpoIso {
public Long mNumber;
public Long mExposureNanos;
public Integer mIso;

public NumExpoIso(Long number, Long expoNanos, Integer iso) {
mNumber = number;
mExposureNanos = expoNanos;
mIso = iso;
}
}

private final int kMaxExpoSamples = 10;
private ArrayList<NumExpoIso> expoStats = new ArrayList<>(kMaxExpoSamples);

private void setExposureAndIso() {
Long exposureNanos = CameraCaptureActivity.mDesiredExposureTime;
Long desiredIsoL = 30L * 30000000L / exposureNanos;
Integer desiredIso = desiredIsoL.intValue();
if (!expoStats.isEmpty()) {
int index = expoStats.size() / 2;
Long actualExpo = expoStats.get(index).mExposureNanos;
Integer actualIso = expoStats.get(index).mIso;
if (actualExpo <= exposureNanos) {
exposureNanos = actualExpo;
desiredIso = actualIso;
} else {
desiredIsoL = actualIso * actualExpo / exposureNanos;
desiredIso = desiredIsoL.intValue();
}
}

// fix exposure
mPreviewRequestBuilder.set(
CaptureRequest.CONTROL_AE_MODE, CameraMetadata.CONTROL_AE_MODE_OFF);
Range<Long> exposureTimeRange = mCameraCharacteristics.get(
CameraCharacteristics.SENSOR_INFO_EXPOSURE_TIME_RANGE);
if (exposureTimeRange != null) {
Log.d(TAG, "exposure time range " + exposureTimeRange.toString());
}

mPreviewRequestBuilder.set(
CaptureRequest.SENSOR_EXPOSURE_TIME, exposureNanos);
Log.d(TAG, "Exposure time set to " + exposureNanos);

// fix ISO
Range<Integer> isoRange = mCameraCharacteristics.get(
CameraCharacteristics.SENSOR_INFO_SENSITIVITY_RANGE);
if (isoRange != null) {
Log.d(TAG, "ISO range " + isoRange.toString());
}

mPreviewRequestBuilder.set(CaptureRequest.SENSOR_SENSITIVITY, desiredIso);
Log.d(TAG, "ISO set to " + desiredIso);
}

private void initPreviewRequest() {
try {
mPreviewRequestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_RECORD);
Expand All @@ -248,13 +189,6 @@ private void initPreviewRequest() {
mPreviewRequestBuilder.set(
CaptureRequest.CONTROL_AWB_MODE, CameraMetadata.CONTROL_AWB_MODE_AUTO);

// fix focus distance
mPreviewRequestBuilder.set(
CaptureRequest.CONTROL_AF_MODE, CameraMetadata.CONTROL_AF_MODE_OFF);
mPreviewRequestBuilder.set(
CaptureRequest.LENS_FOCUS_DISTANCE, 0.0f);
Log.d(TAG, "Focus distance set to Infinity");

mCameraSettingsManager.updateRequestBuilder(mPreviewRequestBuilder);

if (mPreviewSurfaceTexture != null && mPreviewSurface == null) { // use texture view
Expand Down Expand Up @@ -359,14 +293,7 @@ public void onCaptureCompleted(CameraCaptureSession session,

}


Long number = result.getFrameNumber();
Long exposureTimeNs = result.get(CaptureResult.SENSOR_EXPOSURE_TIME);
Integer iso = result.get(CaptureResult.SENSOR_SENSITIVITY);
if (expoStats.size() > kMaxExpoSamples) {
expoStats.subList(0, kMaxExpoSamples / 2).clear();
}
expoStats.add(new NumExpoIso(number, exposureTimeNs, iso));

Float fl = result.get(CaptureResult.LENS_FOCAL_LENGTH);

Expand Down Expand Up @@ -510,6 +437,9 @@ private void logAnalyticsConfig() {
}

void changeManualFocusPoint(float eventX, float eventY, int viewWidth, int viewHeight) {
if (!mCameraSettingsManager.focusOnTouch()) {
return;
}
final int y = (int) ((eventX / (float) viewWidth) * (float) sensorArraySize.height());
final int x = (int) ((eventY / (float) viewHeight) * (float) sensorArraySize.width());
final int halfTouchWidth = 400;
Expand All @@ -528,8 +458,6 @@ void changeManualFocusPoint(float eventX, float eventY, int viewWidth, int viewH
e.printStackTrace();
}

setExposureAndIso();

mPreviewRequestBuilder.set(CaptureRequest.CONTROL_MODE,
CameraMetadata.CONTROL_MODE_AUTO);
mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,6 @@ public class CameraCaptureActivity extends AppCompatActivity {
public static final String TAG = "VIMUC-Main";
private static final boolean VERBOSE = false;

static final Long mDesiredExposureTime = 5000000L; // nanoseconds

private Camera2Proxy mCamera2Proxy = null;
private CameraHandler mCameraHandler;
private CameraCaptureFragment mCameraCaptureFragment;
Expand Down
Loading

0 comments on commit 08a44fa

Please sign in to comment.