Android के लिए ऑगमेंटेड इमेज की डेवलपर गाइड

अपने ऐप्लिकेशन में ऑगमेंटेड इमेज को इस्तेमाल करने का तरीका जानें.

ज़रूरी शर्तें

पक्का करें कि आपको एआर के बुनियादी सिद्धांतों के बारे में पता हो साथ ही, आगे बढ़ने से पहले ARCore सेशन को कॉन्फ़िगर करने का तरीका जानें.

इमेज डेटाबेस बनाना

हर इमेज डेटाबेस में 1,000 इमेज तक की जानकारी सेव की जा सकती है.

इसे बनाने के दो तरीके हैं AugmentedImageDatabase:

  • सेव किए गए इमेज डेटाबेस को लोड करें. इसके बाद, अगर आप चाहें, तो ज़्यादा रेफ़रंस इमेज जोड़ सकते हैं.
  • एक नया खाली डेटाबेस बनाएं. इसके बाद, एक बार में एक इमेज जोड़ें.

सेव किए गए इमेज डेटाबेस को लोड करें

AugmentedImageDatabase.deserialize() का इस्तेमाल करें मौजूदा इमेज डेटाबेस को लोड करने के लिए:

Java

AugmentedImageDatabase imageDatabase;
try (InputStream inputStream = this.getAssets().open("example.imgdb")) {
  imageDatabase = AugmentedImageDatabase.deserialize(session, inputStream);
} catch (IOException e) {
  // The Augmented Image database could not be deserialized; handle this error appropriately.
}

Kotlin

val imageDatabase = this.assets.open("example.imgdb").use {
  AugmentedImageDatabase.deserialize(session, it)
}

arcoreimg का इस्तेमाल करके, इमेज डेटाबेस बनाए जा सकते हैं कमांड लाइन टूल बनाते समय या कॉल करके AugmentedImageDatabase.serialize() एक डेटाबेस में होता है, जिसमें मेमोरी में लोड की गई जानकारी मौजूद होती है.

कोई नया डेटाबेस न बनाएं

रनटाइम के दौरान, खाली इमेज का डेटाबेस बनाने के लिए, AugmentedImageDatabase कंस्ट्रक्टर का इस्तेमाल करें:

Java

AugmentedImageDatabase imageDatabase = new AugmentedImageDatabase(session);

Kotlin

val imageDatabase = AugmentedImageDatabase(session)

किसी मौजूदा डेटाबेस में इमेज जोड़ना

कॉल करके अपने इमेज डेटाबेस में इमेज जोड़ें AugmentedImageDatabase.addImage() हर इमेज के लिए, जिसमें widthInMeters डालना ज़रूरी नहीं है.

Java

Bitmap bitmap;
try (InputStream bitmapString = getAssets().open("dog.jpg")) {
  bitmap = BitmapFactory.decodeStream(bitmapString);
} catch (IOException e) {
  // The bitmap could not be found in assets; handle this error appropriately.
  throw new AssertionError("The bitmap could not be found in assets.", e);
}

// If the physical size of the image is not known, use addImage(String, Bitmap) instead, at the
// expense of an increased image detection time.
float imageWidthInMeters = 0.10f; // 10 cm
int dogIndex = imageDatabase.addImage("dog", bitmap, imageWidthInMeters);

Kotlin

val bitmap = assets.open("dog.jpg").use { BitmapFactory.decodeStream(it) }
// If the physical size of the image is not known, use addImage(String, Bitmap) instead, at the
// expense of an increased image detection time.
val imageWidthInMeters = 0.10f // 10 cm
val dogIndex = imageDatabase.addImage("dog", bitmap, imageWidthInMeters)

लौटाए गए इंडेक्स से, बाद में यह पता लगाया जा सकता है कि कौनसी पहचान इमेज की पहचान की गई.

इमेज ट्रैकिंग चालू करें

सेशन सेट करके, इमेज ट्रैक करने के लिए अपना ARCore सेशन कॉन्फ़िगर करें जिसे मनचाहे इमेज डेटाबेस के साथ कॉन्फ़िगर किया जा सकता है:

Java

Config config = new Config(session);
config.setAugmentedImageDatabase(imageDatabase);
session.configure(config);

Kotlin

val config = Config(session)
config.augmentedImageDatabase = imageDatabase
session.configure(config)

सेशन के दौरान ARCore, इमेज के लिए मिलते-जुलते फ़ीचर पॉइंट कैमरा इमेज से मिलता है.

