首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用OpenGL的安卓屏幕截图

使用OpenGL的安卓屏幕截图
EN

Stack Overflow用户
提问于 2014-05-06 18:13:10
回答 1查看 428关注 0票数 0

我想用Android OpenGL截图。我在这里发现了一些代码,我花了几天的时间试图将它放在适当的位置,但由于我是OpenGL和安卓的新手,我想我遗漏了一些东西。代码中没有错误,但应用程序一旦启动就会崩溃。

MainActivity.java

代码语言:javascript
复制
public class MainActivity extends Activity {

private GLSurfaceView ourSurface;
public static int x=0,y=0;
public static int w;
public static int h;
public static int bt[]=new int[w*h];

 // Call back when the activity is started, to initialize the view
   @Override
 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ourSurface = new GLSurfaceView(this);       // Allocate a GLSurfaceView
        ourSurface.setRenderer(new GLRenderer(this));   // Use a custom renderer
        setContentView(ourSurface); // This activity sets to GLSurfaceView


        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        int h = metrics.heightPixels;
        int w = metrics.widthPixels;
 }

// Call back when the activity is going into the background
@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    ourSurface.onPause();
}


// Call back after onPause()
@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    ourSurface.onResume();
}
}

GLRenderer.java

代码语言:javascript
复制
public class GLRenderer implements Renderer{
   Context context;   // Application's context

public GLRenderer(Context context) {
          this.context = context;
}


@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
      // TODO Auto-generated method stub
      gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);  // Set color's clear-value to black
      gl.glClearDepthf(1.0f);            // Set depth's clear-value to farthest
      gl.glEnable(GL10.GL_DEPTH_TEST); //Enables depth-buffer for hidden surface removal
      gl.glDepthFunc(GL10.GL_LEQUAL);    // The type of depth testing to do
          gl.glShadeModel(GL10.GL_SMOOTH);   // Enable smooth shading of color
      gl.glDisable(GL10.GL_DITHER);     // Disable dithering for better performance
}

@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
    // TODO Auto-generated method stub
    }

@Override
public void onDrawFrame(GL10 gl) {
    // TODO Auto-generated method stub
     gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
     Shooting.savePixelsOnScreen(0, 0, MainActivity.w, MainActivity.h, gl);
}
}

Shooting.java

代码语言:javascript
复制
public class Shooting extends GLSurfaceView {
GLRenderer glRenderer; 
    public Shooting(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
    setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
    //glRenderer = new GLRenderer();
    setRenderer(glRenderer);
}
 public static Bitmap savePixelsOnScreen(int x, int y, int width, int height, GL10 gl)
{
      int b[]=new int[MainActivity.w*(y + MainActivity.h)];
      IntBuffer ib=IntBuffer.wrap(b);
      ib.position(0);
      gl.glReadPixels(x, 0, MainActivity.w, y+MainActivity.h, GL10.GL_RGBA,GL10.GL_UNSIGNED_BYTE, ib);

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

static Bitmap bmp = Bitmap.createBitmap(MainActivity.bt, MainActivity.w,MainActivity.h,Bitmap.Config.ARGB_8888); {

    try
   {
    File f = new File(Environment.getExternalStorageDirectory().getPath() + "testpicture.png");
           f.createNewFile();
           FileOutputStream fos=new FileOutputStream(f);
           bmp.compress(CompressFormat.PNG, 100, fos);
     try
           {
           fos.flush();
           }
     catch (IOException e)
           {
                   // TODO Auto-generated catch block
                   e.printStackTrace();
           }
      try
           {
                   fos.close();
           }
      catch (IOException e)
           {
                   // TODO Auto-generated catch block
                   e.printStackTrace();
           }
   }
      catch (FileNotFoundException e)
   {
                // TODO Auto-generated catch block
                e.printStackTrace();
   }
      catch(IOException e)
   {
                e.printStackTrace();
   }
   }
   EGL10 egl = (EGL10)EGLContext.getEGL();
   GL10 gl = (GL10)egl.eglGetCurrentContext().getGL();
   }
EN

回答 1

Stack Overflow用户

发布于 2014-05-06 23:59:45

你的主要问题在这里:

代码语言:javascript
复制
public static int w;
public static int h;
public static int bt[]=new int[w*h];

默认情况下,wh被初始化为0,因此您将bt作为一个大小为0的数组进行分配。在onCreate中更改wh的值不会自动重新分配bt数组。

可能还有一些正确性问题。例如,我不认为将整数分解为RGB分量的方式是正确的。使用ByteBuffer而不是IntBuffer来读回结果可能更容易。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23491826

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档