GLSurfaceView截图

点击按钮保存截图到SD卡

MainActivity Class :

  protected boolean printOptionEnable = false;

 saveImageButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Log.v("hari", "pan button clicked");
                isSaveClick = true ;
                myRenderer.printOptionEnable = isSaveClick ;

            }
        });
  //---------------------------------------------------------------------

  MyRenderer Class :

   int width_surface , height_surface ;
   @Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
    Log.i("JO", "onSurfaceChanged");
    // Adjust the viewport based on geometry changes,
    // such as screen rotation
    GLES20.glViewport(0, 0, width, height);

    float ratio = (float) width / height;


    width_surface =  width ;
    height_surface = height ;
     }
    //---------------------------------------------------------------------

   @Override
public void onDrawFrame(GL10 gl) {


    try {

    if ( printOptionEnable ) 
    {
        printOptionEnable = false ;
    Log.i("hari", "printOptionEnable if condition:"+printOptionEnable);
    int w = width_surface ;
    int h = height_surface  ;

    Log.i("hari", "w:"+w+"-----h:"+h);

    int b[]=new int[(int) (w*h)];
    int bt[]=new int[(int) (w*h)];
    IntBuffer buffer=IntBuffer.wrap(b);
    buffer.position(0);
    GLES20.glReadPixels(0, 0, w, h,GLES20.GL_RGBA,GLES20.GL_UNSIGNED_BYTE, buffer);
    for(int i=0; i<h; i++)
    {
     //remember, that OpenGL bitmap is incompatible with Android bitmap
     //and so, some correction need.        
         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-i-1)*w+j]=pix1;
         }
      }           
    Bitmap inBitmap = null ;
    if (inBitmap == null || !inBitmap.isMutable()
         || inBitmap.getWidth() != w || inBitmap.getHeight() != h) {
           inBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
      }
     //Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    inBitmap.copyPixelsFromBuffer(buffer);
    //return inBitmap ;
   // return Bitmap.createBitmap(bt, w, h, Bitmap.Config.ARGB_8888);
   inBitmap = Bitmap.createBitmap(bt, w, h, Bitmap.Config.ARGB_8888);

   ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
   inBitmap.compress(CompressFormat.JPEG, 90, bos); 
    byte[] bitmapdata = bos.toByteArray();
    ByteArrayInputStream fis = new ByteArrayInputStream(bitmapdata);

    final Calendar c=Calendar.getInstance();
     long mytimestamp=c.getTimeInMillis();
    String timeStamp=String.valueOf(mytimestamp);
    String myfile="hari"+timeStamp+".jpeg";

    dir_image=new File(Environment.getExternalStorageDirectory()+File.separator+
             "printerscreenshots"+File.separator+"image");
    dir_image.mkdirs();

    try {
        File tmpFile = new File(dir_image,myfile); 
        FileOutputStream fos = new FileOutputStream(tmpFile);

         byte[] buf = new byte[1024];
            int len;
            while ((len = fis.read(buf)) > 0) {
                fos.write(buf, 0, len);
            }
                fis.close();
                fos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

       Log.v("hari", "screenshots:"+dir_image.toString());

    }
    }catch(Exception e) {
        e.printStackTrace() ;
    }


原帖地址:http://stackoverflow.com/questions/5514149/capture-screen-of-glsurfaceview-to-bitmap

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值