使用Unity3D和Vuforia的Android 4.1.2上的相机为黑色 [英] Camera is black on Android 4.1.2 with Unity3D and Vuforia

查看:142
本文介绍了使用Unity3D和Vuforia的Android 4.1.2上的相机为黑色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的公司正在构建具有vuforia(版本5.x)和Unity3D(版本5.3.5f1 Personal)集成的android应用程序.

The company I work is building an android application with vuforia(version 5.x) and Unity3D(version 5.3.5f1 Personal) integration.

在最新的android设备中,相机可以正常打开,但是我们在诸如Samsung Galaxy S2(4.1.2)之类的较旧设备中面临问题.当设备通过vuforia打开相机时,屏幕为黑色,而如果我们尝试拍照,则图像显然也为黑色.

In newest android devices the camera is opening ok but We're facing problem in older devices like Samsung Galaxy S2(4.1.2). When the device open the camera via vuforia the screen is black and if we try to take a picture the image is obviously black as well.

我的活动只是实例化unity player,就像下面的代码一样:

My activity is only instantiating the unity player and it is like code below:

public class MainActivity extends AppCompatActivity {

protected UnityPlayer mUnityPlayer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    getWindow().setFormat(PixelFormat.RGBX_8888); // <--- This makes xperia play happy
    mUnityPlayer = new UnityPlayer(this);
    setContentView(mUnityPlayer);
    mUnityPlayer.requestFocus();
}

public void onImageSaved(final String path) {
    final Intent intent = new Intent(MainActivity.this, CameraPreviewActivity.class);
    final Bundle bundle = new Bundle();
    bundle.putString("IMAGE_PATH", path);
    intent.putExtras(bundle);
    startActivity(intent);
}

public void onImageSavedError(final String message) {
    UnityPlayer.currentActivity.runOnUiThread(new Runnable() {

        @Override
        public void run() {
            Log.e("Teste", message);
        }
    });
}

// Quit Unity
@Override protected void onDestroy ()
{
    mUnityPlayer.quit();
    super.onDestroy();
}

// Pause Unity
@Override protected void onPause()
{
    super.onPause();
    mUnityPlayer.pause();
}

// Resume Unity
@Override protected void onResume()
{
    super.onResume();
    mUnityPlayer.resume();
}

// This ensures the layout will be correct.
@Override public void onConfigurationChanged(Configuration newConfig)
{
    super.onConfigurationChanged(newConfig);
    mUnityPlayer.configurationChanged(newConfig);
}

// Notify Unity of the focus change.
@Override public void onWindowFocusChanged(boolean hasFocus)
{
    super.onWindowFocusChanged(hasFocus);
    mUnityPlayer.windowFocusChanged(hasFocus);
}

// For some reason the multiple keyevent type is not supported by the ndk.
// Force event injection by overriding dispatchKeyEvent().
@Override public boolean dispatchKeyEvent(KeyEvent event)
{
    if (event.getAction() == KeyEvent.ACTION_MULTIPLE)
        return mUnityPlayer.injectEvent(event);
    return super.dispatchKeyEvent(event);
}

// Pass any events not handled by (unfocused) views straight to UnityPlayer
@Override public boolean onKeyUp(int keyCode, KeyEvent event)     { return mUnityPlayer.injectEvent(event); }
@Override public boolean onKeyDown(int keyCode, KeyEvent event)   { return mUnityPlayer.injectEvent(event); }
@Override public boolean onTouchEvent(MotionEvent event)          { return mUnityPlayer.injectEvent(event); }
/*API12*/ public boolean onGenericMotionEvent(MotionEvent event)  { return mUnityPlayer.injectEvent(event); }

@Override
public void onBackPressed() {
    finish();
}

}

vuforia负责打开相机并开始跟踪图像目标,而unity3D将使用动画渲染3D图像并负责拍照.

The vuforia is responsible for opening the camera and starting tracking the image targets while unity3D will render a 3D image with animation and responsible for taking the picture.

在统一项目中,我们开发了一个拍照按钮和一个将图像保存在android目录中的功能.它的脚本是用C#完成的,其实现如下:

In unity project we have developed a button to take a picture and a function thats gonna save the image in an android directory. Its is script was done in C# and its implementation is as follow below:

IEnumerator TakeSnapshot()
{
    yield return new WaitForEndOfFrame();
    Texture2D snap = new Texture2D(Screen.width,Screen.height);
    Camera camera = Camera.current;

    if (rotation != null)
    {
        camera.transform.rotation = rotation.Value;
    }

    if (position != null)
    {
        camera.transform.position = position.Value;
    }

    camera.Render();
    RenderTexture.active = camera.targetTexture;
    snap.ReadPixels(new Rect(0,0,Screen.width,Screen.height),0,0);
    snap.Apply();
    RenderTexture.active = null;

    byte[] bytes = snap.EncodeToJPG();
    DestroyObject(snap);

    string path = Application.persistentDataPath + "/MyAppPath/";
    if(!System.IO.Directory.Exists(path)){
        System.IO.Directory.CreateDirectory(path);
    }

    string filename = fileName(Convert.ToInt32(snap.width), Convert.ToInt32(snap.height));
    path = path + filename;
    System.IO.File.WriteAllBytes(path, bytes);

    using (var actClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
    {
        var jo = actClass.GetStatic<AndroidJavaObject>("currentActivity");

        if (jo == null) {
            Debug.Log ("jo is null");
        } else {
            jo.Call("onImageSaved", path);
        }
    }
}

这篇文章的主要目的是试图了解为什么相机仅在较旧的设备上才是黑色的,因为它在较新的设备上可以正常工作.我还要特别强调一下,openGL可能不是问题,因为我已经在使用sinogen 5.1的旧设备(例如Samsung Galaxy S2)中测试了该应用程序. 导出统一项目的当前openGL版本是openGL2. 预先感谢.

The main goal of this post is try to understand why camera is black only in older devices since it worked fine in newer one. Also I would like to highlight that openGL may not be a problem since I have already tested the application in older devices like Samsung Galaxy S2 using sinogen 5.1. The current openGL version that unity project is being exported is openGL2. Thanks in advance.

推荐答案

尝试以下代码:

public string deviceName;
WebCamTexture wct;
void Start ()
{
    WebCamDevice[] devices = WebCamTexture.devices;
    deviceName = devices[0].name;
    wct = new WebCamTexture(deviceName, 400, 300, 12);
    GetComponent<Renderer> ().material.mainTexture = wct;
    wct.Play();
}

这篇关于使用Unity3D和Vuforia的Android 4.1.2上的相机为黑色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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