2010-06-08 18 views
5

Tôi mới phát triển Android và có nhiệm vụ đọc dữ liệu bộ đệm khung sau một khoảng thời gian nhất định.Mã nguồn Android không hoạt động, đọc bộ đệm khung qua glReadPixels

tôi đã đưa ra đoạn mã sau:

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

}

Ngoài ra, nếu một số người ta có thể trực tiếp tôi để cách tốt hơn để đọc framebuffer nó sẽ là tuyệt vời. Tôi đang sử dụng Android 2.2 và thiết bị ảo cấp API 8. Tôi đã trải qua nhiều cuộc thảo luận trước đó và thấy rằng chúng tôi không thể đọc bộ đệm khung trực tiếp thông qua "/ dev/graphics/fb0".

(chỉnh sửa: định dạng lại dòng mã đầu tiên)

+0

Sự cố bạn gặp phải với mã này là gì? – Alan

+0

Nó đã đưa ra cửa sổ lỗi trên trình giả lập "Rất tiếc, ứng dụng đã dừng đột ngột" – AliR

Trả lời

2

vấn đề được giải quyết, liên quan đến việc thực thi mã trên. Nhưng tôi vẫn không thể đọc dữ liệu bộ đệm khung.

Tôi đã nghiên cứu sâu về nội bộ và truy tìm tất cả các khiếu nại trước đây về truy cập bộ đệm khung trên các phiên bản cũ hơn của Android trước 1.5.

Việc chuyển trên Android trước 1.5 không có bài đăng truy cập bộ đệm khung trực tiếp thông qua/dev/fb0. Điều này không làm việc cho các phiên bản sau này và nhóm phát triển Android không có kế hoạch như đã đề cập trong các nhóm google android.

Tôi hy vọng điều này sẽ giúp rất nhiều người dành nhiều thời gian của mình để tìm ra cách.

liên quan, Ali

+2

Giải quyết như thế nào? xin vui lòng gửi giải pháp của bạn – davenpcj

Các vấn đề liên quan