【发布时间】:2010-07-09 14:48:06
【问题描述】:
在我的活动中,我在 SurfaceView 上显示相机预览。它在 Nexus One 和 HTC Desire 上运行得非常好,但在三星 Galaxy S 上,我看到奇怪的线条、奇怪的比例和一切三倍。请参阅下面的屏幕截图。
这个问题似乎和这个类似: camera preview on android - strange lines on 1.5 version of sdk 但是那里的cmets都没有帮助。我尝试用相机参数交换高度,宽度,但差别不大。
(旁注:我的活动始终处于横向模式,已修复。我在清单中将该修复作为 screenOrientation 参数,以防万一这很重要)。
我的 SurfaceHolderCallback 的代码(我活动中的相关内部类):
class SurfaceHolderCallback implements SurfaceHolder.Callback {
private static final int IMAGE_WIDTH = 512;
private static final int IMAGE_HEIGHT = 384;
private static final String ORIENTATION = "orientation";
private static final String ROTATION = "rotation";
private static final String PORTRAIT = "portrait";
private static final String LANDSCAPE = "landscape";
public void surfaceCreated(SurfaceHolder holder) {
camera = Camera.open();
//Surface.setOrientation(Display.DEFAULT_DISPLAY,Surface.ROTATION_90);
Parameters p = camera.getParameters();
p.setPictureSize(IMAGE_WIDTH, IMAGE_HEIGHT);
p.set(ORIENTATION, PORTRAIT);
p.set(ROTATION, 90);
// p.setPreviewSize(640, 480);
Camera.Size s = p.getSupportedPreviewSizes().get(0);
Log.d(APP, "preview params " + s.width +"/"+ s.height);
p.setPreviewSize( s.width,s.height );
p.setPictureFormat(PixelFormat.JPEG);
p.set("flash-mode", "auto");
camera.setParameters(p);
try {
camera.setPreviewDisplay(surfaceHolder);
} catch (Throwable ignored) {
Log.e(APP, "set preview error.", ignored);
}
}
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
if (isPreviewRunning) {
camera.stopPreview();
}
try {
camera.startPreview();
} catch(Exception e) {
Log.d(APP, "Cannot start preview", e);
}
isPreviewRunning = true;
}
...
【问题讨论】: