2010-06-08 16 views
5

Ich bin neu in der Android-Entwicklung und habe eine Zuweisung zum Lesen von Frame-Puffer-Daten nach einem bestimmten Zeitintervall.Android-Quellcode funktioniert nicht, Frame-Buffer durch glReadPixels lesen

Ich habe mit dem folgenden Code kommen:

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; 
} 

}

Auch, wenn jemand mir eine bessere Art und Weise lenken können die Framebuffer zu lesen, es wäre toll. Ich benutze Android 2.2 und virtuelles Gerät der API-Ebene 8. Ich habe viele vorherige Diskussionen durchlaufen und festgestellt, dass wir Bildpuffer nicht direkt über "/ dev/graphics/fb0" lesen können.

(edit: erste Zeilen Code Neuformatierung)

+0

Was ist das Problem, das Sie mit diesem Code haben, ist? – Alan

+0

Es gab ein Fehlerfenster auf dem Emulator von "Tut mir Leid, die Anwendung wurde unerwartet beendet" – AliR

Antwort

2

Problem gelöst, in Bezug auf die Ausführung des obigen Codes. Trotzdem konnte ich die Bildpufferdaten nicht lesen.

Ich habe die Interna gründlich untersucht und alle früheren Ansprüche über den Zugriff auf Framebuffer auf älteren Versionen von Android Pre 1.5 verfolgt.

Die Portierung auf Android Pre 1.5 hat Beiträge zum Zugriff auf Framebuffer direkt über/dev/fb0. Dies funktioniert nicht für spätere Versionen und das Android-Entwicklungsteam hat keine Pläne, wie sie in android google groups erwähnt werden.

Ich hoffe, dass dies eine Menge Leute hilft, die viel Zeit verbringen, um einen Weg zu finden.

Grüßen, Ali

+2

Gelöst wie? Bitte posten Sie Ihre Lösung – davenpcj

Verwandte Themen