0

我正在尝试使用前置摄像头,但是却出现了后置摄像头。有什么问题?

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {
   // TODO Auto-generated method stub
   if (!previewing) 
   {
   camera = Camera.open();
   if (camera != null) 
   {
   try 
   {
       camera.setDisplayOrientation(90);
       camera.setPreviewDisplay(surfaceholder);
       Camera.Parameters p = camera.getParameters();
       p.set("camera_id",2);
       camera.setParameters(p);
       camera.startPreview();
       previewing = true;
     }
     catch (IOException e)
     {
       // TODO Auto-generated catch block
       e.printStackTrace();
     }
   }
   }
}
4

2 回答 2

2

由于 API 级别 9,有一种方法Camera.open(int)可以将所需相机的数量作为参数

于 2013-06-08T21:26:35.703 回答
2

您可以使用下面的代码来获取前置摄像头

private Camera openFrontFacingCameraGingerbread() {
    int cameraCount = 0;
    Camera cam = null;
    Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
    cameraCount = Camera.getNumberOfCameras();
    for (int camIdx = 0; camIdx < cameraCount; camIdx++) {
        Camera.getCameraInfo(camIdx, cameraInfo);
        if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
            try {
                cam = Camera.open(camIdx);
            } catch (RuntimeException e) {
                Log.e(TAG, "Camera failed to open: " + e.getLocalizedMessage());
            }
        }
    }

    return cam;
}
于 2013-06-08T21:37:53.443 回答