Rash
Rash

Reputation: 896

I am having error to open front camera using android

I am working on android camera app,I want green screen chroma key effect to captured image. I have used http://code.google.com/p/chroma-key-project/downloads/list this chroma key project with my code.

I am using tablet which having front camera instead of back camera..I have also used permission for front camera

 <uses-permission android:name="android.permission.CAMERA" />

    <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.front" />

still it shows an error null pointer exception from preview.java page to this line . camera.setPreviewDisplay(holder); // <9> why?

Check the preview.java page from the given link in chroma key project.please suggest.

this type of error I am getting.

10-11 15:37:36.131: W/dalvikvm(11339): threadid=1: thread exiting with uncaught exception (group=0x409ee1f8) 10-11 15:37:36.141: E/AndroidRuntime(11339): FATAL EXCEPTION: main 10-11 15:37:36.141: E/AndroidRuntime(11339): java.lang.UnsupportedOperationException 10-11 15:37:36.141: E/AndroidRuntime(11339): at java.lang.Thread.stop(Thread.java:1076) 10-11 15:37:36.141: E/AndroidRuntime(11339): at java.lang.Thread.stop(Thread.java:1063) 10-11 15:37:36.141: E/AndroidRuntime(11339): at com.Activity.new2you4kids.MainClass.startCamera(MainClass.java:179) 10-11 15:37:36.141: E/AndroidRuntime(11339): at com.Activity.new2you4kids.MainClass$1.handleMessage(MainClass.java:61) 10-11 15:37:36.141: E/AndroidRuntime(11339): at android.os.Handler.dispatchMessage(Handler.java:99) 10-11 15:37:36.141: E/AndroidRuntime(11339): at android.os.Looper.loop(Looper.java:137) 10-11 15:37:36.141: E/AndroidRuntime(11339): at android.app.ActivityThread.main(ActivityThread.java:4424) 10-11 15:37:36.141: E/AndroidRuntime(11339): at java.lang.reflect.Method.invokeNative(Native Method) 10-11 15:37:36.141: E/AndroidRuntime(11339): at java.lang.reflect.Method.invoke(Method.java:511) 10-11 15:37:36.141: E/AndroidRuntime(11339): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 10-11 15:37:36.141: E/AndroidRuntime(11339): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 10-11 15:37:36.141: E/AndroidRuntime(11339): at dalvik.system.NativeStart.main(Native Method)

Thanks!

Upvotes: 0

Views: 1576

Answers (1)

Rolf ツ
Rolf ツ

Reputation: 8781

Camera.open() tries to open the default back camera i think, so it returns null because there is no camera on the back.

what if you try the function Camera.open(int) to open the front camera?

Like this:

int getFrontCameraId() {
    CameraInfo ci = new CameraInfo();
    for (int i = 0 ; i < Camera.getNumberOfCameras(); i++) {
        Camera.getCameraInfo(i, ci);
        if (ci.facing == CameraInfo.CAMERA_FACING_FRONT) return i;
    }
    return -1; // No front-facing camera found
}

And get it like this:

int index = getFrontCameraId();
if (index == -1) error();
Camera c = Camera.open(index);

From: How to detect if there is front camera and if there is how to reach and use front camera?

Edit: the code you are using is very old and has all kinds of old functions, the crash you are having is caused by an old thread function (stop) i think.

Code from the link:

  public void startCamera() {
        setContentView(R.layout.camview);
        fGameView = null;
        fGameThread.stop();
        fGameThread=null;

according to the developers site:

stop() This method is deprecated. because stopping a thread in this manner is unsafe and can leave your application and the VM in an unpredictable state.

See:

java.lang.UnsupportedOperationException when my splash screen starts

http://developer.android.com/reference/java/lang/Thread.html#stop()

Edit two:

Change the onCreateOptionsMenu code to this, you should not use the myMenu variable.

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    menu.add(0, MENU_START, 0, R.string.menu_start);
    menu.add(0, MENU_SEND, 0, R.string.menu_send);
    return true;
}

Upvotes: 2

Related Questions