后置摄像头android无法正常工作

时间:2013-04-30 18:34:10

标签: android camera back

我有问题。我想从中执行示例应用程序 http://www.vogella.com/articles/AndroidCamera/article.html 有用。 但是当我将代码CAMERA_FACING_FRONT更改为CAMERA_FACING_BACK时,我在LogCat中看到:Camera Error -1

我在三星Galaxy SII上执行它

private int findFrontFacingCamera() {
int cameraId = -1;
// Search for the front facing camera
int numberOfCameras = Camera.getNumberOfCameras();
for (int i = 0; i < numberOfCameras; i++) {
  CameraInfo info = new CameraInfo();
  Camera.getCameraInfo(i, info);
  if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
    Log.d(DEBUG_TAG, "Camera found");
    cameraId = i;
    break;
  }
}
return cameraId;

}

当前代码

private int findFrontFacingCamera() {
    int cameraId = -1;
    // Search for the front facing camera
    int numberOfCameras = Camera.getNumberOfCameras();
    for (int i = 0; i < numberOfCameras; i++) {
      CameraInfo info = new CameraInfo();
      Camera.getCameraInfo(i, info);
      if (info.facing == CameraInfo.CAMERA_FACING_BACK) {
        Log.d(DEBUG_TAG, "Camera found " + info.facing );
        cameraId = i;
        break;
      }
    }
    return cameraId;
  }

logcat的:

  04-30 21:14:36.400: D/MakePhotoActivity(8793): Camera found 0
04-30 21:14:36.400: I/Camera(8793): sendBroadcast intent.stop.app-in-app
04-30 21:14:36.650: D/libEGL(8793): loaded /system/lib/egl/libEGL_mali.so
04-30 21:14:36.655: D/libEGL(8793): loaded /system/lib/egl/libGLESv1_CM_mali.so
04-30 21:14:36.660: D/libEGL(8793): loaded /system/lib/egl/libGLESv2_mali.so
04-30 21:14:36.665: D/(8793): Device driver API match
04-30 21:14:36.665: D/(8793): Device driver API version: 10
04-30 21:14:36.665: D/(8793): User space API version: 10 
04-30 21:14:36.665: D/(8793): mali: REVISION=Linux-r2p4-02rel0 BUILD_DATE=Thu Oct 25 08:43:05 KST 2012 
04-30 21:14:36.685: D/OpenGLRenderer(8793): Enabling debug mode 0
04-30 21:16:21.695: D/dalvikvm(8793): WAIT_FOR_CONCURRENT_GC blocked 0ms
04-30 21:16:21.775: D/dalvikvm(8793): GC_EXPLICIT freed 78K, 14% free 9467K/10883K, paused 9ms+4ms, total 82ms
04-30 21:16:22.010: E/Camera(8793): Error -1

其余代码

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // do we have a camera?
    if (!getPackageManager()
        .hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
      Toast.makeText(this, "No camera on this device", Toast.LENGTH_LONG)
          .show();
    } else {
      cameraId = findFrontFacingCamera();
      if (cameraId < 0) {
        Toast.makeText(this, "No front facing camera found.",
            Toast.LENGTH_LONG).show();
      } else {
        camera = Camera.open(cameraId);
      }
    }
  }

  public void onClick(View view) {

    camera.takePicture(null, null,
        new PhotoHandler(getApplicationContext()));
  }

权限:

 <uses-permission android:name="android.permission.CAMERA"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-feature android:name="android.hardware.camera" />
    <uses-permission android:name="android.permission.FLASHLIGHT"/>
     <uses-feature android:name="android.hardware.camera.autofocus" />

1 个答案:

答案 0 :(得分:0)

我建议添加一些日志以显示info.facingCameraInfo.CAMERA_FACING_FRONT的值。 CAMERA_FACING_BACK的值为0和CAMERA_FACING_FRONT = 1。检查info.facing中的值,即设备返回的值

如果您正在访问背面摄像头,可以试试  camera = Camera.open();

可能存在硬件或设备权限原因导致运行时错误。