android get screen size via C

后端 未结 6 1438

i want to get screen size via c. i know that jni support calling java from c. but is there any person who knows another method? i mean get screen size from lowlevel modules

相关标签:
6条回答
  • 2020-12-11 05:13

    You don't need the buffer but you could use the ANativeWindow class if you pass in a Surface from java.

    void Java_com_justinbuser_nativecore_NativeMethods_setSurface(JNIEnv * env, jobject this, jobject surface){
            ANativeWindow nativeWindow = ANativeWindow_fromSurface(env, surface);
        int width =  ANativeWindow_getWidth(renderEngine->nativeWindow);
        int height = ANativeWindow_getHeight(renderEngine->nativeWindow);
    }
    
    0 讨论(0)
  • 2020-12-11 05:21

    You can also get the window size while processing the APP_CMD_INIT_WINDOW "app command" like this in your native activity:

    static void engine_handle_cmd(struct android_app* app, int32_t cmd) {
    struct engine* engine = (struct engine*)app->userData;
    switch (cmd) {
    case APP_CMD_INIT_WINDOW:
        if (engine->app->window != NULL) {
            int width = ANativeWindow_getWidth(engine->app->window);
            int height = ANativeWindow_getHeight(engine->app->window);
        }
    

    Check out main.c in the native-activity sample NDK project if you want to know how to set up the engine struct & the engine_handle_cmd callback.

    0 讨论(0)
  • 2020-12-11 05:23

    To get screen resolution you have to use struct ANativeWindow_Buffer:

    typedef struct ANativeWindow_Buffer {
        // The number of pixels that are show horizontally.
        int32_t width;
    
        // The number of pixels that are shown vertically.
        int32_t height;
    
        // The number of *pixels* that a line in the buffer takes in
        // memory.  This may be >= width.
        int32_t stride;
    
        // The format of the buffer.  One of WINDOW_FORMAT_*
        int32_t format;
    
        // The actual bits.
        void* bits;
    
        // Do not touch.
        uint32_t reserved[6];
    } ANativeWindow_Buffer;
    

    To get it use function ANativeWindow_lock:

    /**
     * Lock the window's next drawing surface for writing.
     */
    int32_t ANativeWindow_lock(ANativeWindow* window, ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds);
    

    Here's docs.

    If you're looking for usage of this function than take a look at android-ndk sample called native-plasma.

    0 讨论(0)
  • 2020-12-11 05:26

    For anyone like me who is looking for a pure C++ way of doing this, reading /dev/graphics/fb0 does not work reliably. On many devices, it returns an incorrect result (on mine it returns 640 X 400).

    This is a problem for situations in which you cannot use ANativeWindow or ANativeWindow_Buffer, for example in a native binary executable.

    What worked for me :

    Assuming you have a rooted phone, you can process the output of the below command to read the resolution reliably.

    su -c dumpsys display | grep mBaseDisplayInfo
    
    0 讨论(0)
  • 2020-12-11 05:29

    Yes, you can get the screen resolution from /dev/graphics/fb0, but (at least on my phone), read access is restricted to root and users in the 'graphics' group. At any rate, you can do something like this (error checking removed for clarity):

    // ... other standard includes ...
    #include <sys/ioctl.h>
    #include <linux/fb.h>
    
    //...
    
    struct fb_var_screeninfo fb_var;
    int fd = open("/dev/graphics/fb0", O_RDONLY);
    ioctl(fd, FBIOGET_VSCREENINFO, &fb_var);
    close(fd);
    // screen size will be in fb_var.xres and fb_var.yres
    

    I'm not sure if these are considered "public" NDK interfaces, since I don't know if Google considers "Android runs on Linux and uses the Linux Framebuffer interface" to be a part of the public API, but it does appear to work.

    If you do want to make sure it's portable, you should probably have a JNI fallback that calls into the Java WindowManager API.

    0 讨论(0)
  • 2020-12-11 05:37

    This works for me:

    DisplayMetrics displaymetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    int height = displaymetrics.heightPixels;
    int width = displaymetrics.widthPixels;
    
    0 讨论(0)
提交回复
热议问题