在Android中截取SurfaceView的屏幕截图

时间:2015-04-28 07:35:30

标签: android surfaceview

我正在尝试截取SurfaceView的截图,但它始终是黑屏的结果。我一直在寻找,但我找到的所有解决方案对我都不起作用。这是我的代码。

这是我打电话截取屏幕截图

的方法
private void takePicture(){
        if(camera!=null) {
            camera.takePicture(null, null, jpgCb);
        }
    }

这是jpg回调

jpgCb=new Camera.PictureCallback() {
        @Override
        public void onPictureTaken(final byte[] data, final Camera camera) {
            btnAccept.setVisibility(View.VISIBLE);
            btnCancel.setVisibility(View.VISIBLE);
            btnFlash.setEnabled(false);

            btnAccept.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    stopCamera();
                    new AsyncTask<byte[], String, Void>() {
                        @Override
                        protected void onPreExecute() {
                            progressDialog=new ProgressDialog(CameraActivity.this);
                            progressDialog.setCancelable(false);
                            progressDialog.setTitle(getResources().getString(R.string.saving_process));
                            progressDialog.show();
                        }

                        @Override
                        protected void onProgressUpdate(String... values) {
                            progressDialog.setMessage(values[0]);
                        }

                        @Override
                        protected Void doInBackground(byte[]... params) {
                            byte[] data=params[0];

                            File fileDir=new File(Environment.getExternalStorageDirectory().getAbsolutePath() +File.separator+"folder"+File.separator+folderName);

                            if (!fileDir.exists())
                                fileDir.mkdirs();

                            File tmpFile=new File(fileDir,"tmpFile.jpeg");

                            try{
                                FileOutputStream fos=new FileOutputStream(tmpFile);
                                fos.write(data);
                                fos.close();
                            }catch (FileNotFoundException e){
                                Toast.makeText(getApplicationContext(),"Error",Toast.LENGTH_SHORT).show();
                            }catch (IOException e){
                                Toast.makeText(getApplicationContext(),"Error",Toast.LENGTH_SHORT).show();
                            }


                            BitmapFactory.Options options=new BitmapFactory.Options();
                            options.inPreferredConfig = Bitmap.Config.ARGB_8888;

                            tmpFile.delete();
                            takeScreenshot();

                            return null;
                        }

                        @Override
                        protected void onPostExecute(Void aVoid) {
                            super.onPostExecute(aVoid);

                            try{
                                pics=loadPics();
                                setGallery();
                            }catch (NullPointerException e){
                                e.printStackTrace();
                            }

                            Toast.makeText(getApplicationContext(),getResources().getString(R.string.image_saved),Toast.LENGTH_SHORT).show();

                            if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
                                startCamera(w,h);
                            }else
                                startCamera();
                            btnAccept.setVisibility(View.GONE);
                            btnCancel.setVisibility(View.GONE);
                            btnFlash.setEnabled(true);
                            progressDialog.dismiss();
                        }
                    }.execute(data);
                }
            });

            btnCancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    stopCamera();
                    if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
                        startCamera(w,h);
                    }else
                        startCamera();
                    btnAccept.setVisibility(View.GONE);
                    btnCancel.setVisibility(View.GONE);
                    btnFlash.setEnabled(true);
                }
            });
        }
    };

这是我在回调中调用的方法,它真正拍摄了截图

public void takeScreenshot(){
    cameraContainer.setDrawingCacheEnabled(true);
    cameraContainer.buildDrawingCache(true);
    Bitmap bmp=Bitmap.createBitmap(cameraContainer.getDrawingCache());
    cameraContainer.setDrawingCacheEnabled(false);
    ByteArrayOutputStream bos=new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.JPEG,qualityValue,bos);
    byte[] bitmapData=bos.toByteArray();
    ByteArrayInputStream fis=new ByteArrayInputStream(bitmapData);

    String fileName=System.currentTimeMillis()+".jpeg";
    File fileDir=new File(Environment.getExternalStorageDirectory().getAbsolutePath() +File.separator+"folder"+File.separator+folderName);

    if (!fileDir.exists())
        fileDir.mkdirs();

    try{
        File tmpFile=new File(fileDir,fileName);
        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();
    }
}

通过这种方式,我可以将“SurfaceView内容”保存在文件中并存储,但内容为全黑屏幕。

PS:这是我在尝试解决问题时最常发现的问题(实际上,'takeScreenshot'方法来自那个答案)

编辑:这是stopCamera方法

private void stopCamera(){
    camera.setPreviewCallback(null);
    camera.stopPreview();
    camera.release();
    camera=null;
}

1 个答案:

答案 0 :(得分:0)

好吧,在你关闭相机后,我预计相机不再显示任何东西,所以表面应该是空的,意味着黑色。

无论如何,你想要达到什么目的?您已将照片保存到文件中,不是吗?