无法启动前置摄像头以及如何在应用程序内启动前置摄像头

时间:2013-08-09 13:51:19

标签: android android-camera

以下是代码。我想在屏幕的一半部分启动我的应用程序中的前置摄像头。但是我无法做到。 正在执行下面的Log.d()(找到相机)但相机无法启动。

守则:

public class MainActivity extends Activity {
private int cameraId=0;
private final static String DEBUG_TAG = "MakePhotoActivity";
private Camera camera;
private Button cameraButton;



/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    cameraButton=(Button)findViewById(R.id.startCam);
    cameraButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            startFrontCamera();
        }
    });


}//onCreate


protected void startFrontCamera() {
    // TODO Auto-generated method stub
    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);
          }
        }

}


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");
        cameraId = i;
        break;
      }
    }
    return cameraId;
  }


@Override
protected void onPause() {
    // TODO Auto-generated method stub
    if (camera != null) {
        camera.release();
        camera = null;
      }
     Toast.makeText(this, "Destroy called", Toast.LENGTH_LONG)
     .show();
    super.onPause();
}


}//mainActivity

错误

 08-09 19:11:49.415: E/AndroidRuntime(27223): FATAL EXCEPTION: main
 08-09 19:11:49.415: E/AndroidRuntime(27223): java.lang.RuntimeException: Fail to connect to camera service
 08-09 19:11:49.415: E/AndroidRuntime(27223):   at android.hardware.Camera.native_setup(Native Method)
 08-09 19:11:49.415: E/AndroidRuntime(27223):   at android.hardware.Camera.<init>(Camera.java:307)
 08-09 19:11:49.415: E/AndroidRuntime(27223):   at android.hardware.Camera.open(Camera.java:268)
 08-09 19:11:49.415: E/AndroidRuntime(27223):   at com.toy.MainActivity.startFrontCamera(MainActivity.java:57)
 08-09 19:11:49.415: E/AndroidRuntime(27223):   at com.toy.MainActivity$1.onClick(MainActivity.java:35)
 08-09 19:11:49.415: E/AndroidRuntime(27223):   at android.view.View.performClick(View.java:3549)
 08-09 19:11:49.415: E/AndroidRuntime(27223):   at android.view.View$PerformClick.run(View.java:14400)
 08-09 19:11:49.415: E/AndroidRuntime(27223):   at android.os.Handler.handleCallback(Handler.java:605)
 08-09 19:11:49.415: E/AndroidRuntime(27223):   at android.os.Handler.dispatchMessage(Handler.java:92)
 08-09 19:11:49.415: E/AndroidRuntime(27223):   at android.os.Looper.loop(Looper.java:154)
 08-09 19:11:49.415: E/AndroidRuntime(27223):   at android.app.ActivityThread.main(ActivityThread.java:4945)
 08-09 19:11:49.415: E/AndroidRuntime(27223):   at java.lang.reflect.Method.invokeNative(Native Method)
 08-09 19:11:49.415: E/AndroidRuntime(27223):   at java.lang.reflect.Method.invoke(Method.java:511)
 08-09 19:11:49.415: E/AndroidRuntime(27223):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
 08-09 19:11:49.415: E/AndroidRuntime(27223):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
 08-09 19:11:49.415: E/AndroidRuntime(27223):   at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案