How to use the camera view in GoCoder SDK for Android

This article describes how to use the UI component of WZCameraView, a subclass of the Android SDK SurfaceView class, to display a preview of the active camera.


Display the camera preview
Detect the cameras available
Set the active camera
Set the preview frame size
Set the crop and scale behavior

Display the camera preview

Use WZCameraView class with the standard set of View operations or within an app's XML-based UI layout definition to create the camera preview. Use the startPreview and stopPreview methods, respectively, to start and stop the camera preview display.

  1. Use the following custom parameters to specify the initial state for the WZCameraView view element.
    Parameter Default Value Description
    scaleMode fill Specifies the preview scaling mode. Valid values are fill and crop.
    defaultCamera back Specifies the default camera to preview. Valid values are back and front.
    frameSizePreset frameSize640x480 Specifies a preset frame size for the preivew. Valid values are frameSize320x240, frameSize640x480, frameSize960x540, frameSize1280x720, frameSize1920x1080, and frameSize3840x2160.
  2. Use the setCameraView method to associate the camera view with the GoCoder SDK.
    // Initialize the SDK
    goCoder = WowzaGoCoder.init(this, SDK_SAMPLE_APP_LICENSE_KEY);
    // Assign the camera view
    goCoderCameraView = (WZCameraView) findViewById(;
    // Start the camera preview
  3. Create the camera view programmatically or by modifying the app layout definition file.
    • The following is an example that creates the camera view programmatically.
      // Programmatically create and add the camera view
      goCoderCameraView = new WZCameraView(this);
      // Get the parent view and define the layout behavior
      FrameLayout cameraViewContainer = (FrameLayout)findViewById(;
      FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
          FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
      // Add the camera view to the parent containing view
      cameraViewContainer.addView(this.goCoderCameraView, layoutParams);
      // Associate the camera view with the GoCoder SDK
    • The following is an example app layout definition file ([project_root]/app/src/main/res/layout/activity_main.xml) that uses the WZCameraView  class.
      <!-- The root of the application layout -->
      <RelativeLayout xmlns:android=""
        <!-- The camera preview display -->

Detect the cameras available

Use the getNumberOfDeviceCameras static method to return the number of cameras available and the getCameras method to return an array of WZCamera objects for each camera.

// Enable the switch camera button if more than one camera is available
int numCameras = WZCamera.getNumberOfDeviceCameras();
switchCameraButton.setEnabled(numCameras > 1);

Set the active camera

Use the setCamera property to specify the active camera and the switchCamera method to switch between cameras when multiple cameras are available.

// Set the active camera to the front camera if it is not already active
if (!goCoderCameraView.getCamera().isFront())

Set the preview frame size

Use the setFrameSize method to set the frame size for the camera preview.

Note: If you specify a frame size that the active camera doesn't support, the closest supported frame size is used. In this case, the setFrameSize method returns the frame size that is used. Use the getSupportedFrameSizes method of the WZCamera class to determine which frame sizes are supported for a particular camera.

// Set the camera preview to 720p
WZSize actualFrameSize = goCoderCameraView.setFrameSize(new WZSize(1280, 720));

Set the crop and scale behavior

Use the setScaleMode method to specify how the camera preview should be displayed when the active frame size or aspect ratio of the camera does not match the view. The following modes are available:

WZMediaConfig.RESIZE_TO_ASPECT Scale the camera preview to fit within the view surface. Note that letterboxing may be applied to maintain the proper aspect ratio.
WZMediaConfig.FILL_VIEW Scale the camera preview to fill the entire view surface. Note that the preview may be cropped to maintain the proper aspect ratio.

Use the following command to set the previewGravity mode.

// Set the cropping mode so that the full frame is displayed, potentially cropped

If you're having problems or want to discuss this article, post in our forum.