9

我想以编程方式截取我的游戏的屏幕截图,就像在 Eclipse DDMS 中一样。

通过此处提出的解决方案截取屏幕截图:如何以编程方式在 Android 中截取屏幕截图?在大多数其他 SO 问题中,只有 View 元素可见,但 SurfaceView 不可见。

SCREENSHOTS_LOCATIONS = Environment.getExternalStorageDirectory().toString() + "/screenshots/";
// Get root view
View view = activity.getWindow().getDecorView().getRootView();
// Create the bitmap to use to draw the screenshot
final Bitmap bitmap = Bitmap.createBitmap(screenWidth, screenHeight, Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(bitmap);

// Get current theme to know which background to use
final Theme theme = activity.getTheme();
final TypedArray ta = theme
    .obtainStyledAttributes(new int[] { android.R.attr.windowBackground });
final int res = ta.getResourceId(0, 0);
final Drawable background = activity.getResources().getDrawable(res);

// Draw background
background.draw(canvas);

// Draw views
view.draw(canvas);

// Save the screenshot to the file system
FileOutputStream fos = null;
try {
    final File sddir = new File(SCREENSHOTS_LOCATIONS);
    if (!sddir.exists()) {
        sddir.mkdirs();
    }
    fos = new FileOutputStream(SCREENSHOTS_LOCATIONS
            + System.currentTimeMillis() + ".jpg");
    if (fos != null) {
        if (!bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fos)) {
            Log.d("ScreenShot", "Compress/Write failed");
        }
        fos.flush();
        fos.close();
    }

} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

在这方面的任何帮助将不胜感激。谢谢。

4

2 回答 2

1

我希望这对你也有用。这里与上述答案的差别不大,因为我在单击按钮后使用 glsurfaceview 拍摄屏幕截图。此图像存储在提到文件夹的 sdcard 中:sdcard/emulated/0/printerscreenshots/image/...images

 My Program :

 View view_storelayout ;
 view_storelayout = findViewById(R.id.gl_surface_view);

   button onclick() {

   view_storelayout.setDrawingCacheEnabled(true); 

view_storelayout.buildDrawingCache(true);

Bitmap bmp = Bitmap.createBitmap(view_storelayout.getDrawingCache());
view_storelayout.setDrawingCacheEnabled(false); // clear drawing cache
                ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
                bmp.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();
                }

     Toast.makeText(getApplicationContext(), "myPath:"
            +dir_image.toString(), Toast.LENGTH_SHORT).show();
                Log.v("hari", "screenshots:"+dir_image.toString());

  }

 Note :
          But, here i am faced one problem. In this surfaceview , i drawn line and circle
  at runtime. after drawn some objects, when i take screenshots , its stored only for
  black image like surfaceview is stored as an image.

 And also i want to store that surfaceview image as an .dxf format(autocad format).
 i tried to stored image file as an .dxf file format.its saved successfully in 
 autocad format. but, it cant open in autocad software to edit my .dxf file.
于 2013-06-05T10:08:23.613 回答
1

大多数情况下,来自 Usman Kurd 答案的代码将不起作用。

除非您使用 Canvas 将软件中的 H.264 视频帧渲染到视图上,否则绘图缓存方法将不起作用(参见例如此答案)。

您无法从 SurfaceView 的 Surface 部分读取像素。基本问题是 Surface 是具有生产者-消费者接口的缓冲区队列,而您的应用程序位于生产者端。消费者,通常是系统合成器 (SurfaceFlinger),能够捕获屏幕截图,因为它位于管道的另一端。

截取 SurfaceView

于 2015-12-16T14:22:32.057 回答