androidcameralibrary-project

Android: Accessing camera.startPreview() in library project


I am new to Android, but have to do some rather complicated programming on a project. We want to write an Android Plugin for a Unity Project, because we need to measure the user's heartrate. The code itself is already written and working fine in the context of an application. We now have to rewrite it in the context of a library project.

I have already figured how to access getSystemService(), without the GUI-elements (which I do not have due to it being a library and not an app).

Problem now is, that when I call camera.startPreview() nothing happens. I tried to do the following:

public HeartRateMeasurement(Context c) {
    context = c;

    SurfaceView surfaceView = new SurfaceView(c);
    SurfaceHolder holder = surfaceView.getHolder();
    holder.addCallback(this);
    surfaceCreated(holder);

}

Inside surfaceCreated I set some parameters (like the flash-mode "torch") and later call

camera.setPreviewDisplay(holder);
camera = open();

and when the heartrate-measurement should start (happens on a button click)

camera.startPreview();

This all (apart from the constructor-stuff, which I added myself) works as charm when done inside an application, but not within the library - there is no exception thrown, but the flash just does not turn on. In the debug info he tells me "D/Camera: app passed NULL surface", which is not surprising as there is no layout/surface.

My Manifest looks like this:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="tudarmstadt.heartrateplugin">
<application
    android:allowBackup="true"
    android:label="@string/app_name"
    android:supportsRtl="true"></application>
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-feature android:name="android.hardware.camera.flash" />
<uses-feature android:name="android.hardware.Sensor" />
</manifest>

Is there any way I can get startPreview() to work?


Solution

  • attach the surface to the main Layout.

    That is, rootlayout.addView(surfaceView)

    the latest API prevent the programmer to grab data without showing it.

    edit in order to get the root layout you have to use this:

    mRootWindow = getWindow();
    mDecorView = mRootWindow.getDecorView();
    mRootView = mDecorView.findViewById(android.R.id.content);