Creating a Camera Application
Preparation
Setting up your Programming Environment
Android Studio
Configure the Project's AndroidManifest File
Implement the Stopping Recording Function
If you come across any mistakes or bugs in this tutorial, please let us know using a Github issue or a post on the DJI forum, or commenting in the Gitbook. Please feel free to send us Github pull request and help us fix any issues. However, all pull requests related to document must follow the document style
This tutorial uses Phantom 3 Professional as an example. You can download the final project for this tutorial from this Github page. We strongly recommend that you download the final project code and have it open as reference as you work through this tutorial.
You can download the entire project for this tutorial from this Github Page.
Preparation
(1) Download the Mobile SDK for Android from the following URL: https://developer.dji.com/mobile-sdk/downloads
(2) Update the firmware of the product (Phantom 3 Series, Inspire 1, Inspire Pro, M100, OSMO) through the URL: https://developer.dji.com/mobile-sdk/downloads/ (Refer to "How to Update the Firmware": http://www.dji.com/product/phantom-3-pro/info#downloads for instructions on updating the Phantom 3 Professional's firmware.)
(3) Set up an Android development environment (if you do not yet have one). Throughout this tutorial we will be using Android Studio 1.5.1, which you can download here: http://developer.android.com/sdk/index.html.
Setting up your Programming Environment
Android Studio
(1) Start a new Android Studio Project. Give the application and company domain any name you like, click next. Set the minimum SDK version as API 19 (recommended) and click next. Select "Empty Activity" and click next. As you reach the 'Customize the Activity' page, you should name your activity 'FPVTutorialActivity'. The layout name should automatically fill out with 'activity_fpvtutorial'. Press 'Finish'.
(2) Unzip the SDK package downloaded from the DJI website. Go to File -> New -> Import Module. In the 'Source Directory' field, find the DJI-SDK-LIB folder location. Press Finish.
(3) Next, right click on the 'app' module in the file directory to the left, and click 'Open Module Settings'. Navigate to the 'Dependencies' tab. Press the green plus sign, click 'Module Dependency', and select ':DJI-SDK-LIB'. Press 'OK' to confirm. After Gradle finishes rebuilding, your environment will be ready!
Configure the Project's AndroidManifest File
You need to configure your project's AndroidManifest file by adding the following permissions and features for DJI SDK V3.
DJI SDK V3 uses USB Accessory method to connect a Remote Controller with a mobile device. Please add the following codes under tag application to support this feature, especially pay attention to tag , and intent-filter, within the tag activity for dji.sdk.SDKManager.DJIAoaControllerActivity.
The above code shows where to add the API Key for registration. The API Key can be applied as follows:
Register for an account at https://developer.dji.com/register. Once registered, click on your name in the upper right corner. Click on 'Mobile SDK', then 'Create APP' and fill out the creation form. Type in your project's package name in the 'Identification Code' field.
Fill in the android:value field with the APP KEY that you have applied for from http://developer.dji.com/en/user/apps.
Write Code for Showing Video
(1) Initiate DJI SDK
DJI SDK V3 uses class DJISDKManager to manage the SDK's initiation, activation, and connection or disconnection to DJI's product.
Before using DJI SDK V3, developers first need to initiate it by using function initSDKManager of class DJISDKManager. Interface DJISDKManagerCallback is used to listen to the SDK registration result and the connected product changing. An example code snippet is as follows, we recommend developers to implement these codes in the application of Android, which will be active when the mobile app is not closed.
In the implementation of the Interface DJISDKManagerCallback, when the registration succeeds, call function startConnectionToProduct() to start connection with product; set the callback DJIBaseProductListener to listen to the connected product changing.
(2) Obtain or initiate the DJIBaseProduct object
DJI SDK V3 uses an instance of the class DJIBaseProduct to represent a DJI product. We recommend to use the following code to obtain or initiate the instance of DJIBaseProduct.
When an instance of DJIBaseProduct is obtained or initiated, and is not null, the corresponding modules (DJICamera, DJIRemoteController, etc.) can be accessed by using DJIBaseProduct.getXXX() methods, e.g., mProduct.getCamera() to obtain an instance of the camera module.
(3) Show the video view received from the camera
Developers need to implement the interface for receiving the raw video data (H.264 format) and the size of the data. DJI SDK V3 provides DJICodecManager for decoding the raw data. In the following sample code snippet, the raw video is put into an instance of DJICodecManager. After implementing the interface, use function setDJICameraReceivedVideoDataCallback to set it as the interface of DJICamera.
If developers want to show it on the screen of mobile devices, please add a TextureView as the UI for presenting the video. We recommend developers to refer to Google Developer's official documentation on TextureView to learn more about it http://developer.android.com/intl/ko/reference/android/view/TextureView.html.
The folowing is the definition of the Interface CameraReceivedVideoDataCallback
DJI SDK V3 provides a class DJICodecManager for hardware decoding the raw video data and showing the video in the TextureView.
We need to implement the interface SurfaceTextureListener and set it as the listener of the TextureView. SurfaceTextureListener can listen to when the surface texture is available, when it is destroyed, when its size is changed, when it is updated. The TextureView can initiate an instance of DJICodecManager when the surface texture is available.
By putting the received video data from the Interface CameraReceivedVideoDataCallback to an instance of DJICodecManager, it can show the video on the screen.
The interface CameraReceivedVideoDataCallback is set to the camera module for receiving the video data from the camera:
(4) Automatically Detect the Type of Connected Product
DJI SDK V3 can automatically detect the type of connected product and use a broadcast receiver to announce it. The connected product changing is listened to by the function onProductChanged in the interface DJISDKManagerCallback, and interface DJIComponentListener listens to the changing of component (Camera, Remote Controller, etc.):
To listen to the changing product, please register a broadcast receiver,
The above is an example for the explanation of the usage of DJI SDK V3's APIs in showing video. We recommend developers to review the source code of the FPV tutorial project for a better understanding of the usage of DJI SDK V3's APIs.
Connecting to your Aircraft
After you have built and run the project successfully, you can now connect your mobile device to an aircraft to check the FPV. Follow the appropriate instructions for your specific aircraft model:
1. Connecting to a DJI Inspire 1 or Phantom 3 Professional/Advanced:
Turn on your remote controller, then turn on your aircraft
Connect your mobile device to the remote controller using a USB cable. Tap your own app's icon when the message window "Choose an app for the USB device" pops up.
Tap "OK" when the message window prompts "Allow the app to access the USB accessory".
Tap "OK" when the activation alert displays.
You are ready to use the FPV View app.
Checking your results
If you can see the live video stream in the app, congratulations! You've succesfully implemented a First Person View!
Implementing the Capture Function
The private void captureAction() function is used to take photos. In our source code, we implement a "Capture" button which calls this function whenever pressed.
That was a lot of code we just threw at you, so let's break it down.
The first thing we need to do is define a CameraMode enum, which we will use to set the mode of the camera onboard our DJI Drone.
The reason we defined this enum 'cameraMode' was so that we could pass it as a parameter for the setCameraMode() function that we are about to call. setCameraMode() sets the mode of the DJI drone's camera (Capture Mode, Playback Mode, Record Mode etc.). setCameraMode() takes in two parameters:
setCameraMode(CameraMode mode, final DJICompletionCallback callback)
The first parameter, a CameraMode enum, tells the function what mode to set the camera to. In this case, we tell it to set the camera to Capture Mode.
The second parameter is a callback function, which is run after setCameraMode() attempts to set the camera mode. The callback function is reproduced, in brief, below.
The callback function takes in a confirmation signal from the drone, in the form of a DJIError object 'error'. If the error code given by 'error' is null, then the code to take the photo executed successfully. Else, a handler will show an appropriate error message depending on the error code.
Within the callback function we have code to tell the drone to take a photo.
If this code looks familiar, it's because it follows a structure almost identical to the larger function it is a part of! First we create a CameraShootPhotoMode enum called 'photoMode'. When the drone takes a photo, this enum instructs the drone whether it should take a single photo, a burst of photos, or a continuous stream of photos. For this example we'll be taking a single photo at a time.
The startShootPhoto() method tells the drone's camera to take a photo. Just like the setCameraMode() function, it takes in an enum and a callback function. We've just gone over the enum variable it takes in.
The callback function uses a handler to display a message giving an error code and an error description after the drone's camera attempts to take a photo. If a photo has successfully been taken, this message will confirm it.
And that's it! Add a "Capture" button into your app which calls this method, and give it a go!
Implement the Recording Function
The recordAction() method is almost identical to the captureAction() method we just implemented, with just a few key differences! Take a quick look at the code below:
Notice that the cameraMode enum has been set as RecordVideo because this time we want the camera to record.
Additionally, within our callback function, we call startRecordVideo() instead of startShootPhoto(). startRecordVideo() only takes in one parameter, a callback function. It does not take in an enum as startShootPhoto() does, as there is only one recording mode.
Implement the Stopping Recording Function
Once the camera starts recording, we need some way to tell it to stop! That's where stopRecord() comes in. The code below should look quite familiar to you by now:
You can now add a 'Record' and 'Stop Recording' button to your app, and have them call recordAction() and stopRecord() respectively. Build and run the project, and it should look something like the screenshot below:
Congratulations! Your Aerial First Person View Android app is complete, capable of viewing your DJI Drone's video feed, as well as remotely taking picture and videos!
Viewing your Images
Unfortunately, this tutorial does not include guidance on viewing photos and videos onboard your DJI Drone's SD card. However, if you would like to see the pictures and videos you took through your brand new app, you can download DJI GO App, found here:
https://play.google.com/store/apps/details?id=dji.pilot&hl=en
Alternatively, you can search for the app in the Google Play Store under the name 'DJI GO'.
Summary
You’ve come a long way in this tutorial: you’ve learned how to use the DJI Mobile SDK to show the FPV view of the aircraft's camera and control the camera of a DJI platform. These features, Capture and Record are the most basic and common features in a typical drone mobile app. However, if you want to create a drone app that is more fancy, you still have a long way to go. More advanced features would include previewing the photo and video in the SD Card, showing the OSD of the aircraft and so on.
Hope you enjoyed this tutorial, stay tuned for our next one!