在android中拍摄SurfaceView的屏幕截图

时间:2012-10-04 09:27:45

标签: android

我正在使用以下方法拍摄特定视图的屏幕截图,这是一个SurfaceView。

public void takeScreenShot(View surface_view){

    // create bitmap screen capture
    Bitmap bitmap;
    View v1 = surface_view;
    v1.setDrawingCacheEnabled(true);
    bitmap = Bitmap.createBitmap(v1.getDrawingCache());
    v1.setDrawingCacheEnabled(false);

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bitmap.compress(CompressFormat.PNG, 0, bos);
    byte[] imageData = bos.toByteArray();

}

问题是它给了我整个活动的屏幕图像。但我需要拍摄特定视图的屏幕截图。我试过其他方法,但那些给我一个黑屏作为屏幕截图,一些帖子说它需要root设备。 任何人都可以帮助我。我需要这个解决方案。帮助我....

4 个答案:

答案 0 :(得分:2)

表面视图是一个视图,但表面视图上的项目(如位图或其他对象)不是任何视图。因此,当您捕获表面视图时,它将捕获表面上的所有内容。您必须使用其他视图,如图像视图或表面视图上方的其他视图,然后捕获这些视图。

首先获取其图片想要拍摄的视图然后执行此操作

            Bitmap bitmap;
            View rv = **your view**
            rv.setDrawingCacheEnabled(true);
            bitmap = Bitmap.createBitmap(rv.getDrawingCache());
            rv.setDrawingCacheEnabled(false);

            // Write File to internal Storage

            String FILENAME = "captured.png";
            FileOutputStream fos = null;

            try {

                fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);

            } catch (FileNotFoundException e1) {

                e1.printStackTrace();
                Log.v("","FileNotFoundException: "+e1.getMessage());

            }

            try {
                bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fos);
                fos.flush();
                fos.close();

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

答案 1 :(得分:0)

尝试使用此

Bitmap b = Bitmap.createBitmap(width , height, Bitmap.Config.ARGB_8888);  
v.setDrawingCacheEnabled(false);            
v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height);
v.draw(c);

答案 2 :(得分:0)

获得位图后,您只能复制其中的一部分:

 private Bitmap copyBitmap(Bitmap src){

     //Copy the whole bitmap
     //Bitmap newBitmap = Bitmap.createBitmap(src);

     //Copy the center part only
     int w = src.getWidth();
     int h = src.getHeight();
     Bitmap newBitmap = Bitmap.createBitmap(src, w/4, h/4, w/2, h/2);

     return newBitmap;
    }

答案 3 :(得分:0)

使用这种方法它对我来说很好用,实际上surfaceview不适用于v1.setDrawingCacheEnabled(true);我通过使用此代码来完成此操作。

 jpegCallback = new PictureCallback() {

        public void onPictureTaken(byte[] data, Camera camera) {

            camera.startPreview();
            Bitmap cameraBitmap = BitmapFactory.decodeByteArray
                    (data, 0, data.length);
            Matrix matrix = new Matrix();
            matrix.postRotate(90);
            pd = new ProgressDialog(MainActivity.this);
            pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            pd.setTitle("Wait!");
            pd.setMessage("capturing image.........");
            pd.setIndeterminate(false);
            pd.show();

            progressStatus = 0;
            new Thread(new Runnable() {
                @Override
                public void run() {
                    while (progressStatus < 100) {
                        // Update the progress status
                        progressStatus += 1;

                        try {
                            Thread.sleep(20);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                // Update the progress status
                                pd.setProgress(progressStatus);
                                // If task execution completed
                                if (progressStatus == 100) {
                                    // Dismiss/hide the progress dialog
                                    pd.dismiss();
                                }
                            }
                        });
                    }
                }
            }).start();

            Bitmap rotatedBitmap = Bitmap.createBitmap(cameraBitmap, 0, 0, cameraBitmap.getWidth(), cameraBitmap.getHeight(), matrix, true);
            if (rotatedBitmap != null) {
                rotatedBitmap = combinebitmap(rotatedBitmap, bitmapMap);
                Random num = new Random();
                int nu = num.nextInt(1000);
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                rotatedBitmap.compress(Bitmap.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 + "LiveCamera");          //<---
                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(),
                            " Image saved at :LiveCamera", Toast.LENGTH_LONG).show();
                    camera.startPreview();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                MediaScannerConnection.scanFile(MainActivity.this,
                        new String[]{dir_image.toString()}, null,
                        new MediaScannerConnection.OnScanCompletedListener() {
                            public void onScanCompleted(String path, Uri uri) {
                            }
                        });


                safeToTakePicture = true;

            }

        }

    };