Android源代码不工作,通过glReadPixels读取帧缓冲区

发布于 2024-09-04 22:00:06 字数 3958 浏览 5 评论 0原文

我是 Android 开发新手,有一项任务是在指定的时间间隔后读取帧缓冲区数据。

我想出了以下代码:

public class mainActivity extends Activity {
    Bitmap mSavedBM;
    private EGL10 egl;
    private EGLDisplay display;
    private EGLConfig config;    
    private EGLSurface surface;
    private EGLContext eglContext;
    private GL11 gl;
    protected int width, height;


 //Called when the activity is first created. 
@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);

    // get the screen width and height
    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    int screenWidth = dm.widthPixels;
    int screenHeight = dm.heightPixels; 

    String SCREENSHOT_DIR = "/screenshots";
    initGLFr(); //GlView initialized.
    savePixels( 0, 10, screenWidth, screenHeight, gl); //this gets the screen to the mSavedBM.
    saveBitmap(mSavedBM, SCREENSHOT_DIR, "capturedImage");

    //Now we need to save the bitmap (the screen capture) to some location.
    setContentView(R.layout.main); //This displays the content on the screen

}
private void initGLFr()
{
    egl = (EGL10) EGLContext.getEGL();
    display = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
    int[] ver = new int[2];
    egl.eglInitialize(display, ver);

    int[] configSpec = {EGL10.EGL_NONE};
    EGLConfig[] configOut = new EGLConfig[1];
    int[] nConfig = new int[1];
    egl.eglChooseConfig(display, configSpec, configOut, 1, nConfig);
    config = configOut[0];
    eglContext = egl.eglCreateContext(display, config, EGL10.EGL_NO_CONTEXT, null);
    surface = egl.eglCreateWindowSurface(display, config, SurfaceHolder.SURFACE_TYPE_GPU, null);
    egl.eglMakeCurrent(display, surface, surface, eglContext);
    gl = (GL11) eglContext.getGL();
}
public void savePixels(int x, int y, int w, int h, GL10 gl)
{
    if (gl == null)
            return;

     synchronized (this) {
     if (mSavedBM != null) {
     mSavedBM.recycle();
     mSavedBM = null;
     }
     }

    int b[] = new int[w * (y + h)];
    int bt[] = new int[w * h];
    IntBuffer ib = IntBuffer.wrap(b);
    ib.position(0);
    gl.glReadPixels(x, 0, w, y + h, GL10.GL_RGBA,GL10.GL_UNSIGNED_BYTE,ib);

    for (int i = 0, k = 0; i < h; i++, k++)
    {
        //OpenGLbitmap is incompatible with Android bitmap
        //and so, some corrections need to be done.
            for (int j = 0; j < w; j++)
            {
                    int pix = b[i * w + j];
                    int pb = (pix >> 16) & 0xff;
                    int pr = (pix << 16) & 0x00ff0000;
                    int pix1 = (pix & 0xff00ff00) | pr | pb;
                    bt[(h - k - 1) * w + j] = pix1;
            }
    }

    Bitmap sb = Bitmap.createBitmap(bt, w, h, Bitmap.Config.ARGB_8888);
    synchronized (this)
    {
        mSavedBM = sb;
    }
} 

static String saveBitmap(Bitmap bitmap, String dir, String baseName) {
    try {
        File sdcard = Environment.getExternalStorageDirectory();
        File pictureDir = new File(sdcard, dir);
        pictureDir.mkdirs();
        File f = null;
        for (int i = 1; i < 200; ++i) {
            String name = baseName + i + ".png";
            f = new File(pictureDir, name);
            if (!f.exists()) {
                break;
            }
        }
        if (!f.exists()) {
            String name = f.getAbsolutePath();
            FileOutputStream fos = new FileOutputStream(name);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
            fos.flush();
            fos.close();
            return name;
        }
    } catch (Exception e) {

    } finally {

        //if (fos != null) {
         //   fos.close();
       // }

    }
    return null;
}

}

另外,如果有人可以指导我更好的方法来读取帧缓冲区,那就太好了。我使用的是 Android 2.2 和 API 级别 8 的虚拟设备。 我经历了很多之前的讨论,发现我们不能直接通过“/dev/graphics/fb0”读取帧缓冲区。

(编辑:重新格式化第一行代码)

I am new to Android development and have an assignment to read frame buffer data after a specified interval of time.

I have come up with the following code:

public class mainActivity extends Activity {
    Bitmap mSavedBM;
    private EGL10 egl;
    private EGLDisplay display;
    private EGLConfig config;    
    private EGLSurface surface;
    private EGLContext eglContext;
    private GL11 gl;
    protected int width, height;


