2017-07-25 151 views
3

我正在使用SurfaceView來顯示我捕獲的預覽。我想要使​​用width = 1080,height = 1920作爲預覽。我在哪裏可以設置預覽的大小?如何設置android camera2預覽和捕捉大小?

我搜索了一個答案,但他們都是相機版本之一。我正在使用android.hardware.camera2。

private void takePreview() { 
    try { 
     final CaptureRequest.Builder previewRequestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW); 
     previewRequestBuilder.addTarget(mSurfaceHolder.getSurface()); 
     mCameraDevice.createCaptureSession(Arrays.asList(mSurfaceHolder.getSurface(), mImageReader.getSurface()), new CameraCaptureSession.StateCallback() // ③ 
     { 
      @Override 
      public void onConfigured(CameraCaptureSession cameraCaptureSession) { 
       if (null == mCameraDevice) return; 
       mCameraCaptureSession = cameraCaptureSession; 
       try { 
        previewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE); 
        previewRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_ALWAYS_FLASH); 
        previewRequestBuilder.set(CaptureRequest.JPEG_THUMBNAIL_SIZE, new Size(1080,1920)); 

        CaptureRequest previewRequest = previewRequestBuilder.build(); 
        mCameraCaptureSession.setRepeatingRequest(previewRequest, null, childHandler); 
       } catch (CameraAccessException e) { 
        Log.e("takePreview","onConfigured(CameraCaptureSession cameraCaptureSession)",e); 
       } 
      } 
      @Override 
      public void onConfigureFailed(CameraCaptureSession cameraCaptureSession) { 
       Log.e("takePreview","onConfigureFailed"); 
      } 
     }, childHandler); 
    } catch (CameraAccessException e) { 
     Log.e("takePreview","CameraAccessException"); 
    } 
} 

回答

0

看一看的Camera2Basic例如,谷歌GitHub上提供:https://github.com/googlesamples/android-Camera2Basic

存在其中選擇可選的預覽大小爲給定設備的主要片段的方法。如果你想讓你的應用程序更加靈活,而不是硬編碼大小,這可能是一個更好的方法,但即使你仍然願意堅持設置的大小,你可以看到他們如何使用結果。

總結在於,您只需將TextureView的大小設置爲任意大小的預覽大小即可。

方法名是 'chooseOptimalSize',它包括該評論/解釋:

/** 
    * Given {@code choices} of {@code Size}s supported by a camera, choose the smallest one that 
    * is at least as large as the respective texture view size, and that is at most as large as the 
    * respective max size, and whose aspect ratio matches with the specified value. If such size 
    * doesn't exist, choose the largest one that is at most as large as the respective max size, 
    * and whose aspect ratio matches with the specified value. 
    * 
    * @param choices   The list of sizes that the camera supports for the intended output 
    *       class 
    * @param textureViewWidth The width of the texture view relative to sensor coordinate 
    * @param textureViewHeight The height of the texture view relative to sensor coordinate 
    * @param maxWidth   The maximum width that can be chosen 
    * @param maxHeight   The maximum height that can be chosen 
    * @param aspectRatio  The aspect ratio 
    * @return The optimal {@code Size}, or an arbitrary one if none were big enough 
    */ 
+2

嗨@米克,我已閱讀代碼。示例使用(TextureView)texture.setDefaultBufferSize(mPreviewSize.getWidth(),mPreviewSize.getHeight())告訴攝像機使用正確的分辨率。但SurfaceView沒有這種方法。 –