我制作了一个应该显示相机预览的表面视图。然而,当我用我的虚拟安卓手机打开它时,我得到一个黑白格子背景和一个大的移动绿色框,而不是实际的相机图像。
这是表面视图的代码:
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback{
private Camera camera;
private SurfaceHolder surfaceholder;
public CameraPreview(Context context) {
super(context);
surfaceholder= getHolder();
surfaceholder.addCallback(this);
surfaceholder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Camera.Parameters parameters = camera.getParameters();
parameters.setPreviewSize(width, height);
parameters.setPictureFormat(PixelFormat.JPEG);
camera.setParameters(parameters);
camera.startPreview();
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
camera = Camera.open();
try {
camera.setPreviewDisplay(surfaceholder);
} catch (IOException e) {}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
stopCam();
}
public void stopCam(){
if (camera!=null){
camera.stopPreview();
camera.release();
}
}
}