2016-12-02 12 views
0

매초마다 카메라 미리보기에서 스크린 샷을 찍고 싶습니다.매초 SurfaceView의 프레임을 얻는 방법은 무엇입니까?

SurfaceView를 사용하여 카메라 미리보기를 보여줍니다. 나는 초당 미리보기 (스크린 샷)를 받아야하지만 사진 찍기는 사용하지 않아야한다.

나는 방법 mCamera.setPreviewCallbackWithBuffer에 대해 알고 있지만 프레임 하나만 가져갈 수 있습니다. 매 초마다 업데이트되도록하려면 MediaRecorder을 시작하고 비디오를 녹화해야합니다. 그러나 비디오의 경우 많은 메모리를 사용할 수 있다는 것을 의미하는 outputFile을 설정해야합니다.

mCamera.setPreviewCallbackWithBuffer(new Camera.PreviewCallback() { 
     @Override 
     public void onPreviewFrame(byte[] data, Camera camera) { 
      Camera.Parameters parameters = camera.getParameters(); 
      int width = parameters.getPreviewSize().width; 
      int height = parameters.getPreviewSize().height; 

      YuvImage yuv = new YuvImage(data, parameters.getPreviewFormat(), width, height, null); 

      ByteArrayOutputStream out = new ByteArrayOutputStream(); 
      yuv.compressToJpeg(new Rect(0, 0, width, height), 50, out); 

      byte[] bytes = out.toByteArray(); 
      final Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length); 

     } 
    }); 

출력 파일을 설정하고 매 순간 사진을 찍지 않고 어떻게 할 수 있습니까?

답변

0

카메라 미리보기를 표시하기 위해 SurfaceView을 사용하는 경우 매초 Camera#takePicture 번으로 전화를 시도 할 수 있습니다.

약 1 초마다 일정을 잡으려면 모든보기에서 postDelayed 메서드를 사용할 수 있습니다. 예를 들면 :

private Runnable capturePreview = new Runnable() { 
    @Override 
    public void run() { 
     camera.takePicture(null, null, callback); 

     // Run again after approximately 1 second. 
     surfaceView.postDelayed(this, 1000); 
    } 
} 

private Camera.PictureCallback callback = new Camera.PictureCallback() { 
    @Override 
    public void onPictureTaken(byte[] data, Camera camera) { 
     Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length); 

     // Do whatever you need with your bitmap. 

     // Consider freeing the memory afterwards. 
     bitmap.recycle(); 
    } 
} 

그리고 당신은 미리보기가 준비 될 때마다 호출하여 시작할 수 있습니다 :

surfaceView.postDelayed(capturePreview, 1000); 

을 그리고 미리보기가 더 이상 표시 될 때마다 멈추지 :

surfaceView.removeCallbacks(capturePreview); 

당신은 TextureView를 사용하는 경우 getBitmap()을 사용하면 쉽게 현재 내용을 가져올 수 있습니다.

textureView.postDelayed(capturePreview, 1000); 

를 중지 : 다시

private Runnable capturePreview = new Runnable() { 
    @Override 
    public void run() { 
     Bitmap preview = textureView.getBitmap(); 

     // Do whatever you need with the bitmap. 

     // Run again after approximately 1 second. 
     textureView.postDelayed(this, 1000); 
    } 
}, 1000); 

그리고 시작 :

textureView.removeCallbacks(capturePreview); 

따라서 위의 코드는 같은 것이된다