2016-12-10 145 views
0

我正在關注來自google的camera2basic示例,以瞭解camera2 API。 我很難理解下面的方法,特別是這種方法的要求,它在做什麼,因爲它沒有適當的記錄。需要幫助瞭解來自Google的Camera2Basic示例

/** 
* Configures the necessary {@link android.graphics.Matrix} transformation to `mTextureView`. 
* This method should be called after the camera preview size is determined in 
* setUpCameraOutputs and also the size of `mTextureView` is fixed. 
* 
* @param viewWidth The width of `mTextureView` 
* @param viewHeight The height of `mTextureView` 
*/ 
private void configureTransform(int viewWidth, int viewHeight) { 
    Activity activity = getActivity(); 
    if (null == mTextureView || null == mPreviewSize || null == activity) { 
     return; 
    } 
    int rotation = activity.getWindowManager().getDefaultDisplay().getRotation(); 
    Matrix matrix = new Matrix(); 
    RectF viewRect = new RectF(0, 0, viewWidth, viewHeight); 
    RectF bufferRect = new RectF(0, 0, mPreviewSize.getHeight(), mPreviewSize.getWidth()); 
    float centerX = viewRect.centerX(); 
    float centerY = viewRect.centerY(); 
    if (Surface.ROTATION_90 == rotation || Surface.ROTATION_270 == rotation) { 
     bufferRect.offset(centerX - bufferRect.centerX(), centerY - bufferRect.centerY()); 
     matrix.setRectToRect(viewRect, bufferRect, Matrix.ScaleToFit.FILL); 
     float scale = Math.max(
       (float) viewHeight/mPreviewSize.getHeight(), 
       (float) viewWidth/mPreviewSize.getWidth()); 
     matrix.postScale(scale, scale, centerX, centerY); 
     matrix.postRotate(90 * (rotation - 2), centerX, centerY); 
    } else if (Surface.ROTATION_180 == rotation) { 
     matrix.postRotate(180, centerX, centerY); 
    } 
    mTextureView.setTransform(matrix); 
} 

任何潛在客戶將不勝感激。

這裏是鏈接到回購: - Camera2BasicFragment.java

回答

1

我加0°和180°

@Override public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int width, int height) { 

      configCamara(width, height); 
      openCamera(); 
      previewRotation(width, height); 
     } 
private void previewRotation(int width, int height) { 
     int rotation = getWindowManager().getDefaultDisplay().getRotation(); 
     Matrix matrix = new Matrix(); 
     RectF textureRectF = new RectF(0, 0, width, height); 
     RectF previewRectF = new RectF(0, 0, previewSize.getHeight(), previewSize.getWidth()); 
     float centerX = textureRectF.centerX(); 
     float centery = textureRectF.centerY(); 

     if (rotation == Surface.ROTATION_270 || rotation == Surface.ROTATION_90) { 
      previewRectF.offset(centerX - previewRectF.centerX(), centery - previewRectF.centerY()); 
      matrix.setRectToRect(textureRectF, previewRectF, Matrix.ScaleToFit.FILL); 
      float scale = Math.max((float) width/mTextureView.getWidth(), (float) height/mTextureView.getHeight()); 

      matrix.postScale(scale, scale, centery, centerX); 
      matrix.postRotate(90 * (rotation - 2), centerX, centery); 
     } else if (rotation == Surface.ROTATION_0) { 
      matrix.postRotate(rotation, centerX, centery); 
     }else if (rotation == Surface.ROTATION_180) { 
      matrix.postRotate(180, centerX, centery); 
     } 
     mTextureView.setTransform(matrix); 
    } 
0

當設備方向是Surface.ROTATION_90或Surface.ROTATION_270,有兩個步驟構建TextureView的變換:縮放和旋轉。在Android演示代碼中,第一步是縮放,第二步是旋轉。這很難理解。

我改變了兩個步驟的順序。在我的代碼中,第一步是旋轉,第二步是縮放。

private Matrix mTempMatrix = new Matrix(); 
private void configureTransform(int viewWidth, int viewHeight) { 
    Activity activity = getActivity(); 
    if (null == mTextureView || null == mPreviewSize || null == activity) { 
     return; 
    } 
    int rotation = activity.getWindowManager().getDefaultDisplay().getRotation(); 
    Matrix matrix = new Matrix(); 
    RectF viewRect = new RectF(0, 0, viewWidth, viewHeight); 
    float centerX = viewRect.centerX(); 
    float centerY = viewRect.centerY(); 
    if (Surface.ROTATION_90 == rotation || Surface.ROTATION_270 == rotation) { 
     //1. scale 
     matrix.postRotate(90 * (rotation - 2), centerX, centerY); 
     //2. rotate 
     RectF rotatedRect = new RectF(0, 0, viewHeight, viewWidth); 
     rotatedRect.offset(centerX - rotatedRect.centerX(), centerY - rotatedRect.centerY()); 
     RectF bufferRect = new RectF(0, 0, mPreviewSize.getWidth(), mPreviewSize.getHeight()); 
     bufferRect.offset(centerX - bufferRect.centerX(), centerY - bufferRect.centerY()); 
     mTempMatrix.setRectToRect(rotatedRect, bufferRect, Matrix.ScaleToFit.FILL); 
     matrix.postConcat(mTempMatrix); 
     float scale = Math.max(
       (float) viewHeight/mPreviewSize.getHeight(), 
       (float) viewWidth/mPreviewSize.getWidth()); 
     matrix.postScale(scale, scale, centerX, centerY); 
    } else if (Surface.ROTATION_180 == rotation) { 
     matrix.postRotate(180, centerX, centerY); 
    } 
    mTextureView.setTransform(matrix); 
}