2017-05-08 11 views
0

camera2 api로 카메라를 미리 보려면 SurfaceView을 사용합니다. 그러나 그것은 그림을 따라 항상 부분적으로 검은 색입니다.카메라 2를 미리 볼 때 SurfaceView가 채워지지 않습니다?

내 xml 코드.

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

    <android.support.v7.widget.Toolbar 
     android:id="@+id/tool_bar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="?attr/colorPrimary" 
     android:elevation="4dp" 
     android:theme="@style/ThemeOverlay.AppCompat.ActionBar" 
     app:popupTheme="@style/ThemeOverlay.AppCompat.Light"> 
     <TextView 
      android:text="Egos" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"/> 
    </android.support.v7.widget.Toolbar> 

    <Button 
     android:id="@+id/button" 
     android:text="Record" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:onClick="record"/> 

    <SurfaceView 
     android:id="@+id/surface_view" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@id/tool_bar" 
     android:layout_above="@id/button"/> 

</RelativeLayout> 

부분 콜백.

mSurfaceView.getHolder().addCallback(new SurfaceHolder.Callback() { 
    @Override 
    public void surfaceCreated(SurfaceHolder holder) { 
     mSurfaceHolder = holder; 
     try { 
      tryPre(); 
     } catch (CameraAccessException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

사전 방법.

private void tryPre() throws CameraAccessException { 
    if (mSurfaceHolder == null) { 
     return; 
    } 
    // mSurfaceHolder.setFixedSize(1080, 1530); // no use 
    List<Surface> outputSurfaces = new ArrayList<>(1); 
    Surface surface = mSurfaceHolder.getSurface(); 
    outputSurfaces.add(surface); 
    final CaptureRequest.Builder captureBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW); 
    captureBuilder.addTarget(surface); 
    final int rotation = getWindowManager().getDefaultDisplay().getRotation(); 
    captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, ORIENTATIONS.get(rotation)); 
    mCameraDevice.createCaptureSession(outputSurfaces, new CameraCaptureSession.StateCallback() { 
     @Override 
     public void onConfigured(CameraCaptureSession session) { 
      if (null == mCameraDevice) { 
       return; 
      } 
      try { 
       captureBuilder.set(CaptureRequest.CONTROL_AF_MODE, 
         CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE); 
       session.setRepeatingRequest(captureBuilder.build(), 
         captureCallback, mHandler); 
      } catch (CameraAccessException e) { 
       e.printStackTrace(); 
      } 
     } 
     @Override 
     public void onConfigureFailed(CameraCaptureSession session) { 
     } 
    }, mHandler); 
} 

enter image description here

답변

0

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
xmlns:app="http://schemas.android.com/apk/res-auto"> 

<android.support.v7.widget.Toolbar 
    android:id="@+id/tool_bar" 
    android:layout_width="match_parent" 
    android:layout_height="?attr/actionBarSize" 
    android:background="?attr/colorPrimary" 
    android:elevation="4dp" 
    android:theme="@style/ThemeOverlay.AppCompat.ActionBar" 
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"> 
    <TextView 
     android:text="Egos" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 
</android.support.v7.widget.Toolbar> 

<SurfaceView 
    android:id="@+id/surfaceView" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_below="@+id/tool_bar"/> 

<ImageButton 
    android:id="@+id/captureImage" 
    android:layout_width="60dp" 
    android:layout_height="60dp" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true" 
    android:layout_marginBottom="@dimen/activity_vertical_margin" 
    android:background="@android:color/transparent" 
    android:scaleType="fitXY" 
    android:src="@android:drawable/presence_video_away"/> 

</RelativeLayout> 
시도