Class CameraHelper
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
static enum
Error codes that may be sent to theCameraErrorListener.onError(CameraHelper.CameraError error)
method.static enum
Allows an app to specify what type of camera support (i.e.static enum
MeteringRegion flags are used by thesetFocusRegion(DetectionRegion, int)
andsetFocusPoint(float, float, int, int)
methods. -
Method Summary
Modifier and TypeMethodDescriptionstatic CameraHelper.Builder
Builder()
Get Builder object for creating a CameraHelper objectstatic boolean
Determines whether the device has a usable camera or not.int
Get the camera's rotation.static String
Gets a string description for a camera operation error code.Gets an object containing metadata from the last frame captured.android.graphics.RectF
This method returns a rect representing the visible region of the camera surface.android.graphics.Point
Get the resolution of the current camera session.static boolean
This method wraps Android 23+ permission checking code so that we don't need to use the Support v4 library in DMSDK.boolean
Allows an application to check whether the camera is currently focusing.boolean
Check whether the device supports the use of auto exposure regions.boolean
Check whether the device supports the use of auto focus regions.boolean
Check whether the device supports the use of auto white balance regions.boolean
Queries whether the torch is currently on or not.boolean
Queries whether the handset has a flash (torch).void
release()
Release all objects held by the camera code.void
setCamera2ImageBufferCount
(int bufferCount) Set the number of image capture buffers to be used during Camera2 image capture.void
setConfigurationListener
(CameraConfigurationListener configurationListener) Set configuration listener.void
setDataListener
(CameraDataListener dataListener) Set data listener.void
setErrorListener
(CameraErrorListener errorListener) Set error errorListener.void
setFocusPoint
(float x, float y, int displayRotation, int regionMask) Set the camera focus to a point within the camera frame.void
setFocusRegion
(DetectionRegion region, int regionMask) Deprecated.Setting large focus regions can lead to unpredictable camera behavior, depending on the device OEM.void
setNotifyListener
(CameraNotifyListener notifyListener) Set notify listener.void
setRegionListener
(CameraRegionListener regionListener) Set region listener.void
setSurfaceTextureListener
(android.view.TextureView.SurfaceTextureListener listener) Set listener for the camera surface texture.void
setTorch
(boolean state) Enables/Disables the torch as long as the hardware supports torch modevoid
setUIHandler
(android.os.Handler handler) Set the handler to receive notifications from the camera system.void
Deprecated.The camera lifecycle is managed automatically byCameraSurfaceView
.void
Deprecated.The camera lifecycle is managed automatically byCameraSurfaceView
.void
This method causes the camera to perform an immediate focus operation and then resume normal auto-focus mode.
-
Method Details
-
Builder
Get Builder object for creating a CameraHelper object- Returns:
- Builder
-
setDataListener
Set data listener. This method can be used to change the data listener used after a CameraHelper object is created.- Parameters:
dataListener
- Data listener instance
-
setErrorListener
Set error errorListener. This method can be used to change the error errorListener used after a CameraHelper object is created.- Parameters:
errorListener
- Error errorListener instance
-
setNotifyListener
Set notify listener. This method can be used to change the notify listener used after a CameraHelper object is created.- Parameters:
notifyListener
- Notify listener instance
-
release
public void release()Release all objects held by the camera code. The normal use for this method is to call it from the application's onDestroy() method. -
setConfigurationListener
Set configuration listener. This method can be used to change the configuration listener used after a CameraHelper object is created.- Parameters:
configurationListener
- Configuration listener instance
-
setRegionListener
Set region listener. This method can be used to change the region listener used after a CameraHelper object is created.- Parameters:
regionListener
- Region listener instance
-
deviceHasCamera
@RequiresPermission("android.permission.CAMERA") public static boolean deviceHasCamera()Determines whether the device has a usable camera or not. This method only checks using the Camera1 API, as all devices support that.- Returns:
- True if the device has a camera, false otherwise.
-
setCamera2ImageBufferCount
public void setCamera2ImageBufferCount(int bufferCount) Set the number of image capture buffers to be used during Camera2 image capture. By default this is set to 3. In some instances camera performance can be smoother with a higher number of buffers. The trade-off here is the amount of memory used. For most applications this method is not needed.- Parameters:
bufferCount
- Number of buffers to be used.
-
startPreview
Deprecated.The camera lifecycle is managed automatically byCameraSurfaceView
. Using this method may move the camera into an unrecoverable state.Start camera preview. -
stopPreview
Deprecated.The camera lifecycle is managed automatically byCameraSurfaceView
. Using this method may move the camera into an unrecoverable state.Stop camera preview. -
isTorchSupported
public boolean isTorchSupported()Queries whether the handset has a flash (torch). The value returned will not be valid if this is called before the camera has been initialized and opened. If you use theCameraNotifyListener
interface, wait forCameraNotifyListener.onCameraAvailable()
to be signalled before calling this method.- Returns:
- True if the device has an accessible torch.
-
setTorch
public void setTorch(boolean state) Enables/Disables the torch as long as the hardware supports torch mode- Parameters:
state
- Whether to enable or disable the torch
-
isTorchOn
public boolean isTorchOn()Queries whether the torch is currently on or not.- Returns:
- True if the torch is on, false if it is not or if the torch itself is unavailable.
-
triggerCenterFocus
public void triggerCenterFocus()This method causes the camera to perform an immediate focus operation and then resume normal auto-focus mode. The primary reason for using this is to enable tap-to-focus behavior in an application. -
setFocusRegion
Deprecated.Setting large focus regions can lead to unpredictable camera behavior, depending on the device OEM. UsesetFocusPoint(float, float, int, int)
instead.Set the camera focus to a region on the screen. This method is generally used to set the focus region to match the read region. Region focus is only supported on Camera2 devices.Note that you do not need to worry about whether your device supports regions or not. If the device doesn't support a capability it won't be set. With that said if you want to check whether your device supports different types of regions you can use the methods
isRegionAFSupported()
,isRegionAESupported()
andisRegionAWBSupported()
.- Parameters:
region
- Region for focusregionMask
- Which auto settings should the region be applied to? Unless you have a specific need to only set a specific type of auto region we suggest using CameraHelper.RegionMetering.AUTO_ALL for this parameter.
-
setFocusPoint
public void setFocusPoint(float x, float y, int displayRotation, int regionMask) Set the camera focus to a point within the camera frame. The point is assumed to be in coordinates relative to the camera surface view. Setting the focus point is only supported on Camera2 devices.Note that you do not need to worry about whether your device supports regions or not. If the device doesn't support a capability it won't be set. With that said if you want to check whether your device supports different types of regions you can use the methods
isRegionAFSupported()
,isRegionAESupported()
andisRegionAWBSupported()
.- Parameters:
x
- X location within the camera surface view.y
- Y location within the camera surface view.displayRotation
- Display rotation of the screen in degrees.regionMask
- Which auto settings should the region be applied to? Unless you have a specific need to only set a specific type of auto region we suggest using CameraHelper.RegionMetering.AUTO_ALL for this parameter.
-
isRegionAFSupported
public boolean isRegionAFSupported()Check whether the device supports the use of auto focus regions.- Returns:
- True if an AF region is supported.
-
isRegionAESupported
public boolean isRegionAESupported()Check whether the device supports the use of auto exposure regions.- Returns:
- True if an AE region is supported.
-
isRegionAWBSupported
public boolean isRegionAWBSupported()Check whether the device supports the use of auto white balance regions.- Returns:
- True if an AWB region is supported.
-
isCameraFocusing
public boolean isCameraFocusing()Allows an application to check whether the camera is currently focusing.- Returns:
- True if the camera is focusing.
-
setSurfaceTextureListener
public void setSurfaceTextureListener(@NonNull android.view.TextureView.SurfaceTextureListener listener) Set listener for the camera surface texture.- Parameters:
listener
- Surface texture listener.
-
getCameraRotation
public int getCameraRotation()Get the camera's rotation. The Android camera internally produces an image in landscape orientation, even if the device is currently in portrait orientation. In order to display a frame captured as the user saw it on the screen the image will need to be rotated some amount. Generally this is either 90 or 270 degrees.- Returns:
- Rotation of the camera image. This value will be divisible by 90.
-
setUIHandler
public void setUIHandler(@Nullable android.os.Handler handler) Set the handler to receive notifications from the camera system.- Parameters:
handler
- Handler
-
getMetadata
Gets an object containing metadata from the last frame captured.- Returns:
- Object or null if metadata is not available.
-
getResolution
@Nullable public android.graphics.Point getResolution()Get the resolution of the current camera session. This will not be available until after the camera has started. If you use theCameraNotifyListener
interface, wait forCameraNotifyListener.onCameraAvailable()
to be signalled before calling this method.- Returns:
- Point object containing the resolution or null if the camera is not yet available.
-
getDescriptionForErrorCode
Gets a string description for a camera operation error code.- Parameters:
code
- An error code fromCameraHelper.CameraError
.- Returns:
- A string containing the error description.
-
haveCameraPermission
public static boolean haveCameraPermission()This method wraps Android 23+ permission checking code so that we don't need to use the Support v4 library in DMSDK.- Returns:
- True if the app has Camera permission.
-
getRectForVisibleSurface
@NonNull public android.graphics.RectF getRectForVisibleSurface()This method returns a rect representing the visible region of the camera surface. Whenever a camera surface is displayed the CameraHelper works to fill the full surface area while still preserving the surface's aspect ratio. This means that in most cases at least some area of the camera image will not be displayed. The rect returned by this method may be used to set the image detection region via theVideoCaptureReader.setImageDetectionRegion(DetectionRegion)
orImageReader.setImageDetectionRegion(DetectionRegion)
methods.Note: The camera image will always be centered within the camera surface. This means that the camera focus area (center of the camera view) will appear in the center of your camera surface.
Note: The returned rect is in display orientation.
- Returns:
- A rect containing the region. Each value within the rect will be between 0.0 and 1.0.
-