How to use the camera view in GoCoder SDK for Android

Learn how to use the UI component of WZCameraView, a subclass of the SurfaceView class in the Wowza GoCoder™ SDK for Android, to display a preview of the active camera.

Contents


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(R.id.cameraPreview);
    
    // Start the camera preview
    goCoderCameraView.startPreview();
  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(R.id.cameraView);
      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
      this.goCoder.setCameraView(goCoderCameraView);
    • 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="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:wowza="http://schemas.android.com/apk/res-auto"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        tools:context=".MainActivity">
      
        <!-- The camera preview display -->
        <com.wowza.gocoder.sdk.api.devices.WZCameraView
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:id="@+id/cameraPreview"
          wowza:scaleMode="fill"
          wowza:defaultCamera="back"
          wowza:frameSizePreset="frameSize1280x720"/>
      
      </RelativeLayout>
      

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())
  goCoderCameraView.switchCamera();

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
goCoderCameraView.setScaleMode(WZMediaConfig.FILL_VIEW);

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