我正在開發一款在SurfaceView
上繪製相機的應用程序。一開始我發現了觀點的麻煩。他們沒有正確顯示。所以我使用了下面的方法來修正長寬比問題:Android中正確的寬高比
注意:這個方法在幾個地方被發現是爲了糾正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。它有一個決議,該方法認爲是最appropiate之一,但它顯示的圖像與郵筒在人像模式中,像下面的圖片顯示:
這究竟是爲什麼?如何在縱向模式下解決這個郵筒問題?
您可以選擇裁剪圖像而不是顯示黑色邊距。 – 2014-11-21 10:03:42
怎樣才能做到這一點?這將是我的解決方案之一。第一個,莊稼形象。第二個,試着將它放在我考慮寬度的空間中。 – Sonhja 2014-11-21 11:05:50
裁剪預覽的可靠方法涉及使用[SurfaceTexture](http://developer.android.com/reference/android/graphics/SurfaceTexture.html)。 – 2014-11-21 11:12:34