相机表面视图的屏幕截图

时间:2015-07-01 07:05:52

标签: android opengl-es camera surfaceview

我无法在android中以编程方式捕捉相机表面视图的截图 图像保存为空白图像。我使用了Root视图。

在代码下面,我尝试了两种方式。

abstract void onItemClick(AdapterView<?> parent, View view, int position, long id)

这是第二种方式:

public void TakeScreenshot(){   
        Random num = new Random();
        int nu=num.nextInt(1000); 

        View v1 = surfaceView.getRootView();

       v1.setDrawingCacheEnabled(true);
       v1.buildDrawingCache(true);

        Bitmap bmp =  Bitmap.createBitmap(v1.getDrawingCache());

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

        String picId=String.valueOf(nu);
        String myfile="Ghost"+picId+".jpeg";

        File dir_image = new  File(Environment.getExternalStorageDirectory()+
                        File.separator+"Ultimate Entity Detector");          
        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();
            Toast.makeText(getApplicationContext(),
                           "The file is saved at :SD/Ultimate Entity Detector",Toast.LENGTH_LONG).show();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

1 个答案:

答案 0 :(得分:0)

您可以使用这些代码获取布局的屏幕截图

       public Bitmap takeScreenshot() {
       View rootView = findViewById(android.R.id.content).getRootView();
       rootView.setDrawingCacheEnabled(true);
       return rootView.getDrawingCache();
       }

它会将当前视图作为位图返回,您可以将视图存储在文件中。

这都是快乐的编码.. :)

相关问题