Search code examples
androidc++android-ndkadbandroid-camera2

How to access camera from natively compiled c++ binary in Android


I have been experimenting to access the camera with Android C++ native API. But instead of including the binary inside an APK, I just compiled the code into an executable and pushed into the device using ADB. The binary is placed inside /data/local/tmp directory and executed from adb shell.

But i am unable to access the camera, is there any permission that i am missing? or does Android make it forbidden to use camera directly from an executable?

#include<stdio.h>
#include <camera/NdkCameraManager.h>

int main(){
    printf("Camera Manager\n");
    ACameraManager *cameraManager = ACameraManager_create();
    ACameraIdList *cameraIds = nullptr;
    ACameraManager_getCameraIdList(cameraManager, &cameraIds);
    printf("Number of camera %d\n", cameraIds->numCameras);
    //deleting the camera manager
    ACameraManager_delete(cameraManager);
    printf("Deleted the camera\n");  
}

The above code always print 0 as the number of camera


Solution

  • Most likely, your phone has a LEGACY camera. NdkCamera does not work with LEGACY camera, that's how you get 0 for numCameras. You don't need permissions to open the manager and read the idList.

    But you cannot actually open a camera from command line: you cannot assign android.permission.CAMERA to a native binary. Also, you need a binder interface to work with camera, see https://stackoverflow.com/a/54224388/192373 (that answer was written before the NDK r.20 when NdkBinder was released).