【发布时间】:2014-05-08 09:10:58
【问题描述】:
Surfaceview 和相机预览的问题让我快疯了。我的问题是预览(在 portrati 模式下)被拉伸了,我不明白为什么(在 nexus 7 设备上)。 这是包含表面视图的布局:
<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(以编程方式初始化)是一个半透明视图,显示图片的用户限制(拍摄的图片将平方)。 这就是我初始化 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
预览中的图像看起来比实际更薄、更高。 我该如何解决?
【问题讨论】:
标签: android camera android-camera surfaceview