Use the camera view in GoCoder SDK for Android

Learn how to use the UI component of WOWZCameraView, 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 available cameras
Set the active camera
Set the preview frame size
Set the crop and scale behavior
More resources

Display the camera preview


Use the WOWZCameraView 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 WOWZCameraView element:
     
    Parameter Description
    scaleMode The preview scaling mode. Valid values are fill (the default) and crop.
    defaultCamera The default camera to preview. Valid values are back (the default) and front.
    frameSizePreset The preset frame size for the preview. Valid values are frameSize320x240, frameSize640x480 (the default), 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 = (WOWZCameraView) findViewById(R.id.cameraPreview);
    
    // Start the camera preview
    goCoderCameraView.startPreview();
  3. Create 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);

...or by modifying the app layout definition file ([project_root]/app/src/main/res/layout/activity_main.xml), which uses the WOWZCameraView 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.WOWZCameraView
    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 available cameras


Use the getNumberOfDeviceCameras static method to get the number of cameras available, and use the getCameras method to get an array of WOWZCamera objects for each camera.

// Enable the switch camera button if more than one camera is available
int numCameras = WOWZCamera.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's not 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 WOWZCamera class to determine which frame sizes are supported for a particular camera.

// Set the camera preview to 720p
WOWZSize actualFrameSize = goCoderCameraView.setFrameSize(new WOWZSize(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:

Parameter Description
WOWZMediaConfig.RESIZE_TO_ASPECT Scale the camera preview to fit within the screen area. Letterboxing may be applied to maintain the aspect ratio.
WOWZMediaConfig.FILL_VIEW Scale the camera preview to fill the entire screen area. The preview may be cropped to maintain the 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(WOWZMediaConfig.FILL_VIEW);

More resources