安卓:访问camera.start preVIEW()在库项目 [英] Android: Accessing camera.startPreview() in library project

查看:172
本文介绍了安卓:访问camera.start preVIEW()在库项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的Andr​​oid,但必须做一个项目,一些比较复杂的编程。
我们要编写一个Android插件的团结项目,因为我们需要测量用户的心率。在code本身已经写入,并在应用程序的情况下正常工作。我们现在必须重写它在图书馆项目的环境中。

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.

我已经想通了如何访问的 getSystemService()的,没有GUI元素(这我没有,由于它是一个库,而不是应用程序)。

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).

现在的问题是,当我打电话的 camera.start preVIEW()的什么也没有发生。我试着做到以下几点:

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);

}

里面surfaceCreated我设置一些参数(如闪光模式火炬),后来电话

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();

这一切(除了构造函数的东西,这是我自己加)的工作原理魅力的应用程序中完成的时候,而不是在图书馆内 - 没有异常抛出,但闪光只是不打开。在调试信息,他告诉我,D /摄像机:通过应用表面NULL,因为没有布局/面,这并不奇怪。

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>

有什么办法,我可以得到的启动preVIEW()的工作?

推荐答案

表面附着的主要布局。

这就是 rootlayout.addView(surfaceView)

最新的API prevent程序员获取数据,而不显示它。

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);

这篇关于安卓:访问camera.start preVIEW()在库项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