需要帮助了解来自谷歌的 Camera2Basic 示例
Posted
技术标签:
【中文标题】需要帮助了解来自谷歌的 Camera2Basic 示例【英文标题】:Need help in understanding Camera2Basic Sample from google 【发布时间】:2017-04-26 03:01:48 【问题描述】:我正在关注谷歌的 camera2basic 示例,以了解 camera2 API。 我正在努力理解以下方法,特别是这种方法的要求以及它在做什么,因为它没有正确记录。
/**
* 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);
任何线索将不胜感激。
这里是 repo 的链接:- 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);
【讨论】:
【参考方案2】:当设备方向为Surface.ROTATION_90或Surface.ROTATION_270时,构造TextureView的transform有两个步骤:缩放和旋转。在Android demo的代码中,第一步是缩放,第二步是旋转。很难理解。
我改变了两个步骤的顺序。在我的代码中,第一步是旋转,第二步是缩放。
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);
【讨论】:
以上是关于需要帮助了解来自谷歌的 Camera2Basic 示例的主要内容,如果未能解决你的问题,请参考以下文章