2

我正在尝试创建一个简单的应用程序,该应用程序将获取相机并将其输出到本机窗口。到目前为止,我可以创建窗口,设置其背景颜色并获取相机。但是,我似乎无法弄清楚如何抓取表面并将其用于预览。这是我现有代码的片段......

struct android_app* app;
EGLDisplay display;
EGLSurface surface;
EGLContext context;
using namespace android;
sp<Camera> cam;

void init_camera(){
    int num = Camera::getNumberOfCameras();
    if(num == 0){
        LOGI("No Camera found not using one");
        return;
    }
    else if(num >1 ){
        LOGI("%d Cameras Found using the first");
    }

    CameraInfo camInfo;
    status_t infoRes = Camera::getCameraInfo(0, &camInfo);
    if(infoRes == NO_ERROR){
        int facing = camInfo.facing;
        if(facing == 0){
            LOGI("Using rear camera");
        }
        else{
            LOGI("Using front camera");
        }
    }
    else{
        LOGE("There was an error getting information");
    }
    cam = Camera::connect(0);
    status_t info1 = cam->setPreviewDisplay(&surface);
}

但是当我尝试编译时,我得到...

jni/../../AOSP/frameworks/native/include/utils/StrongPointer.h: In constructor 'android::sp<T>::sp(U*) [with U = void*, T = android::Surface]':
jni/main.cpp:51:51:   instantiated from here
jni/../../AOSP/frameworks/native/include/utils/StrongPointer.h:134:34: error: cannot convert 'void**' to 'android::Surface*' in initialization
jni/../../AOSP/frameworks/native/include/utils/StrongPointer.h:136:16: error: invalid use of incomplete type 'struct android::Surface'
jni/../../AOSP/frameworks/native/include/ui/GraphicBuffer.h:124:18: error: forward declaration of 'struct android::Surface'
jni/../../AOSP/frameworks/native/include/utils/StrongPointer.h: In destructor 'android::sp<T>::~sp() [with T = android::Surface]':
jni/main.cpp:51:51:   instantiated from here
jni/../../AOSP/frameworks/native/include/utils/StrongPointer.h:149:16: error: invalid use of incomplete type 'struct android::Surface'
jni/../../AOSP/frameworks/native/include/ui/GraphicBuffer.h:124:18: error: forward declaration of 'struct android::Surface'
make: *** [obj/local/armeabi/objs/gonative/main.o] Error 1

任何想法我应该如何做到这一点?

更新

这个“可能”的工作......

void connectSurface() {
    // set up the thread-pool
    //sp<ProcessState> proc(ProcessState::self());
    //ProcessState::self()->startThreadPool();
    // create a client to surfaceflinger
    sp<SurfaceComposerClient> client = new SurfaceComposerClient();
    String8* s = new String8("test");
    sp<SurfaceControl> surfaceControl = client->createSurface(*s, 160, 240, PIXEL_FORMAT_RGB_565);
    SurfaceComposerClient::openGlobalTransaction();
    surfaceControl->setLayer(100000);
    SurfaceComposerClient::closeGlobalTransaction();

    // pretend it went cross-process
    Parcel parcel;
    SurfaceControl::writeSurfaceToParcel(surfaceControl, &parcel);
    parcel.setDataPosition(0);
    surfacew = Surface::readFromParcel(parcel);
    //ANativeWindow* window = surfacew.get();
    app->window = surfacew.get();
    /////////////////////////
}

或类似的东西(可能是错误的位图等)。但这需要

检查 android.permission.ACCESS_SURFACE_FLINGER 的 uid=10052 => denied (2700 us)

所以我回到第一方,我如何获得表面?

4

0 回答 0