मिलती-जुलती इमेज पाने के लिए, अपने फ़्रेम अपडेट लूप में, अपडेट किए गए AugmentedImage के लिए पोल करें.

Java

Collection<AugmentedImage> updatedAugmentedImages =
    frame.getUpdatedTrackables(AugmentedImage.class);
for (AugmentedImage img : updatedAugmentedImages) {
  if (img.getTrackingState() == TrackingState.TRACKING) {
    // Use getTrackingMethod() to determine whether the image is currently
    // being tracked by the camera.
    switch (img.getTrackingMethod()) {
      case LAST_KNOWN_POSE:
        // The planar target is currently being tracked based on its last
        // known pose.
        break;
      case FULL_TRACKING:
        // The planar target is being tracked using the current camera image.
        break;
      case NOT_TRACKING:
        // The planar target isn't been tracked.
        break;
    }

    // You can also check which image this is based on img.getName().
    if (img.getIndex() == dogIndex) {
      // TODO: Render a 3D version of a dog in front of img.getCenterPose().
    } else if (img.getIndex() == catIndex) {
      // TODO: Render a 3D version of a cat in front of img.getCenterPose().
    }
  }
}

Kotlin

val updatedAugmentedImages = frame.getUpdatedTrackables(AugmentedImage::class.java)

for (img in updatedAugmentedImages) {
  if (img.trackingState == TrackingState.TRACKING) {
    // Use getTrackingMethod() to determine whether the image is currently
    // being tracked by the camera.
    when (img.trackingMethod) {
      AugmentedImage.TrackingMethod.LAST_KNOWN_POSE -> {
        // The planar target is currently being tracked based on its last known pose.
      }
      AugmentedImage.TrackingMethod.FULL_TRACKING -> {
        // The planar target is being tracked using the current camera image.
      }
      AugmentedImage.TrackingMethod.NOT_TRACKING -> {
        // The planar target isn't been tracked.
      }
    }

    // You can also check which image this is based on AugmentedImage.getName().
    when (img.index) {
      dogIndex -> TODO("Render a 3D version of a dog at img.getCenterPose()")
      catIndex -> TODO("Render a 3D version of a cat at img.getCenterPose()")
    }
  }
}

इस्तेमाल के अलग-अलग उदाहरणों के साथ

जब ARCore को किसी ऑगमेंटेड इमेज का पता चलता है, तो यह उसके लिए Trackable बनाता है ऑगमेंटेड इमेज और सेट TrackingState TRACKING और TrackingMethod FULL_TRACKING तक. ट्रैक की गई इमेज के कैमरा व्यू से बाहर जाने पर, ARCore TrackingMethod को बदलता है स्क्रीन की दिशा और स्थिति बताते हुए LAST_KNOWN_POSE तक इमेज को हाइलाइट करें.

आपके ऐप्लिकेशन को इनका इस्तेमाल अलग-अलग तरीके से करना चाहिए. यह इस बात पर निर्भर करता है कि आपको किस तरह का इस्तेमाल करना है केस.

  • फ़िक्स्ड इमेज. इस्तेमाल के ज़्यादातर उदाहरण, जिनमें ऐसी इमेज शामिल हैं जो एक जगह पर ही मौजूद हैं (यानी, चलने की उम्मीद नहीं है) TrackingState का इस्तेमाल करके यह तय कर सकती है कि क्या इमेज का पता लगा लिया गया है और क्या उसकी जगह पता है. TrackingMethod को अनदेखा किया जा सकता है.

  • इमेज को एक जगह से दूसरी जगह ले जाना. अगर आपके ऐप्लिकेशन को किसी चलती हुई इमेज को ट्रैक करना है, तो दोनों का इस्तेमाल करें TrackingState और TrackingMethod पता लगाने के लिए कि इमेज को पता लगाया गया है और क्या उसकी स्थिति पता है.

इस्तेमाल का उदाहरण फ़िक्स्ड इमेज इमेज को एक जगह से दूसरी जगह ले जाया जा रहा है
उदाहरण दीवार पर लटका हुआ पोस्टर बस के बगल में दिख रहा विज्ञापन
पोज़
हो सकता है मान्य तब माना जाता है, जब
TrackingState == TRACKING TrackingState == TRACKING अभी तक किसी भी व्यक्ति ने चेक इन नहीं किया है
और
TrackingMethod == FULL_TRACKING

इन्हें भी देखें