我的想法是显示 Metaio 图像跟踪器识别的图像名称。我成功地显示了为第一个COS 动态创建的图像,但它不显示为第二个COS 动态创建的图像。它确实检测到第二个图像(它进入了 if 块,已经记录了那个 --> Java 代码)但我不明白为什么它不显示第二个图像。它肯定会检测到第二张图像,因为当我在前几毫秒将相机拉回第一张图像时,第二张图像的名称会反映出来,它会再次更改为第一张图像的名称。
我使用了动态创建图像的广告牌方法。感谢 metaio 开发人员!
等待回复..提前谢谢大家!
Java 代码
public class Identify extends ARViewActivity {
/*
* This method is regularly called, calculates the distance between phone
* and target and performs actions based on the distance
*/
private void checkDistanceToTarget() {
TrackingValuesVector poses = metaioSDK.getTrackingValues();
if (poses.size() != 0) {
Log.e("METAIO", "FOUND SOMETHING");
for (int i = 0; i < poses.size(); i++) {
TrackingValues pose = poses.get(i);
String cosName = pose.getCosName();
if (cosName.equalsIgnoreCase("MarkerlessCOS1")) {
String title = "MYSELF";
String texturePath = getAnnotationImageForTitle(title);
metaioSDK.createGeometryFromImage(texturePath, true, true);
System.out.println("MYSELF");
} else if (cosName.equalsIgnoreCase("MarkerlessCOS2")) {
System.out.println("TIGER");
String title = "TIGER";
String texturePath = getAnnotationImageForTitle(title);
metaioSDK.createGeometryFromImage(texturePath, true, true); // THIS DOESN'T WORK!! :(
} else {
}
}
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
protected void onDestroy() {
super.onDestroy();
}
@Override
protected int getGUILayout() {
// TODO: return 0 in case of no GUI overlay
return R.layout.identify_national_personality;
}
@Override
protected IMetaioSDKCallback getMetaioSDKCallbackHandler() {
return null;
}
@Override
public void onDrawFrame() {
super.onDrawFrame();
try {
checkDistanceToTarget();
} catch (Exception e) {
e.printStackTrace();
}
}
public void onButtonClick(View v) {
finish();
}
@Override
protected void loadContents() {
String trackingConfigFile = AssetsManager.getAssetPath(
getApplicationContext(),
"TrackingDataXML/TrackingData_MarkerlessFast.xml");
// Assigning tracking configuration
boolean result = metaioSDK.setTrackingConfiguration(trackingConfigFile);
// metaioSDK.set }
@Override
protected void onGeometryTouched(final IGeometry geometry) {
}
private String getAnnotationImageForTitle(String title) {
Bitmap billboard = null;
try {
final String texturepath = getCacheDir() + "/" + title + ".png";
Paint mPaint = new Paint();
// Load background image and make a mutable copy
float dpi = SystemInfo.getDisplayDensity(getApplicationContext());
int scale = dpi > 240 ? 2 : 1;
String filepath = AssetsManager.getAssetPath(
getApplicationContext(), "POI_bg"
+ (scale == 2 ? "@2x" : "") + ".png");
Bitmap mBackgroundImage = BitmapFactory.decodeFile(filepath);
billboard = mBackgroundImage.copy(Bitmap.Config.ARGB_8888, true);
Canvas c = new Canvas(billboard);
mPaint.setColor(Color.WHITE);
mPaint.setTextSize(24);
mPaint.setTypeface(Typeface.DEFAULT);
mPaint.setTextAlign(Paint.Align.CENTER);
float y = 40 * scale;
float x = 30 * scale;
// Draw POI name
if (title.length() > 0) {
String n = title.trim();
final int maxWidth = 160 * scale;
int i = mPaint.breakText(n, true, maxWidth, null);
int xPos = (c.getWidth() / 2);
int yPos = (int) ((c.getHeight() / 2) - ((mPaint.descent() + mPaint
.ascent()) / 2));
c.drawText(n.substring(0, i), xPos, yPos, mPaint);
// Draw second line if valid
if (i < n.length()) {
n = n.substring(i);
y += 20 * scale;
i = mPaint.breakText(n, true, maxWidth, null);
if (i < n.length()) {
i = mPaint.breakText(n, true, maxWidth - 20 * scale,
null);
c.drawText(n.substring(0, i) + "...", x, y, mPaint);
} else {
c.drawText(n.substring(0, i), x, y, mPaint);
}
}
}
// Write texture file
try {
FileOutputStream out = new FileOutputStream(texturepath);
billboard.compress(Bitmap.CompressFormat.PNG, 90, out);
MetaioDebug.log("Texture file is saved to " + texturepath);
return texturepath;
} catch (Exception e) {
MetaioDebug.log("Failed to save texture file");
e.printStackTrace();
}
} catch (Exception e) {
MetaioDebug.log("Error creating annotation texture: "
+ e.getMessage());
MetaioDebug.printStackTrace(Log.DEBUG, e);
return null;
} finally {
if (billboard != null) {
billboard.recycle();
billboard = null;
}
}
return null;
}
}
XML 文件
<?xml version="1.0"?>
<TrackingData>
<Sensors>
<Sensor Type="FeatureBasedSensorSource" Subtype="Fast">
<!-- Assign an ID to this sensor -->
<SensorID>FeatureTracking1</SensorID>
<Parameters>
<FeatureDescriptorAlignment>regular</FeatureDescriptorAlignment>
<MaxObjectsToDetectPerFrame>5</MaxObjectsToDetectPerFrame>
<MaxObjectsToTrackInParallel>10</MaxObjectsToTrackInParallel>
<SimilarityThreshold>0.6</SimilarityThreshold>
</Parameters>
<SensorCOS>
<SensorCosID>Patch1</SensorCosID>
<Parameters>
<ReferenceImage>myself.jpg</ReferenceImage>
<SimilarityThreshold>0.6</SimilarityThreshold>
</Parameters>
</SensorCOS>
<SensorCOS>
<SensorCosID>Patch2</SensorCosID>
<Parameters>
<ReferenceImage>tiger.jpg</ReferenceImage>
<SimilarityThreshold>0.6</SimilarityThreshold>
</Parameters>
</SensorCOS>
</Sensor>
</Sensors>
<Connections>
<COS>
<Name>MarkerlessCOS1</Name>
<Fuser Type="SmoothingFuser">
<Parameters>
<KeepPoseForNumberOfFrames>2</KeepPoseForNumberOfFrames>
<GravityAssistance></GravityAssistance>
<AlphaTranslation>1.0</AlphaTranslation>
<GammaTranslation>1.0</GammaTranslation>
<AlphaRotation>0.8</AlphaRotation>
<GammaRotation>0.8</GammaRotation>
<ContinueLostTrackingWithOrientationSensor>false</ContinueLostTrackingWithOrientationSensor>
</Parameters>
</Fuser>
<SensorSource>
<SensorID>FeatureTracking1</SensorID>
<SensorCosID>Patch1</SensorCosID>
<HandEyeCalibration>
<TranslationOffset>
<X>0</X>
<Y>0</Y>
<Z>0</Z>
</TranslationOffset>
<RotationOffset>
<X>0</X>
<Y>0</Y>
<Z>0</Z>
<W>1</W>
</RotationOffset>
</HandEyeCalibration>
<COSOffset>
<TranslationOffset>
<X>0</X>
<Y>0</Y>
<Z>0</Z>
</TranslationOffset>
<RotationOffset>
<X>0</X>
<Y>0</Y>
<Z>0</Z>
<W>1</W>
</RotationOffset>
</COSOffset>
</SensorSource>
</COS>
<COS>
<Name>MarkerlessCOS2</Name>
<Fuser Type="SmoothingFuser">
<Parameters>
<KeepPoseForNumberOfFrames>2</KeepPoseForNumberOfFrames>
<GravityAssistance></GravityAssistance>
<AlphaTranslation>0.8</AlphaTranslation>
<GammaTranslation>0.8</GammaTranslation>
<AlphaRotation>0.5</AlphaRotation>
<GammaRotation>0.5</GammaRotation>
<ContinueLostTrackingWithOrientationSensor>false</ContinueLostTrackingWithOrientationSensor>
</Parameters>
</Fuser>
<SensorSource>
<SensorID>FeatureTracking1</SensorID>
<SensorCosID>Patch2</SensorCosID>
<HandEyeCalibration>
<TranslationOffset>
<X>0</X>
<Y>0</Y>
<Z>0</Z>
</TranslationOffset>
<RotationOffset>
<X>0</X>
<Y>0</Y>
<Z>0</Z>
<W>1</W>
</RotationOffset>
</HandEyeCalibration>
<COSOffset>
<TranslationOffset>
<X>0</X>
<Y>0</Y>
<Z>0</Z>
</TranslationOffset>
<RotationOffset>
<X>0</X>
<Y>0</Y>
<Z>0</Z>
<W>1</W>
</RotationOffset>
</COSOffset>
</SensorSource>
</COS>
</Connections>
</TrackingData>