如何使用前置摄像头

时间:2013-06-08 21:22:59

标签: android

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

@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();
     }
   }
   }
}

2 个答案:

答案 0 :(得分:2)

从API级别9开始,有一个方法Camera.open(int),您可以在其中提供所需摄像机的编号作为参数

答案 1 :(得分: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;
}