public final class Camera3
extends java.lang.Object
It is adapted from the Camera2Basic Google Sample at https://github.com/googlesamples/android-Camera2Basic
Modifier and Type | Class and Description |
---|---|
static class |
Camera3.CameraState |
static interface |
Camera3.PreviewSizeCallback |
Modifier and Type | Field and Description |
---|---|
static CaptureRequestConfiguration |
CAPTURE_CONFIG_DEFAULT |
static ErrorHandler |
ERROR_HANDLER_DEFAULT |
static CaptureRequestConfiguration |
PRECAPTURE_CONFIG_NONE |
static CaptureRequestConfiguration |
PRECAPTURE_CONFIG_TRIGGER_AUTO_EXPOSE |
Constructor and Description |
---|
Camera3(android.content.Context context,
ErrorHandler errorHandler)
Creates a new Camera3 manager instance.
|
Modifier and Type | Method and Description |
---|---|
boolean |
captureConfigured()
It is only safe to call
resume() if you have already configured a capture
session with
startCaptureSession(String, PreviewHandler, List, List, Runnable) . |
void |
captureImage(StillCaptureHandler handler,
CaptureRequestConfiguration precapture,
CaptureRequestConfiguration capture)
Starts the process of capturing a still image from the camera.
|
java.util.List<java.lang.String> |
getAvailableCameras() |
java.util.List<android.util.Size> |
getAvailableImageSizes(java.lang.String cameraId,
int format)
Get all supported image sizes for the given image format and camera
|
java.util.Collection<android.util.Size> |
getAvailableVideoSizes(java.lang.String cameraId)
TODO doc
TODO BUG sometimes some of these sizes don't actually work - lead to surface abandoned error
|
android.hardware.camera2.CameraCharacteristics |
getCameraInfo(java.lang.String cameraId) |
android.util.Size |
getDefaultVideoSize(java.lang.String cameraId)
Get a recommended video size at a reasonable aspect ratio and size.
|
ErrorHandler |
getErrorHandler() |
android.util.Size |
getLargestAvailableImageSize(java.lang.String cameraId,
int imageFormat)
Gets the largest image dimensions (by area) that the given camera is able to take in the
given format
|
boolean |
hasCameraPermission()
Checks if the app has permission to access the camera
|
boolean |
hasMicrophonePermission()
Checks if the app has permission to access the microphone
|
boolean |
hasWritePermission()
Checks if the app has permission to write to the file system.
|
boolean |
isStarted()
Images can only be captured between calling
resume() or
startCaptureSession(String, PreviewHandler, List, List, Runnable) and calling
pause() . |
void |
pause()
Stops background threads and frees the camera.
|
boolean |
requestsInQueue()
Check if there are any enqueued requests that have not yet been resolved
|
void |
resume() |
void |
resume(java.lang.Runnable onSessionRestarted)
Resumes the capture session established by the last call to
startCaptureSession(String, PreviewHandler, List) |
void |
saveImageAsync(android.media.Image image,
java.io.File file)
A utility method to asynchronously save an image file.
|
void |
saveImageSync(android.media.Image image,
java.io.File file)
A utility method to synchronously save an image file.
|
void |
setCaptureResultListener(CaptureResultListener listener)
Registers a listener to be updated whenever Camera3 gets a result from the camera.
|
void |
startCaptureSession(java.lang.String cameraId,
PreviewHandler previewHandler,
java.util.List<StillCaptureHandler> stillCaptureSessions) |
void |
startCaptureSession(java.lang.String cameraId,
PreviewHandler previewHandler,
java.util.List<StillCaptureHandler> stillCaptureSessions,
java.util.List<VideoCaptureHandler> videoCaptureHandlers) |
void |
startCaptureSession(java.lang.String cameraId,
PreviewHandler previewHandler,
java.util.List<StillCaptureHandler> stillCaptureHandlers,
java.util.List<VideoCaptureHandler> videoCaptureHandlers,
java.lang.Runnable onSessionStarted)
Starts a new session.
|
void |
startVideoCapture(VideoCaptureHandler handler,
java.io.File outputFile,
VideoCaptureStartedCallback callback)
Asyncronously requests to start capturing video
|
void |
stopVideoCapture(VideoCaptureHandler handler)
Stops video recording.
|
public static final CaptureRequestConfiguration PRECAPTURE_CONFIG_TRIGGER_AUTO_EXPOSE
public static final CaptureRequestConfiguration PRECAPTURE_CONFIG_NONE
public static final CaptureRequestConfiguration CAPTURE_CONFIG_DEFAULT
public static final ErrorHandler ERROR_HANDLER_DEFAULT
public Camera3(@NonNull android.content.Context context, @Nullable ErrorHandler errorHandler)
Activity
. Many different preview sessions, capture sessions, and capture
requests can exist for one Camera3 object.context
- The context from which to access the camera. Should usually just be the
current activityerrorHandler
- An ErrorHandler
to handle any errors that arise over the lifetimepublic void startCaptureSession(@NonNull java.lang.String cameraId, @Nullable PreviewHandler previewHandler, @Nullable java.util.List<StillCaptureHandler> stillCaptureSessions)
public void startCaptureSession(@NonNull java.lang.String cameraId, @Nullable PreviewHandler previewHandler, @Nullable java.util.List<StillCaptureHandler> stillCaptureSessions, @Nullable java.util.List<VideoCaptureHandler> videoCaptureHandlers)
public void startCaptureSession(@NonNull java.lang.String cameraId, @Nullable PreviewHandler previewHandler, @Nullable java.util.List<StillCaptureHandler> stillCaptureHandlers, @Nullable java.util.List<VideoCaptureHandler> videoCaptureHandlers, @Nullable java.lang.Runnable onSessionStarted)
cameraId
- which camera to use (from getAvailableCameras()
).previewHandler
- an object representing the configuration for the camera
preview, or
null
to not show a preview (see
PreviewHandler
).stillCaptureHandlers
- a list of zero or more StillCaptureHandler
's,
or null if no still images will be captured. Usually only one
is required.videoCaptureHandlers
- a list of zero or more VideoCaptureHandler
's,
or null if no video will be recorded. Usually a maximum of one
is required.onSessionStarted
- an optional callback that will be called to notify the user when
the camera has been opened and the capture session has been
started.PreviewHandler
,
StillCaptureHandler
,
VideoCaptureHandler
public void resume()
resume(Runnable)
public void resume(@Nullable java.lang.Runnable onSessionRestarted)
Resumes the capture session established by the last call to
startCaptureSession(String, PreviewHandler, List)
If you're not sure if startCaptureSession(String, PreviewHandler, List)
has
been called already or not, you can check with captureConfigured()
onSessionRestarted
- an optional callback that will be called to notify the user when
the camera has been opened and the capture session has been
restarted.public void pause()
pause
must be called in Activity.onPause()
form the activity
that started the session. Otherwise, the app could close without relinquishing control of the
camera.
public void setCaptureResultListener(@Nullable CaptureResultListener listener)
OnImageAvailableListener
in
StillCaptureHandler.StillCaptureHandler(int, Size, OnImageAvailableListener)
listener
- the listener to register or null
to stop listeningpublic void startVideoCapture(@NonNull VideoCaptureHandler handler, @Nullable java.io.File outputFile, @Nullable VideoCaptureStartedCallback callback)
handler
- the handler responsible for configuring and writing the videooutputFile
- the file where the video will be saved, or null to write to a temp filecallback
- a callback function to be called when the video capture has been startedpublic void stopVideoCapture(@NonNull VideoCaptureHandler handler)
Note: the starting video recording is asyncronous so if you stop immediately after you start this method may cause an IllegalStateException
handler
- the handler that is currently recording videopublic void captureImage(@NonNull StillCaptureHandler handler, @Nullable CaptureRequestConfiguration precapture, @NonNull CaptureRequestConfiguration capture)
startCaptureSession(String, PreviewHandler, List)
handler
- the StillCaptureHandler
which will be responsible for processing
the imageprecapture
- the precapture configuration. Use PRECAPTURE_CONFIG_TRIGGER_AUTO_EXPOSE
to trigger auto focus prior to the image capture or use PRECAPTURE_CONFIG_NONE
to skip precapture (and use the preview
focus). This will speed up the capture process.capture
- the configuration for the actual image capture (on top of the defaults).
Use CAPTURE_CONFIG_DEFAULT
if you don't want to change the
default configuration at all.@Contract(pure=true) public ErrorHandler getErrorHandler()
@Contract(pure=true) public boolean captureConfigured()
resume()
if you have already configured a capture
session with
startCaptureSession(String, PreviewHandler, List, List, Runnable)
.
This method allows you to check that status.@Contract(pure=true) public boolean isStarted()
resume()
or
startCaptureSession(String, PreviewHandler, List, List, Runnable)
and calling
pause()
. This method allows you to check whether the camera can receive
requests. If this is false, you can call resume()
.true
if the camera can receive requests@Contract(pure=true) public boolean requestsInQueue()
true
if the request queue is not empty.public java.util.List<java.lang.String> getAvailableCameras() throws android.hardware.camera2.CameraAccessException
android.hardware.camera2.CameraAccessException
@Nullable public android.hardware.camera2.CameraCharacteristics getCameraInfo(java.lang.String cameraId) throws android.hardware.camera2.CameraAccessException
android.hardware.camera2.CameraAccessException
@NonNull public java.util.List<android.util.Size> getAvailableImageSizes(@NonNull java.lang.String cameraId, int format)
cameraId
- the id of the camera form getAvailableCameras()
format
- one of ImageFormat
@Nullable public android.util.Size getLargestAvailableImageSize(java.lang.String cameraId, int imageFormat)
cameraId
- the id of the camera form getAvailableCameras()
imageFormat
- one of ImageFormat
Size
if one exists or null
if the camera does not offer
any sizes for the given formatgetAvailableImageSizes(String, int)
@NonNull public java.util.Collection<android.util.Size> getAvailableVideoSizes(@NonNull java.lang.String cameraId)
cameraId
- the id of the camera form getAvailableCameras()
public android.util.Size getDefaultVideoSize(@NonNull java.lang.String cameraId)
getAvailableVideoSizes(String)
cameraId
- the id of the camera form getAvailableCameras()
null
if the camera does not support any
video sizespublic void saveImageAsync(android.media.Image image, java.io.File file)
The caller must obtain permission to write to external storage (if necessary) before calling this method.
image
- the image to savefile
- the file to write tosaveImageSync(Image, File)
public void saveImageSync(android.media.Image image, java.io.File file)
image
- the image to savefile
- the file to write topublic boolean hasCameraPermission()
Manifest.permission.CAMERA
public boolean hasMicrophonePermission()
Manifest.permission.RECORD_AUDO
public boolean hasWritePermission()
Manifest.permission.WRITE_EXTERNAL_STORAGE