`
地球小野花
  • 浏览: 163154 次
  • 性别: Icon_minigender_1
  • 来自: 马赛大回旋
社区版块
存档分类
最新评论

转-Android原生(Native)C(JNI/NDK)开发之二:framebuffer篇

阅读更多

为方便以后学习和工作,现转载一批文章,方便以后使用。

 

来源:http://blog.sina.com.cn/s/blog_4a0a39c30100auh9.html   作者:风子

 

如对Android原生(Natvie)C开发还任何疑问,请参阅《Android原生(Native)C开发之一:环境搭建篇》:http://blog.sina.com.cn/s/blog_4a0a39c30100auh9.html  (或者http://android-sz.iteye.com/blog/828246)

 

虽然现在能通过交叉环境编译程序,并push到Android上执行,但那只是console台程序,是不是有些单调呢?下面就要看如何通过Linux的 framebuffer 技术在Android上画图形,关于Linux的framebuffer技术,这里就不再详细讲解了,请大家google一下。

 

操作framebuffer的主要步骤如下:

1、打开一个可用的FrameBuffer设备;
2、通过mmap调用把显卡的物理内存空间映射到用户空间;
3、更改内存空间里的像素数据并显示;

4、退出时关闭framebuffer设备。

 

下面的这个例子简单地用framebuffer画了一个渐变的进度条,代码 framebuf.c 如下:

#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <linux/fb.h>
#include <sys/mman.h>
inline static unsigned short int make16color(unsigned char r, unsigned char g, unsigned char b)
{
    return (
 (((r >> 3) & 31) << 11) |
 (((g >> 2) & 63) << 5)  |
  ((b >> 3) & 31)        );
}
int main() {
    int fbfd = 0;
    struct fb_var_screeninfo vinfo;
    struct fb_fix_screeninfo finfo;
    long int screensize = 0;
 char *fbp = 0;
    int x = 0, y = 0;
    int guage_height = 20, step = 10;
    long int location = 0;
    // Open the file for reading and writing
    fbfd = open("/dev/graphics/fb0", O_RDWR);
    if (!fbfd) {
        printf("Error: cannot open framebuffer device.\n");
        exit(1);
    }
    printf("The framebuffer device was opened successfully.\n");
    // Get fixed screen information
    if (ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo)) {
        printf("Error reading fixed information.\n");
        exit(2);
    }
    // Get variable screen information
    if (ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo)) {
        printf("Error reading variable information.\n");
        exit(3);
    }
 printf("sizeof(unsigned short) = %d\n", sizeof(unsigned short));
    printf("%dx%d, %dbpp\n", vinfo.xres, vinfo.yres, vinfo.bits_per_pixel );
 printf("xoffset:%d, yoffset:%d, line_length: %d\n", vinfo.xoffset, vinfo.yoffset, finfo.line_length );
    // Figure out the size of the screen in bytes
    screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;;
    // Map the device to memory
    fbp = (char *)mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED,
                       fbfd, 0);
    if ((int)fbp == -1) {
        printf("Error: failed to map framebuffer device to memory.\n");
        exit(4);
    }
    printf("The framebuffer device was mapped to memory successfully.\n");
 //set to black color first
 memset(fbp, 0, screensize);
    //draw rectangle
    y = (vinfo.yres - guage_height) / 2 - 2;       // Where we are going to put the pixel
    for (x = step - 2; x < vinfo.xres - step + 2; x++) {
        location = (x+vinfo.xoffset) * (vinfo.bits_per_pixel/8) +
                       (y+vinfo.yoffset) * finfo.line_length;
        *((unsigned short int*)(fbp + location)) = 255;
    }
    y = (vinfo.yres + guage_height) / 2 + 2;       // Where we are going to put the pixel
    for (x = step - 2; x < vinfo.xres - step + 2; x++) {
        location = (x+vinfo.xoffset) * (vinfo.bits_per_pixel/8) +
                       (y+vinfo.yoffset) * finfo.line_length;
        *((unsigned short int*)(fbp + location)) = 255;
    }
    x = step - 2;
    for (y = (vinfo.yres - guage_height) / 2 - 2; y < (vinfo.yres + guage_height) / 2 + 2; y++) {
        location = (x+vinfo.xoffset) * (vinfo.bits_per_pixel/8) +
                       (y+vinfo.yoffset) * finfo.line_length;
        *((unsigned short int*)(fbp + location)) = 255;
    }
    x = vinfo.xres - step + 2;
    for (y = (vinfo.yres - guage_height) / 2 - 2; y < (vinfo.yres + guage_height) / 2 + 2; y++) {
        location = (x+vinfo.xoffset) * (vinfo.bits_per_pixel/8) +
                       (y+vinfo.yoffset) * finfo.line_length;
        *((unsigned short int*)(fbp + location)) = 255;
    }
    // Figure out where in memory to put the pixel
    for ( x = step; x < vinfo.xres - step; x++ ) {
        for ( y = (vinfo.yres - guage_height) / 2; y < (vinfo.yres + guage_height) / 2; y++ ) {
            location = (x+vinfo.xoffset) * (vinfo.bits_per_pixel/8) +
                       (y+vinfo.yoffset) * finfo.line_length;
            if ( vinfo.bits_per_pixel == 32 ) {
                *(fbp + location) = 100;        // Some blue
                *(fbp + location + 1) = 15+(x-100)/2;     // A little green
                *(fbp + location + 2) = 200-(y-100)/5;    // A lot of red
                *(fbp + location + 3) = 0;      // No transparency
            } else { //assume 16bpp
                unsigned char b = 255 * x / (vinfo.xres - step);
                unsigned char g = 255;     // (x - 100)/6 A little green
                unsigned char r = 255;    // A lot of red
                unsigned short int t = make16color(r, g, b);
                *((unsigned short int*)(fbp + location)) = t;
            }
        }
  //printf("x = %d, temp = %d\n", x, temp);
        //sleep to see it
        usleep(200);
    }
    //clean framebuffer
    munmap(fbp, screensize);
    close(fbfd);
    return 0;
}

 

注意,在Android环境,framebuffer设备不是象linux一样的 /dev/fb0,而是 /dev/graphics/fb0 

fbfd = open("/dev/graphics/fb0", O_RDWR);

打开framebuffer设备,

    fbp = (char *)mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED,
                       fbfd, 0);

 

将设备map到一块内存,然后就可以操作这块内存空间来显示你想画的图形了。

最后别忘了关闭设备:

    munmap(fbp, screensize);
    close(fbfd);

 

效果图如下:


 

  • 大小: 12 KB
分享到:
评论

相关推荐

    opencv-3.4.4-android-sdk/sdk/native/其他文件.zip

    opencv-3.4.4-android-sdk/sdk/native/3rdparty opencv-3.4.4-android-sdk/sdk/native/jni opencv-3.4.4-android-sdk/sdk/native/staticlibs

    android-ndk(android-ndk-r25b-linux.zip)

    Android NDK,全称为Native Development Kit,是Google提供的一款用于Android平台的C和C++库开发工具集。这个“android-ndk-r25b-linux.zip”文件是NDK的一个特定版本,即r25b,专为Linux操作系统设计。在Android...

    Android Studio JNI/NDK 编程

    Android Studio JNI/NDK编程是Android应用开发中的一个重要领域,它允许开发者利用C/C++的高效性能来编写部分应用程序,特别是在处理图形计算、硬件加速、底层库集成等场景时非常有用。JNI(Java Native Interface)...

    android-ndk(android-ndk-r26b-windows.zip)

    Android NDK,全称为Native Development Kit,是Google提供的一款用于Android平台的C和C++开发工具集。这个压缩包“android-ndk-r26b-windows.zip”包含了NDK的第26个版本,专为Windows操作系统设计。通过这个工具,...

    webrtc-android-jni.rar

    在Android平台上,WebRTC的实现涉及到JNI(Java Native Interface),这是Java平台用来调用C/C++原生代码的接口。 在这个"webrtc-android-jni.rar"压缩包中,我们可以推测其内容可能与Android应用开发中集成WebRTC...

    android-ndk(android-ndk-r25b-windows.zip)

    Android NDK,全称为Native Development Kit,是Google提供的一款用于Android平台的C和C++开发工具集。这个压缩包“android-ndk-r25b-windows.zip”包含了NDK的第25个版本,专为Windows操作系统设计。NDK的主要功能...

    JNI NDK 开发指南

    NDK(Native Development Kit)是Google提供的一套工具集,用于在Android平台上开发原生代码,通常用C++或其他本地语言编写,以提高性能或使用特定硬件功能。 JNI在Android开发中的应用主要体现在以下几个方面: 1...

    尚硅谷Android高级开发技术之JNI和NDK开发

    这算是尚硅谷培训学校推出的Android视频教程的高级教程了吧,而本套教程正是在Android开发中的核心重点开发技术(JNI/NDK),我们知道,Android的底层是Linux且Java的性能并不如C/C++好,所以在开发一些需要超高性能...

    android-ndk(android-ndk-r23b-windows.zip)

    Android NDK,全称为Native Development Kit,是Google提供的一款用于Android平台的C/C++开发工具集。这个"android-ndk-r23b-windows.zip"压缩包包含了NDK的第23个版本,专为Windows操作系统设计。NDK是Android应用...

    android-ndk(android-ndk-r26b-darwin.zip)

    Android NDK,全称为Native Development Kit,是Google提供的一款用于Android平台的C和C++原生代码开发工具。NDK允许开发者在Android应用中使用原生代码,以实现高性能、低级别的硬件交互以及利用已有的C/C++库。在...

    Android Studio JNI_NDK开发实例

    Android Studio JNI_NDK开发实例是Android应用开发中的一个重要领域,涉及到Java与原生代码(C/C++)的交互。JNI(Java Native Interface)是Java平台的一部分,它允许Java代码和其他语言写的代码进行交互。NDK...

    android-ndk-r17c-windows-x86_64.zip

    Android NDK(Native ...总的来说,Android NDK r17c是Android原生开发的重要资源,特别是对于那些需要使用GCC的开发者。理解NDK的工作原理和它提供的功能,将有助于你更有效地利用原生代码提升应用的性能和功能。

    Android NDK开发入门

    Android NDK (Native Development Kit) 是一套工具集合,用于在Android平台上使用C或C++语言进行原生代码开发。通过NDK,开发者可以直接调用系统底层API接口,实现对硬件资源更高效的利用,提升应用程序性能。本文将...

    android-sdk-NDK16.1.4479499.rar

    Android SDK NDK是Android开发中的一个重要工具,全称为Android Software Development Kit Native Development Kit。NDK允许开发者使用C和C++语言编写部分应用代码,以提高性能或利用特定硬件功能。在标题"android-...

    android-ndk(android-ndk-r25b-darwin.zip)

    Android NDK,全称为Native Development Kit,是Google提供的一款用于Android平台的C和C++库开发工具集。这个工具允许开发者在Android应用中使用原生代码,以实现高性能计算、图形处理、游戏引擎等复杂功能。`...

    android-serialport-api-DL645-1997 安卓串口编程 jni ndk cygwin

    NDK(Native Development Kit)是Google提供的一个工具集,它允许开发者在Android应用中使用原生代码。NDK包含了一系列的工具,用于编译、调试和运行原生代码,这通常是为了解决性能瓶颈或者利用特定硬件功能。在`...

    Hello-jni-ndk

    《JNI与Android NDK开发详解——以"Hello-jni-ndk"为例》 JNI(Java Native Interface)是Java平台标准的一部分,它允许Java代码和其他语言写的代码进行交互。在Android开发中,JNI通常用于调用C/C++库,以实现高...

    aarch64-linux-android-4.9(windows-64位)

    NDK(Native Development Kit)是Google提供的一套工具,允许开发者使用C和C++原生代码来开发Android应用的一部分,以实现高性能计算或利用硬件加速功能。 NDK工具链是NDK的核心组成部分,它包含了一系列的交叉...

    android-ndk-r23-windows.zip

    Android Native Development Kit (NDK) 是Google提供的一款用于开发Android应用程序的重要工具集,它允许开发者使用C和C++编写部分或全部应用程序,以利用原生代码的高性能。本文将深入探讨Android NDK R23在Windows...

    Android C++高级编程 使用NDK

    Android NDK(Native Development Kit)是Google提供的一个工具集,它允许开发者使用原生代码(如C和C++)来编写部分或全部应用,以实现高性能计算和系统级操作。本篇文章将深入探讨NDK的使用方法、优势以及相关的...

Global site tag (gtag.js) - Google Analytics