[英]Camera2 full screen preview and image capture
渦流是正確的長寬比。攝像頭傳感器為4:3。 屏幕通常為16:9。 示例代碼選擇顯示整個相機預覽,因此屏幕的一部分不會被填充。 您需要拉伸以填滿整個屏幕,但是在這種情況下,捕獲的圖像包括預覽中未顯示的區域。
若要全屏查看,請在AutoFitTextureView中將onMeasure方法更改為:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
if (0 == mRatioWidth || 0 == mRatioHeight) {
setMeasuredDimension(width, height);
} else {
if (width < height * mRatioWidth / mRatioHeight) {
// setMeasuredDimension(width, width * mRatioHeight / mRatioWidth);
setMeasuredDimension(height * mRatioWidth / mRatioHeight, height);
} else {
//setMeasuredDimension(height * mRatioWidth / mRatioHeight, height);
setMeasuredDimension(width, width * mRatioHeight / mRatioWidth);
}
}
}
更新:正如Eddy所指出的,這將在全屏幕上顯示相機預覽的左上部分,而使底部和右側不在視圖范圍內,結果是圖像偏離中心。 在我的特殊情況下,對象需要水平居中,因此我修改了變換矩陣以使對象水平居中。 這是代碼:
// adjust the x shift because we are not showing the whole camera sensor on the screen, need to center the capture area
int screenWidth = Resources.getSystem().getDisplayMetrics().widthPixels;
Log.d(TAG, "screen width " + screenWidth);
int xShift = (viewWidth - screenWidth)/2;
matrix.setTranslate(-xShift, 0);
mTextureView.setTransform(matrix);
請注意,攝像頭傳感器的縱橫比通常與設備屏幕的縱橫比不匹配-傳感器通常為4:3,屏幕通常在16:9左右。 雖然兩者都有很大的差異。
由於寬高比不匹配,因此您需要確定是否要使用黑條,或縮放相機預覽以使其充滿屏幕(但是圖像的某些部分不可見)。
無論如何,要使預覽全屏顯示,您只需要使TextureView(或SurfaceView)成為整個布局即可。 因此,基本上,編輯以下布局文件: fragment_camera2_basic.xml ,刪除所有其他視圖,並在源代碼中刪除對其的引用。 或將它們更改為TextureView的頂部,而不是旁邊的-這是所有標准的Android UI布局。 而且,您需要將“活動”設為全屏 。
默認情況下,AutoFillTextureView將嘗試保持寬高比,因此會產生黑條。 如果您不希望出現黑條,則必須更改AutoFillTextureView,這不是很簡單,我在這里不再贅述。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.