简体   繁体   English

在android中拍摄SurfaceView的屏幕截图

[英]Taking screen shot of a SurfaceView in android

I am using following method to taking screen shot of a particular view which is a SurfaceView. 我正在使用以下方法来拍摄特定视图的屏幕截图,这是一个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();

}

the problem is its giving me the whole activity screen image. 问题是它给了我整个活动的屏幕图像。 But I need to take screen shot of the particular view. 但我需要拍摄特定视图的屏幕截图。 I tried other ways but those give me a black screen as screen shot, some posts says that it requires rooted device. 我试过其他方法,但那些给我一个黑屏作为屏幕截图,一些帖子说它需要root设备。 Can any one help me please. 任何人都可以帮助我。 I'm in need of this solution. 我需要这个解决方案。 Help me.... 帮我....

Surface view is a view but item on surface view like bitmap or other object are not any view. 曲面视图是一个视图,但表面视图上的项目(如位图或其他对象)不是任何视图。 So while you capture surface view it will capture every thing on the surface. 因此,当您捕获表面视图时,它将捕获表面上的所有内容。 You have to use other view like image view or other above the surface view and then capture those view. 您必须使用其他视图,如图像视图或表面视图上方的其他视图,然后捕获这些视图。

First get the view whose picture want to take then do this 首先获取其图片想要拍摄的视图,然后执行此操作

            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();
            } 

Try using this one 尝试使用这个

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);

Once you get the bitmap, you can copy only a part of it like this: 获得位图后,您只能复制其中的一部分,如下所示:

 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;
    }

use this method its works fine for me actually surfaceview does not work with v1.setDrawingCacheEnabled(true); 使用这种方法它对我来说很好,实际上surfaceview不能与v1.setDrawingCacheEnabled(true)一起使用; i do this by using this code. 我这样做是通过使用此代码。

 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;

            }

        }

    };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM