【发布时间】:2014-11-21 08:54:51
【问题描述】:
我正在开发一个在SurfaceView 上绘制相机的应用程序。一开始我发现观点有问题。它们没有正确显示。所以我用下面的方法来修正纵横比的问题:
注意:在多个地方都可以找到此方法,作为校正 Android 相机纵横比的方法。
private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) {
final double ASPECT_TOLERANCE = 0.1;
double targetRatio;
// Check wheter is portrait or landscape
if (orientation == 1)
targetRatio = (double) h / w;
else
targetRatio = (double) w / h;
if (sizes == null)
return null;
Size optimalSize = null;
double minDiff = Double.MAX_VALUE;
int targetHeight = h;
// Try to find an size match aspect ratio and size
for (Size size : sizes) {
double ratio = (double) size.width / size.height;
if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE)
continue;
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
// Cannot find the one match the aspect ratio, ignore the requirement
if (optimalSize == null) {
minDiff = Double.MAX_VALUE;
for (Size size : sizes) {
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
}
return optimalSize;
}
这很完美,我的相机显示正确。但我发现新设备存在问题,例如 LG G3。它具有该方法认为最合适的分辨率,但它在纵向模式下显示带有邮筒的图像,如下图所示:
为什么会这样?在竖屏模式下如何解决这个邮筒?
【问题讨论】:
-
您可以选择裁剪图像而不是显示黑边。
-
如何做到这一点?那将是我的解决方案之一。第一个,裁剪图像。第二个,试着把它放在我考虑过宽度的空间里。
-
裁剪预览的可靠方法是使用SurfaceTexture。
标签: android android-camera aspect-ratio