 //Called when the activity is first created. 
@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);

    // get the screen width and height
    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    int screenWidth = dm.widthPixels;
    int screenHeight = dm.heightPixels; 

    String SCREENSHOT_DIR = "/screenshots";
    initGLFr(); //GlView initialized.
    savePixels( 0, 10, screenWidth, screenHeight, gl); //this gets the screen to the mSavedBM.
    saveBitmap(mSavedBM, SCREENSHOT_DIR, "capturedImage");

    //Now we need to save the bitmap (the screen capture) to some location.
    setContentView(R.layout.main); //This displays the content on the screen

}
private void initGLFr()
{
    egl = (EGL10) EGLContext.getEGL();
    display = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
    int[] ver = new int[2];
    egl.eglInitialize(display, ver);

    int[] configSpec = {EGL10.EGL_NONE};
    EGLConfig[] configOut = new EGLConfig[1];
    int[] nConfig = new int[1];
    egl.eglChooseConfig(display, configSpec, configOut, 1, nConfig);
    config = configOut[0];
    eglContext = egl.eglCreateContext(display, config, EGL10.EGL_NO_CONTEXT, null);
    surface = egl.eglCreateWindowSurface(display, config, SurfaceHolder.SURFACE_TYPE_GPU, null);
    egl.eglMakeCurrent(display, surface, surface, eglContext);
    gl = (GL11) eglContext.getGL();
}
public void savePixels(int x, int y, int w, int h, GL10 gl)
{
    if (gl == null)
            return;

     synchronized (this) {
     if (mSavedBM != null) {
     mSavedBM.recycle();
     mSavedBM = null;
     }
     }

    int b[] = new int[w * (y + h)];
    int bt[] = new int[w * h];
    IntBuffer ib = IntBuffer.wrap(b);
    ib.position(0);
    gl.glReadPixels(x, 0, w, y + h, GL10.GL_RGBA,GL10.GL_UNSIGNED_BYTE,ib);

    for (int i = 0, k = 0; i < h; i++, k++)
    {
        //OpenGLbitmap is incompatible with Android bitmap
        //and so, some corrections need to be done.
            for (int j = 0; j < w; j++)
            {
                    int pix = b[i * w + j];
                    int pb = (pix >> 16) & 0xff;
                    int pr = (pix << 16) & 0x00ff0000;
                    int pix1 = (pix & 0xff00ff00) | pr | pb;
                    bt[(h - k - 1) * w + j] = pix1;
            }
    }

    Bitmap sb = Bitmap.createBitmap(bt, w, h, Bitmap.Config.ARGB_8888);
    synchronized (this)
    {
        mSavedBM = sb;
    }
} 

static String saveBitmap(Bitmap bitmap, String dir, String baseName) {
    try {
        File sdcard = Environment.getExternalStorageDirectory();
        File pictureDir = new File(sdcard, dir);
        pictureDir.mkdirs();
        File f = null;
        for (int i = 1; i < 200; ++i) {
            String name = baseName + i + ".png";
            f = new File(pictureDir, name);
            if (!f.exists()) {
                break;
            }
        }
        if (!f.exists()) {
            String name = f.getAbsolutePath();
            FileOutputStream fos = new FileOutputStream(name);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
            fos.flush();
            fos.close();
            return name;
        }
    } catch (Exception e) {

    } finally {

        //if (fos != null) {
         //   fos.close();
       // }

    }
    return null;
}

}

Also, if some one can direct me to better way to read the framebuffer it would be great. I am using Android 2.2 and virtual device of API level 8.
I have gone through many previous discussions and have found that we can not read frame buffer directly through the "/dev/graphics/fb0".

(edit: reformatting first lines of code)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你怎么这么可爱啊 2024-09-11 22:00:06

关于上述代码的执行,问题已解决。但我仍然无法读取帧缓冲区数据。

我深入研究了内部结构,并追踪了之前所有有关在 Android 1.5 之前的旧版本上访问帧缓冲区的声明。

Android 1.5 之前的移植确实有关于直接通过 /dev/fb0 访问帧缓冲区的帖子。这不适用于更高版本,并且 Android 开发团队没有像他们在 android google groups 中提到的计划。

我希望这可以帮助很多人花费大量时间来找出方法。

问候,
阿里

problem solved, regarding the execution of the above code. But still I could not read the frame buffer data.

I have researched in depth the internals and traced all the previous claims about accessing framebuffer on older versions of Android pre 1.5.

The porting on Android pre 1.5 does have posts on accessing framebuffer directly through /dev/fb0. This does not work for later versions and the Android Development team has no plans as mentioned by them in android google groups.

I hope this helps a lot of people spending a lot of their time to figure out a way.

regards,
Ali

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文