[英]Camera is black on Android 4.1.2 with Unity3D and Vuforia
我工作的公司正在構建具有vuforia(版本5.x)和Unity3D(版本5.3.5f1 Personal)集成的android應用程序。
在最新的android設備中,相機可以正常打開,但在三星Galaxy S2(4.1.2)等舊設備中我們正面臨問題。 當設備通過vuforia打開相機時,屏幕為黑色,如果我們嘗試拍照,則圖像顯然也為黑色。
我的活動只是實例化unity player,就像下面的代碼:
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圖像並負責拍照。
在統一項目中,我們開發了一個按鈕來拍攝圖片,並且該功能可以將圖像保存在android目錄中。 它的腳本是用C#完成的,其實現如下:
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。 提前致謝。
試試這個代碼:
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();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.