2015-07-01 3 views
2

enter image description hereЗахват экрана SurfaceView

В изображении выше я есть один SurfaceView и один Button именем Capture. SurfaceView показывает предварительный просмотр камеры. Итак, я хочу снимок экрана surfaceview, когда я нажимаю на кнопку Capture. Я попробовал много примеров и ответов, но никто из них не работает. Я просто получаю Черное Снимок экрана.

В некоторых сообщениях я также нашел скриншот от surfaceview. Поэтому, пожалуйста, помогите мне с любым альтернативным решением.

Вот некоторые ответы, которые я уже видел:

how to create and save a screenshot from a surfaceview?

Taking screenshot programmatically doesnt capture the contents of surfaceVIew

How to capture screenshot of surfaceview with background

и многое другое.

Это мой код активности вызова:

public class CamView extends Activity implements SurfaceHolder.Callback { 

    protected static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 0; 

    private SurfaceView SurView; 

    private SurfaceHolder camHolder; 

    private boolean previewRunning; 

    final Context context = this; 

    public static Camera camera = null; 

    private RelativeLayout CamView; 

    private Bitmap bmp1; 

    @SuppressWarnings("deprecation") 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_cam_view); 

     CamView = (RelativeLayout) findViewById(R.id.camview); 
     SurView = (SurfaceView) findViewById(R.id.sview); 
     camHolder = SurView.getHolder(); 
     camHolder.addCallback(this); 
     camHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 

     Button btn = (Button) findViewById(R.id.button1); 

     btn.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       camera.takePicture(null, null, mPicture); 
      } 
     }); 
    } 

    @Override 
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { 
     if (previewRunning) { 
      camera.stopPreview(); 
     } 
     Camera.Parameters camParams = camera.getParameters(); 
     Camera.Size size = camParams.getSupportedPreviewSizes().get(0); 
     camParams.setPreviewSize(size.width, size.height); 
     camera.setParameters(camParams); 
     try { 
      camera.setPreviewDisplay(holder); 
      camera.startPreview(); 
      previewRunning = true; 
     } 
     catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

    public void surfaceCreated(SurfaceHolder holder) { 
     try { 
      camera = Camera.open(); 
     } 
     catch (Exception e) { 
      e.printStackTrace(); 
      finish(); 
     } 
    } 

    @Override 
    public void surfaceDestroyed(SurfaceHolder holder) { 
     camera.stopPreview(); 
     camera.release(); 
     camera = null; 
    } 

    public void TakeScreenshot() { 
     Random num = new Random(); 
     int nu = num.nextInt(1000); 
     CamView.setDrawingCacheEnabled(true); 
     CamView.buildDrawingCache(true); 
     Bitmap bmp = Bitmap.createBitmap(CamView.getDrawingCache()); 
     CamView.setDrawingCacheEnabled(false); 
     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 = "anand" + picId + ".jpeg"; 

     File dir_image = new File(Environment.getExternalStorageDirectory() + File.separator + "anand"); 
     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(); 
      bmp1 = null; 

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

    private PictureCallback mPicture = new PictureCallback() { 
     @Override 
     public void onPictureTaken(byte[] data, Camera camera) { 

      File dir_image2 = new File(Environment.getExternalStorageDirectory() + File.separator + "anand"); 
      dir_image2.mkdirs(); 
      File tmpFile = new File(dir_image2, "TempPic.jpg"); 
      try { 
       FileOutputStream fos = new FileOutputStream(tmpFile); 
       fos.write(data); 
       fos.close(); 
      } 
      catch (FileNotFoundException e) { 
      } 
      catch (IOException e) { 
      } 

      String path = (Environment.getExternalStorageDirectory() + File.separator + "anand" + File.separator + "TempPic.jpg"); 

      BitmapFactory.Options options = new BitmapFactory.Options(); 
      options.inPreferredConfig = Bitmap.Config.ARGB_8888; 
      bmp1 = BitmapFactory.decodeFile(path, options); 

      tmpFile.delete(); 
      TakeScreenshot(); 
     } 
    }; 

    public Bitmap decodeFile(File f) { 
     Bitmap b = null; 
     try { 
      BitmapFactory.Options o = new BitmapFactory.Options(); 
      o.inJustDecodeBounds = true; 

      FileInputStream fis = new FileInputStream(f); 
      BitmapFactory.decodeStream(fis, null, o); 
      fis.close(); 
      int IMAGE_MAX_SIZE = 1000; 
      int scale = 1; 
      if (o.outHeight > IMAGE_MAX_SIZE || o.outWidth > IMAGE_MAX_SIZE) { 
       scale = (int) Math.pow(2, (int) Math.round(Math.log(IMAGE_MAX_SIZE/(double) Math.max(o.outHeight, o.outWidth))/Math.log(0.5))); 
      } 

      BitmapFactory.Options o2 = new BitmapFactory.Options(); 
      o2.inSampleSize = scale; 
      fis = new FileInputStream(f); 
      b = BitmapFactory.decodeStream(fis, null, o2); 
      fis.close(); 
     } 
     catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return b; 
    } 
} 

Это мой код макета:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:id="@+id/camview"> 

<SurfaceView 
    android:id="@+id/sview" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" /> 

<Button 
    android:id="@+id/button1" 
    style="?android:attr/buttonStyleSmall" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" /> 


</RelativeLayout> 

ответ

1

код я использую для захвата изображения;

buttonClick = (Button) findViewById(R.id.btnBasicCamTakePic); 
buttonClick.setOnClickListener(new OnClickListener() { 
    public void onClick(View v) { 
     preview.camera.takePicture(shutterCallback, rawCallback,jpegCallback); 
    } 
}); 

    ShutterCallback shutterCallback = new ShutterCallback() { 
     public void onShutter() { 
      Log.d(TAG, "onShutter'd"); 
     } 
    }; 

    /** Handles data for raw picture */ 
    PictureCallback rawCallback = new PictureCallback() { 
     public void onPictureTaken(byte[] data, Camera camera) { 
      Log.d(TAG, "onPictureTaken - raw"); 
     } 
    }; 

    /** Handles data for jpeg picture */ 
    PictureCallback jpegCallback = new PictureCallback() { 
     public void onPictureTaken(byte[] data, Camera camera) { 

      File myExternalFile = new File(BasicCam.this.getExternalFilesDir("/MyFileStorage/qrscans/"), fileName); 
      myExternalFile.delete(); 
      myExternalFile.createNewFile(); 
      FileOutputStream output = new FileOutputStream(myExternalFile); 
      output.write(data2); 
      output.flush(); 
      output.close();    
     } 
    }); 
+0

Извините, но я не могу сохранить изображение на своей SD-карте. –

+0

http://developer.android.com/training/basics/data-storage/files.html попробуйте прокрутить вниз, чтобы экономить извне, вам нужно изменить часть файла – matty357

+0

И мой ответ по-прежнему правилен для захвата BMP , вам нужно будет отрегулировать запись файла, чтобы работать для ваших нужд. – matty357

Смежные вопросы