2017-05-08 14 views
0

camera2 apiでカメラをプレビューするには、SurfaceViewを使用します。しかし、それは常に次の写真のように部分的に黒です。Camera2のプレビュー時にSurfaceViewが塗りつぶされることはありませんか?

My 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> 
をお試しください
関連する問題