【问题标题】:Taking screen shot of a SurfaceView in android在android中截取SurfaceView的屏幕截图
【发布时间】:2012-10-04 09:27:44
【问题描述】:

我正在使用以下方法对 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();

}

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

【问题讨论】:

  • 您好,“问题是它给了我整个活动屏幕图像”,您的意思是您能够捕获整个表面和表面视图吗?我面临同样的问题,但是使用您的代码,我仍然可以获得所有元素可见的整个活动,但 SurfaceView (我得到一个黑框)。你能告诉我surface_view到底是什么吗?

标签: android


【解决方案1】:

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

首先获取要拍照的视图,然后执行此操作

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

【讨论】:

  • 我试着用这种方式截取SurfaceView,但是输出位图的宽高是-1……我做错了什么?
【解决方案2】:

试试这个

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

【讨论】:

    【解决方案3】:

    一旦你得到位图,你可以像这样只复制它的一部分:

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

    【讨论】:

      【解决方案4】:

      使用这种方法它对我来说效果很好,实际上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;
      
                  }
      
              }
      
          };
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-25
        • 2017-10-09
        • 1970-01-01
        • 2021-07-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-22
        相关资源
        最近更新 更多