0

For two days I have been trying to show the camera in a view. I followed many tutorials like these two:

http://code.google.com/p/openmobster/wiki/CameraTutorial http://developer.android.com/training/camera/cameradirect.html

but without success to show the camera on the screen.

I finally found this which works directly. Just had to remove one deprecated method. From that one I tried to build something similar to the example in the two first sites, but I cannot get it working.

At this line:

surfaceView = (PreView)findViewById(R.id.camerapreview);

I tried to modify it by:

surfaceView = new PreView(this);
      FrameLayout layout = (FrameLayout)findViewById(R.id.camerapreview); 
      layout.addView(surfaceView);

But it does not work. Then I also changed the SurfaceView by LinearLayout in the activity_main.xml. I did not work. The constructor of PReView did not even run (I put a breakpoint in it, but the bug is before that). Do I need to have a nested layout to get it working? I tried many things. I hope someone will have a look:

MainActivity.java:

package com.example.cameratest2;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.graphics.PixelFormat;
import android.os.Bundle;

public class MainActivity extends Activity{
PreView surfaceView;

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

      getWindow().setFormat(PixelFormat.UNKNOWN);
      surfaceView = (PreView)findViewById(R.id.camerapreview);
      //surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
  }
}

Preview.java:

package com.example.cameratest2;

import java.io.IOException;

import android.content.Context;
import android.hardware.Camera;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class PreView extends SurfaceView implements SurfaceHolder.Callback {
Camera camera;
boolean previewing = false;
SurfaceHolder surfaceHolder;

public PreView(Context context) {
    super(context);

    surfaceHolder = this.getHolder();
    surfaceHolder.addCallback(this);
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, 
    int width,int height)   {
    if(previewing){
     camera.stopPreview();
     previewing = false;
    }

    if (camera != null){
     try {
      camera.setPreviewDisplay(surfaceHolder);
      camera.startPreview();
      previewing = true;
     } catch (IOException e) {
      e.printStackTrace();
     }
    }
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    camera = Camera.open();
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    camera.stopPreview();
    camera.release();
    camera = null;
    previewing = false;
}
  }

activity_main.xml

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 >
 <SurfaceView
 android:id="@+id/camerapreview"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 />
 </LinearLayout>

the logCat:

    11-09 04:22:58.856: W/dalvikvm(1612): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
    11-09 04:22:58.975: E/AndroidRuntime(1612): FATAL EXCEPTION: main
    11-09 04:22:58.975: E/AndroidRuntime(1612): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.cameratest2/com.example.cameratest2.MainActivity}: java.lang.ClassCastException: android.view.SurfaceView cannot be cast to com.example.cameratest2.PreView
    11-09 04:22:58.975: E/AndroidRuntime(1612):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
    11-09 04:22:58.975: E/AndroidRuntime(1612):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
    11-09 04:22:58.975: E/AndroidRuntime(1612):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
    11-09 04:22:58.975: E/AndroidRuntime(1612):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
    11-09 04:22:58.975: E/AndroidRuntime(1612):     at android.os.Handler.dispatchMessage(Handler.java:99)
    11-09 04:22:58.975: E/AndroidRuntime(1612):     at android.os.Looper.loop(Looper.java:137)
    11-09 04:22:58.975: E/AndroidRuntime(1612):     at android.app.ActivityThread.main(ActivityThread.java:4745)
    11-09 04:22:58.975: E/AndroidRuntime(1612):     at java.lang.reflect.Method.invokeNative(Native Method)
    11-09 04:22:58.975: E/AndroidRuntime(1612):     at java.lang.reflect.Method.invoke(Method.java:511)
    11-09 04:22:58.975: E/AndroidRuntime(1612):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
    11-09 04:22:58.975: E/AndroidRuntime(1612):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    11-09 04:22:58.975: E/AndroidRuntime(1612):     at dalvik.system.NativeStart.main(Native Method)
    11-09 04:22:58.975: E/AndroidRuntime(1612): Caused by: java.lang.ClassCastException: android.view.SurfaceView cannot be cast to com.example.cameratest2.PreView
    11-09 04:22:58.975: E/AndroidRuntime(1612):     at com.example.cameratest2.MainActivity.onCreate(MainActivity.java:19)
    11-09 04:22:58.975: E/AndroidRuntime(1612):     at android.app.Activity.performCreate(Activity.java:5008)
    11-09 04:22:58.975: E/AndroidRuntime(1612):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
    11-09 04:22:58.975: E/AndroidRuntime(1612):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
    11-09 04:22:58.975: E/AndroidRuntime(1612):     ... 11 more

[edit]

I found this : Draw SurfaceView from layout xml.
As explained in this link, I made a modification in my activity_main.xml replacing <SurfaceView> with <com.example.cameratest2.PreView> and modifying the MainActivity.java to replace

 surfaceView = (PreView)findViewById(R.id.camerapreview);

with

 surfaceView = new PreView(this);

...but it still does not work, and I get this error:

11-09 05:01:14.114: E/AndroidRuntime(1700): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class com.example.cameratest2.PreView
Community
  • 1
  • 1
yoh
  • 154
  • 1
  • 10
  • are you getting an exception? can you post some logcat data? – takecare Nov 09 '12 at 12:21
  • thanks for the quick answer and sorry to do not respond earlier, I was slueeping.I add The Logcat to give you more information. There is something I do not iunderstand. I do not know If I have to run an other activity to link with the layout or just change the 19 lines. – yoh Nov 09 '12 at 18:42

0 Answers0