簡體   English   中英

DJI SDK獲取最后一張圖片

[英]DJI SDK Get last image

我目前正在嘗試使用移動SDK訪問DJI Phantom 4拍攝的最后一張圖像。 我看過: 如何使用IOS DJI-SDK以編程方式從無人機下載圖像 ,它有所幫助,但是,在.refreshFileList()調用中,引發了一個錯誤,指出``此過程的執行已超時(代碼-1003)”。

任何幫助將不勝感激! 這是我的代碼:

/***** Setup Camera *****/

// get current product
guard let drone = DJISDKManager.product() else {
    print("Product is connected but DJISDKManager.product is nil when attempting to download media")
    return
}

// Get camera on drone
guard  let camera: DJICamera = drone.camera else {
    print("Unable to detect Camera in initDownload()")
    return
}

print("Successfully detected the camera")


// take picture when project starts
camera.startShootPhoto(completion: { (error) in

    if (error != nil) {
        print("Shoot photo error: \(error.debugDescription)")
    }

})


/***** Get Last Picture *****/

// check if we can download images with the product
if !camera.isMediaDownloadModeSupported() {
    print("Product does not support media download mode")
    return
}

print("before set mode...")

// switch camera mode to allow for media downloads
camera.setMode( .mediaDownload, withCompletion: {(error) in

    print("in set mode...")

    if error != nil {

        print(("\(error!.localizedDescription)"))

    } else {

        // get the media manager from the drone to gain access to the files
        let manager = camera.mediaManager!

        manager.refreshFileList(of: DJICameraStorageLocation.sdCard, withCompletion:  { (error) in

            print("in refresh file list...")

            if error != nil {

                ///////TIMES OUT HERE/////////

                print("Refresh error State: \(manager.sdCardFileListState.rawValue)")
                print("Error refreshing list: \(error!.localizedDescription)")

            }else {

                print("Refreshed file list")
                print("No error State: \(manager.sdCardFileListState.rawValue)")

                // get list of files
                guard let files = manager.sdCardFileListSnapshot() else {
                    print("No files to download")
                    return
                }

                print("There are files to download.. Beginning Download")
                print(("files \(files.count)"))
            }
        }) // end of file-refresh block
    } // end of if else
})// end of camera setMode block

代碼的問題在於,您在更改模式之前沒有等待startShootPhoto操作完成。 這可能是導致您出錯的原因。

這是您功能的工作版本

 func shootPhoto() {
    guard let drone = (DJISDKManager.product() as? DJIAircraft) else {
        print("Product is connected but DJISDKManager.product is nil when attempting to download media")
        return
    }

    // Get camera on drone
    guard  let camera: DJICamera = drone.camera else {
        print("Unable to detect Camera in initDownload()")
        return
    }

    print("Successfully detected the camera")


    // take picture when project starts
    camera.startShootPhoto(completion: { (error) in

        if (error != nil) {
            print("Shoot photo error: \(error.debugDescription)")
        }
        else {
            self.getLastImage(camera: camera)
        }

    })

}

func getLastImage(camera: DJICamera) {
    /***** Get Last Picture *****/

    // check if we can download images with the product
    if !camera.isMediaDownloadModeSupported() {
        print("Product does not support media download mode")
        return
    }

    print("before set mode...")
    let retry = RetryManager() // Run the same block multiple times if the command has an error
    // switch camera mode to allow for media downloads
    retry.runBlock(withRetries: 5) {
        camera.setMode( .mediaDownload, withCompletion: {(error) in

            print("in set mode...")

            if error != nil {

                print(("\(error!.localizedDescription)"))

            } else {
                retry.stop()
                // get the media manager from the drone to gain access to the files
                let manager = camera.mediaManager!

                manager.refreshFileList(of: DJICameraStorageLocation.sdCard, withCompletion:  { (error) in

                    print("in refresh file list...")

                    if error != nil {

                        ///////TIMES OUT HERE/////////

                        print("Refresh error State: \(manager.sdCardFileListState.rawValue)")
                        print("Error refreshing list: \(error!.localizedDescription)")

                    }else {

                        print("Refreshed file list")
                        print("No error State: \(manager.sdCardFileListState.rawValue)")

                        // get list of files
                        guard let files = manager.sdCardFileListSnapshot() else {
                            print("No files to download")
                            return
                        }

                        print("There are files to download.. Beginning Download")
                        print(("files \(files.count)"))
                    }
                }) // end of file-refresh block
            } // end of if else
            retry.proceed()
        })// end of camera setMode block
    }

}

該代碼對我有用,這是重試管理器的要點,因為前幾次嘗試均失敗,所以我不得不使用它。 https://gist.github.com/justinmiller62/4c76d9704524fd8aaa60e166a5cccea8

另外,如果您希望代碼內聯而不是像這樣嵌套,則可以使用信號量等待上一個功能完成。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM