我一直试图让一些AR(增强现实)SDK在Fragment中工作。 但是,我似乎无法让它发挥作用。
我找到了一些让Metaio(AR框架)在片段中工作的代码。
所以我已将该代码应用于我自己的项目,它正在运行,但代码未编程为扫描图片。我想用它扫描图片标记。
我复制了一些代码来扫描Metaio示例项目中的图片标记,但它没有用。
现在它失败了(在没有记录之后的调试日志):
trackingConfigFile = AssetsManager.getAssetPath(getActivity().getApplicationContext(), "AEDApp/Assets/TrackingData_PictureMarker.xml");
这是我的完整代码:
package com.example.bt6_aedapp;
import android.app.Application;
import android.content.res.Configuration;
import android.hardware.Camera.CameraInfo;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.metaio.cloud.plugin.MetaioCloudPlugin;
import com.metaio.sdk.MetaioDebug;
import com.metaio.sdk.MetaioSurfaceView;
import com.metaio.sdk.SensorsComponentAndroid;
import com.metaio.sdk.jni.ERENDER_SYSTEM;
import com.metaio.sdk.jni.ESCREEN_ROTATION;
import com.metaio.sdk.jni.IGeometry;
import com.metaio.sdk.jni.IMetaioSDKAndroid;
import com.metaio.sdk.jni.IMetaioSDKCallback;
import com.metaio.sdk.jni.MetaioSDK;
import com.metaio.sdk.jni.TrackingValuesVector;
import com.metaio.sdk.jni.Vector3d;
import com.metaio.tools.Screen;
import com.metaio.tools.SystemInfo;
import com.metaio.tools.io.AssetsManager;
public class fragmentA extends Fragment implements MetaioSurfaceView.Callback {
private Application mAppContext;
private ViewGroup mRootLayout;
String trackingConfigFile;
private MetaioSDKCallbackHandler mCallback;
private IGeometry mModel;
private IMetaioSDKAndroid mMetaioSDK;
private MetaioSurfaceView mSurfaceView;
private static boolean mNativeLibsLoaded = false;
private boolean mRendererInitialized;
private SensorsComponentAndroid mSensors;
static {
mNativeLibsLoaded = IMetaioSDKAndroid.loadNativeLibs();
}
@Override
public void onCreate(Bundle savedInstanceState) {
MetaioCloudPlugin.startJunaio(null, getActivity().getApplicationContext());
super.onCreate(savedInstanceState);
Log.d("LifeCycle", "onCreate");
mAppContext = getActivity().getApplication();
mMetaioSDK = null;
mSurfaceView = null;
mRendererInitialized = false;
try {
mCallback = new MetaioSDKCallbackHandler();
if (!mNativeLibsLoaded){
throw new Exception("Unsupported platform, failed to load the native libs");
}
// Create sensors component
mSensors = new SensorsComponentAndroid(mAppContext);
// Create Unifeye Mobile by passing Activity instance and
// application signature
mMetaioSDK = MetaioSDK.CreateMetaioSDKAndroid(getActivity(), getResources().getString(R.string.metaioSDKSignature));
mMetaioSDK.registerSensorsComponent(mSensors);
} catch (Throwable e) {
MetaioDebug.log(Log.ERROR, "ArCameraFragment.onCreate: failed to create or intialize metaio SDK: " + e.getMessage());
return;
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.d("LifeCycle", "onCreateView");
View view = inflater.inflate(R.layout.fragment_a, container, false);
mRootLayout = (ViewGroup)getActivity().findViewById(R.id.pager);
return view;
}
@Override
public void onStart() {
super.onStart();
Log.d("LifeCycle", "onStart");
if(mMetaioSDK == null){
return;
}
MetaioDebug.log("ArCameraFragment.onStart()");
try {
mSurfaceView = null;
// Start camera
startCamera();
// Add Unifeye GL Surface view
mSurfaceView = new MetaioSurfaceView(mAppContext);
mSurfaceView.registerCallback(this);
mSurfaceView.setKeepScreenOn(true);
MetaioDebug.log("ArCameraFragment.onStart: addContentView(mMetaioSurfaceView)");
mRootLayout.addView(mSurfaceView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
mSurfaceView.setZOrderMediaOverlay(true);
} catch (Exception e) {
MetaioDebug.log(Log.ERROR, "Error creating views: " + e.getMessage());
MetaioDebug.printStackTrace(Log.ERROR, e);
}
}
@Override
public void onResume() {
super.onResume();
Log.d("LifeCycle", "onResume");
// make sure to resume the OpenGL surface
if (mSurfaceView != null) {
mSurfaceView.onResume();
}
if(mMetaioSDK != null){
mMetaioSDK.resume();
}
}
@Override
public void onPause() {
super.onPause();
Log.d("LifeCycle", "onPause");
// pause the OpenGL surface
if (mSurfaceView != null) {
mSurfaceView.onPause();
}
if (mMetaioSDK != null) {
// Disable the camera
mMetaioSDK.pause();
}
}
@Override
public void onStop() {
super.onStop();
Log.d("LifeCycle", "onStop");
if (mMetaioSDK != null) {
// Disable the camera
mMetaioSDK.stopCamera();
}
if (mSurfaceView != null) {
mRootLayout.removeView(mSurfaceView);
}
System.runFinalization();
System.gc();
}
@Override
public void onDestroy() {
super.onDestroy();
mCallback.delete();
mCallback = null;
/*Log.d("LifeCycle", "onDestroy");
try {
mRendererInitialized = false;
} catch (Exception e) {
MetaioDebug.printStackTrace(Log.ERROR, e);
}
MetaioDebug.log("ArCameraFragment.onDestroy");
if (mMetaioSDK != null) {
mMetaioSDK.delete();
mMetaioSDK = null;
}
MetaioDebug.log("ArCameraFragment.onDestroy releasing sensors");
if (mSensors != null) {
mSensors.registerCallback(null);
mSensors.release();
mSensors.delete();
mSensors = null;
}
// Memory.unbindViews(activity.findViewById(android.R.id.content));
System.runFinalization();
System.gc();*/
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
final ESCREEN_ROTATION rotation = Screen.getRotation(getActivity());
mMetaioSDK.setScreenRotation(rotation);
MetaioDebug.log("onConfigurationChanged: " + rotation);
}
@Override
public void onDrawFrame() {
if(mMetaioSDK != null) {
TrackingValuesVector poses = mMetaioSDK.getTrackingValues();
if(poses.size() != 0) {
mModel.setCoordinateSystemID(poses.get(0).getCoordinateSystemID());
}
}
// Log.d("LifeCycle", "onDrawFrame");
/* if (mRendererInitialized) {
mMetaioSDK.render();
} */
}
@Override
public void onSurfaceCreated() {
Log.d("LifeCycle", "onSurfaceCreated");
try {
if (!mRendererInitialized) {
mMetaioSDK.initializeRenderer(mSurfaceView.getWidth(), mSurfaceView.getHeight(), Screen.getRotation(getActivity()),
ERENDER_SYSTEM.ERENDER_SYSTEM_OPENGL_ES_2_0);
mRendererInitialized = true;
} else {
MetaioDebug.log("ArCameraFragment.onSurfaceCreated: Reloading textures...");
mMetaioSDK.reloadTextures();
}
MetaioDebug.log("ArCameraFragment.onSurfaceCreated: Registering audio renderer...");
// mMetaioSDK.registerAudioCallback(mSurfaceView.getAudioRenderer());
mMetaioSDK.registerCallback(mCallback);
MetaioDebug.log("ARViewActivity.onSurfaceCreated");
} catch (Exception e) {
MetaioDebug.log(Log.ERROR, "ArCameraFragment.onSurfaceCreated: " + e.getMessage());
}
mSurfaceView.queueEvent(new Runnable() {
@Override
public void run() {
loadContents();
}
});
}
private void loadContents() {
try {
trackingConfigFile = AssetsManager.getAssetPath(getActivity().getApplicationContext(), "AEDApp/Assets/TrackingData_PictureMarker.xml");
boolean result = mMetaioSDK.setTrackingConfiguration(trackingConfigFile);
Log.d("result", Boolean.toString(result));
MetaioDebug.log("Tracking data loaded: " + result);
String aedLogo = AssetsManager.getAssetPath(getActivity().getApplicationContext(), "AEDApp/Assets/metaioman.md2");
Log.d("aedLogo", "aaa: " + aedLogo);
if(aedLogo != null) {
mModel = mMetaioSDK.createGeometry(aedLogo);
if(mModel != null) {
mModel.setScale(new Vector3d(4.0f, 4.0f, 4.0f));
}
else {
MetaioDebug.log(Log.ERROR, "Error loading geometry: " + aedLogo);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onSurfaceChanged(int width, int height) {
Log.d("LifeCycle", "onSurfaceChanged");
mMetaioSDK.resizeRenderer(width, height);
}
@Override
public void onSurfaceDestroyed() {
Log.d("LifeCycle", "onSurfaceDestroyed");
MetaioDebug.log("ArCameraFragment.onSurfaceDestroyed(){");
mSurfaceView = null;
// mMetaioSDK.registerAudioCallback(null);
}
protected void startCamera() {
final int cameraIndex = SystemInfo.getCameraIndex(CameraInfo.CAMERA_FACING_BACK);
if (mMetaioSDK != null) {
mMetaioSDK.startCamera(cameraIndex, 640, 480);
}
}
final class MetaioSDKCallbackHandler extends IMetaioSDKCallback {
@Override
public void onTrackingEvent(final TrackingValuesVector trackingValues) {
super.onTrackingEvent(trackingValues);
if(!trackingValues.isEmpty() && trackingValues.get(0).isTrackingState()){
Log.d("Track", "NOT EMPTY");
}
}
}
}
我真的希望有人可以帮助我,因为我无法理解...... :(
修改
错误(e.printStackTrace())抛出的是:
03-24 20:25:19.068: W/System.err(28062): java.lang.NullPointerException: null string
03-24 20:25:19.068: W/System.err(28062): at com.metaio.sdk.jni.MetaioSDKJNI.IMetaioSDK_setTrackingConfiguration__SWIG_1(Native Method)
03-24 20:25:19.068: W/System.err(28062): at com.metaio.sdk.jni.IMetaioSDK.setTrackingConfiguration(IMetaioSDK.java:106)
03-24 20:25:19.068: W/System.err(28062): at com.example.bt6_aedapp.fragmentA.loadContents(fragmentA.java:278)
03-24 20:25:19.068: W/System.err(28062): at com.example.bt6_aedapp.fragmentA.access$0(fragmentA.java:274)
03-24 20:25:19.068: W/System.err(28062): at com.example.bt6_aedapp.fragmentA$1.run(fragmentA.java:268)
03-24 20:25:19.068: W/System.err(28062): at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1463)
03-24 20:25:19.068: W/System.err(28062): at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1240)
我想用它做什么:
EDIT 如果有人知道如何在片段中制作另一个AR框架,我很乐意知道。
答案 0 :(得分:1)
我对片段几乎一无所知,但对于空字符串,我认为这是因为你没有提取资产。
在此视频http://youtu.be/KVtCi-WwmFU?t=30m29s中,我们会对其进行解释。 基本上你要做的就是添加这段代码
private class AssetsExtracter extends AsyncTask<Integer, Integer, Boolean>{
@Override
protected Boolean doInBackground(Integer... params){
try
{
AssetsManager.extractAllAssets(getApplicationContext(), BuildConfig.DEBUG);
}catch (IOException e){
MetaioDebug.printStackTrace(Log.ERROR, e);
return false;
}
return true;
}
}
到你的活动(或者在这种情况下,我猜你的片段)。 然后你必须添加这个类的字段,如
private AssetsExtracter mTask;
并在你输入的onCreate()方法中
mTask = new AssetsExtracter();
mTask.execute(0);
之后,您的资产应该可以从AssetsManager.getAssetPath(..)获得,并且它不应再返回空字符串。