Class CameraHelper

java.lang.Object
com.digimarc.capture.camera.CameraHelper

public class CameraHelper extends Object
The CameraHelper class provides a mechanism to switch to use android.hardware.camera2 API in order to get the best Digimarc Barcode reading performance. On suitable devices, this class will use Camera2 for image capture. Otherwise the legacy camera API will be used. In order to use Camera2 the device must: 1) be running Android 5.0+ and 2) support the Camera2 Full or Limited implementation.
  • Method Details

    • Builder

      @NonNull public static CameraHelper.Builder Builder()
      Get Builder object for creating a CameraHelper object
      Returns:
      Builder
    • setDataListener

      public void setDataListener(@Nullable CameraDataListener dataListener)
      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

      public void setErrorListener(@Nullable CameraErrorListener errorListener)
      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

      public void setNotifyListener(@Nullable CameraNotifyListener notifyListener)
      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

      public void setConfigurationListener(@Nullable CameraConfigurationListener configurationListener)
      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

      public void setRegionListener(@Nullable CameraRegionListener regionListener)
      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 public void startPreview()
      Deprecated.
      The camera lifecycle is managed automatically by CameraSurfaceView. Using this method may move the camera into an unrecoverable state.
      Start camera preview.
    • stopPreview

      @Deprecated public void stopPreview()
      Deprecated.
      The camera lifecycle is managed automatically by CameraSurfaceView. 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 the CameraNotifyListener interface, wait for CameraNotifyListener.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 public void setFocusRegion(DetectionRegion region, int regionMask)
      Deprecated.
      Setting large focus regions can lead to unpredictable camera behavior, depending on the device OEM. Use setFocusPoint(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() and isRegionAWBSupported().

      Parameters:
      region - Region for focus
      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.
    • 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() and isRegionAWBSupported().

      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

      @Nullable public Metadata 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 the CameraNotifyListener interface, wait for CameraNotifyListener.onCameraAvailable() to be signalled before calling this method.
      Returns:
      Point object containing the resolution or null if the camera is not yet available.
    • getDescriptionForErrorCode

      @NonNull public static String getDescriptionForErrorCode(@NonNull CameraHelper.CameraError code)
      Gets a string description for a camera operation error code.
      Parameters:
      code - An error code from CameraHelper.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 the VideoCaptureReader.setImageDetectionRegion(DetectionRegion) or ImageReader.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.