Class CameraPreview

  • All Implemented Interfaces:
    Drawable.Callback, AccessibilityEventSource, KeyEvent.Callback, ViewManager, ViewParent
    Direct Known Subclasses:
    BarcodeView

    public class CameraPreview
    extends ViewGroup
    CameraPreview is a view that handles displaying of a camera preview on a SurfaceView. It is intended to be used as a base for realtime processing of camera images, e.g. barcode decoding or OCR, although none of this happens in CameraPreview itself. The camera is managed on a separate thread, using CameraInstance. Two methods MUST be called on CameraPreview to manage its state: 1. resume() - initialize the camera and start the preview. Call from the Activity's onResume(). 2. pause() - stop the preview and release any resources. Call from the Activity's onPause(). Startup sequence: 1. Create SurfaceView. 2. open camera. 2. layout this container, to get size 3. set display config, according to the container size 4. configure() 5. wait for preview size to be ready 6. set surface size according to preview size 7. set surface and start preview
    • Constructor Detail

      • CameraPreview

        public CameraPreview​(Context context)
      • CameraPreview

        public CameraPreview​(Context context,
                             AttributeSet attrs,
                             int defStyleAttr)
    • Method Detail

      • initializeAttributes

        protected void initializeAttributes​(AttributeSet attrs)
        Initialize from XML attributes.
        Parameters:
        attrs - the attributes
      • addStateListener

        public void addStateListener​(CameraPreview.StateListener listener)
        Add a listener to be notified of changes to the preview state, as well as camera errors.
        Parameters:
        listener - the listener
      • setTorch

        public void setTorch​(boolean on)
        Call this on the main thread, while the preview is running.
        Parameters:
        on - true to turn on the torch
      • setPreviewScalingStrategy

        public void setPreviewScalingStrategy​(PreviewScalingStrategy previewScalingStrategy)
        Override the preview scaling strategy.
        Parameters:
        previewScalingStrategy - null for the default
      • getPreviewScalingStrategy

        public PreviewScalingStrategy getPreviewScalingStrategy()
        Override this to specify a different preview scaling strategy.
      • calculateTextureTransform

        protected Matrix calculateTextureTransform​(Size textureSize,
                                                   Size previewSize)
        Calculate transformation for the TextureView. An identity matrix would cause the preview to be scaled up/down to fill the TextureView.
        Parameters:
        textureSize - the size of the textureView
        previewSize - the camera preview resolution
        Returns:
        the transform matrix for the TextureView
      • onLayout

        protected void onLayout​(boolean changed,
                                int l,
                                int t,
                                int r,
                                int b)
        Specified by:
        onLayout in class ViewGroup
      • getFramingRect

        public Rect getFramingRect()
        The framing rectangle, relative to this view. Use to draw the rectangle. Will never be null while the preview is active.
        Returns:
        the framing rect, or null
        See Also:
        isPreviewActive()
      • getPreviewFramingRect

        public Rect getPreviewFramingRect()
        The framing rect, relative to the camera preview resolution. Will never be null while the preview is active.
        Returns:
        the preview rect, or null
        See Also:
        isPreviewActive()
      • getPreviewSize

        public Size getPreviewSize()
      • getCameraSettings

        public CameraSettings getCameraSettings()
        Returns:
        the CameraSettings currently in use
      • setCameraSettings

        public void setCameraSettings​(CameraSettings cameraSettings)
        Set the CameraSettings. Use this to select a different camera, change exposure and torch settings, and some other options. This has no effect if the camera is already open.
        Parameters:
        cameraSettings - the new settings
      • resume

        public void resume()
        Start the camera preview and decoding. Typically this should be called from the Activity's onResume() method. Call from UI thread only.
      • pause

        public void pause()
        Pause scanning and the camera preview. Typically this should be called from the Activity's onPause() method. Call from UI thread only.
      • pauseAndWait

        public void pauseAndWait()
        Pause scanning and preview; waiting for the Camera to be closed. This blocks the main thread.
      • getFramingRectSize

        public Size getFramingRectSize()
      • setFramingRectSize

        public void setFramingRectSize​(Size framingRectSize)
        Set an exact size for the framing rectangle. It will be centered in the view.
        Parameters:
        framingRectSize - the size
      • getMarginFraction

        public double getMarginFraction()
      • setMarginFraction

        public void setMarginFraction​(double marginFraction)
        The the fraction of the width/height of view to be used as a margin for the framing rect. This is ignored if framingRectSize is specified.
        Parameters:
        marginFraction - the fraction
      • isUseTextureView

        public boolean isUseTextureView()
      • setUseTextureView

        public void setUseTextureView​(boolean useTextureView)
        Set to true to use TextureView instead of SurfaceView. Will only have an effect on API >= 14.
        Parameters:
        useTextureView - true to use TextureView.
      • isActive

        protected boolean isActive()
        Considered active if between resume() and pause().
        Returns:
        true if active
      • createCameraInstance

        protected CameraInstance createCameraInstance()
        Create a new CameraInstance. Override to use a custom CameraInstance.
        Returns:
        a new CameraInstance
      • previewStarted

        protected void previewStarted()
        Called when the preview is started. Override this to start decoding work.
      • getCameraInstance

        public CameraInstance getCameraInstance()
        Get the current CameraInstance. This may be null, and may change when pausing / resuming the preview. While the preview is active, getCameraInstance() will never be null.
        Returns:
        the current CameraInstance
        See Also:
        isPreviewActive()
      • isPreviewActive

        public boolean isPreviewActive()
        The preview typically starts being active a while after calling resume(), and stops when calling pause().
        Returns:
        true if the preview is active
      • calculateFramingRect

        protected Rect calculateFramingRect​(Rect container,
                                            Rect surface)
        Calculate framing rectangle, relative to the preview frame. Note that the SurfaceView may be larger than the container. Override this for more control over the framing rect calculations.
        Parameters:
        container - this container, with left = top = 0
        surface - the SurfaceView, relative to this container
        Returns:
        the framing rect, relative to this container
      • isCameraClosed

        public boolean isCameraClosed()
        Returns:
        true if the camera has been closed in a background thread.