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

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