我对表面 View 和相机预览的问题感到疯狂。我的问题是预览(在纵向模式下)被拉伸(stretch),我不明白为什么(在 Nexus 7 设备上)。 这是包含 SurfaceView 的布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:context=".pic" >
<SurfaceView
android:id="@+id/surfaceview"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<View
android:id="@+id/bottom_border"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_alignParentBottom="true" />
<!-- android:layout_below="@+id/surfaceview" -->
<ImageView
android:id="@+id/footer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:scaleType="fitXY"
android:src="@drawable/footer" />
</RelativeLayout>
因此,页脚 ImageView 包含一个带有其他 View 的页脚(用于拍照、接受矿石、拒绝图片等的按钮),而 View (以编程方式初始化)是一个半透明 View ,显示图片的用户限制(拍摄的图片将在平方)。 这就是我初始化 Camera 对象的方式(surfaceChanged 方法)
Camera.Parameters params = mCamera.getParameters();
Camera.Size result = getBestPreviewSize(params, width, height);
params.setPreviewSize(result.width, result.height);
params.setPictureFormat(ImageFormat.JPEG);
params.setJpegQuality(100);
params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
// params.set
params.setPictureSize(dpWidth, dpWidth);
// default orientation is in landscape
params.setRotation(90);
mCamera.setParameters(params);
mCamera.startPreview();
这就是我计算最佳预览尺寸的方法:
public Camera.Size getBestPreviewSize(Camera.Parameters params, int width,
int height) {
Camera.Size result = null;
for (Camera.Size size : params.getSupportedPreviewSizes()) {
if (size.width <= width && size.height <= height) {
if (result == null) {
result = size;
} else {
int resultArea = result.width * result.height;
int newArea = size.width * size.height;
if (newArea > resultArea) {
result = size;
}
}
}
}
return result;
}
支持的预览尺寸为
w: 1920 h: 1080
w: 1280 h: 768
w: 1280 h: 720
w: 1024 h: 768
w: 800 h: 600
w: 800 h: 480
w: 720 h: 480
w: 640 h: 480
w: 352 h: 288
w: 320 h: 240
w: 176 h: 144
选择的是
1024 768
预览图像比实际图像显得更薄、更高。 我该如何解决它?
最佳答案
正在缩放预览以匹配 SurfaceView 的大小。您需要使用可调整的布局来包装 SurfaceView,以保持相机输入的宽高比。
其中一个例子是 AspectFrameLayout类(class) Grafika 。您可以看到它在显示相机预览的 Activity 中使用(例如“连续捕获”和“显示+捕获相机”)。您的布局将如下所示:
<com.android.grafika.AspectFrameLayout
android:id="@+id/continuousCapture_afl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true" >
<SurfaceView
android:id="@+id/continuousCapture_surfaceView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center" />
</com.android.grafika.AspectFrameLayout>
然后从相机参数中设置尺寸:
Camera.Size cameraPreviewSize = parms.getPreviewSize();
AspectFrameLayout layout = (AspectFrameLayout) findViewById(R.id.continuousCapture_afl);
layout.setAspectRatio((double) cameraPreviewSize.width / cameraPreviewSize.height);
关于android - SurfaceView相机预览: find camera preview size without stretch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23537463/