Skip to content

Commit

Permalink
CameraAPI: fix camera issue, and cleanup after taking a photo
Browse files Browse the repository at this point in the history
Fixes #124 and #435.
  • Loading branch information
povidalo authored and Grimler91 committed Dec 5, 2021
1 parent 92b4cd8 commit a5f7d14
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions app/src/main/java/com/termux/api/PhotoAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,11 @@ public void run() {
} catch (Exception e) {
stdout.println("Error writing image: " + e.getMessage());
TermuxApiLogger.error("Error writing image", e);
} finally {
closeCamera(camera, looper);
}
} finally {
mImageReader.close();
releaseSurfaces(outputSurfaces);
closeCamera(camera, looper);
}
}
}.start(), null);
Expand Down Expand Up @@ -156,8 +158,6 @@ public void onConfigured(final CameraCaptureSession session) {
Thread.sleep(500);
session.stopRepeating();
TermuxApiLogger.info("preview stoppend");
previewTexture.release();
dummySurface.release();

final CaptureRequest.Builder jpegRequest = camera.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
// Render to our image reader:
Expand All @@ -170,13 +170,17 @@ public void onConfigured(final CameraCaptureSession session) {
saveImage(camera, session, jpegRequest.build());
} catch (Exception e) {
TermuxApiLogger.error("onConfigured() error in preview", e);
mImageReader.close();
releaseSurfaces(outputSurfaces);
closeCamera(camera, looper);
}
}

@Override
public void onConfigureFailed(CameraCaptureSession session) {
TermuxApiLogger.error("onConfigureFailed() error in preview");
mImageReader.close();
releaseSurfaces(outputSurfaces);
closeCamera(camera, looper);
}
}, null);
Expand All @@ -187,7 +191,6 @@ static void saveImage(final CameraDevice camera, CameraCaptureSession session, C
@Override
public void onCaptureCompleted(CameraCaptureSession completedSession, CaptureRequest request, TotalCaptureResult result) {
TermuxApiLogger.info("onCaptureCompleted()");
closeCamera(camera, null);
}
}, null);
}
Expand Down Expand Up @@ -244,6 +247,13 @@ static int correctOrientation(final Context context, final CameraCharacteristics
return jpegOrientation;
}

static void releaseSurfaces(List<Surface> outputSurfaces) {
for (Surface outputSurface : outputSurfaces) {
outputSurface.release();
}
TermuxApiLogger.info("surfaces released");
}

static void closeCamera(CameraDevice camera, Looper looper) {
try {
camera.close();
Expand Down

0 comments on commit a5f7d14

Please sign in to comment.