我正在开发一款camera2应用程序。在Nexus 5和Nexus 4上一切正常,但在LG G2预览视频是拉伸的(仅适用于后置摄像头,前置摄像头一切都很好)。我正在设置SurfaceTexture:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
float ratioSurface = width > height ? (float) width / height : (float) height / width;
float ratioPreview = (float) mRatioWidth / mRatioHeight;
int scaledHeight = height;
int scaledWidth = width;
if (ratioPreview > ratioSurface) {
scaledHeight = (int) (((float) mRatioWidth / mRatioHeight) * width);
} else if (ratioPreview < ratioSurface) {
scaledWidth = (int) (height / ((float) mRatioWidth / mRatioHeight));
}
setMeasuredDimension(scaledWidth, scaledHeight);
}
在setAspectRatio()之后,我称之为setSurfaceTexture():
private void setSurfaceTexture(Size size) {
final Matrix matrix = new Matrix();
int width = resource.cameraTexture.getWidth();
int height = resource.cameraTexture.getHeight();
int rotation = controller.getRotation();
if (Surface.ROTATION_90 == rotation || Surface.ROTATION_270 == rotation) {
matrix.postRotate(90 * (rotation - 2), width / 2, height / 2);
}
resource.mForegroundHandler.post(new Runnable() {
@Override
public void run() {
resource.cameraTexture.setTransform(matrix);
}
});
}
我试图使用SurfaceView,但结果是一样的:当切换到视频模式时,预览会被拉伸。我尝试了很多方法,但结果总是一样的。 求你帮帮我......
编辑:在使用CyanogenMod的LG G2上,视频仍在延伸,但在官方Lollipop的手机中,下一个问题是:LEGACY设备并不总是支持预览视频16:9,因此我将视频大小更改为4:3。