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

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

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

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

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

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

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

  • सेव की गई इमेज का डेटाबेस लोड करना. इसके बाद, ज़रूरत पड़ने पर ज़्यादा रेफ़रंस इमेज जोड़ें.
  • नया खाली डेटाबेस बनाएं. इसके बाद, एक बार में एक रेफ़रंस इमेज जोड़ें.

सेव की गई इमेज का डेटाबेस लोड करना

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

JavaKotlin
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.
}
val imageDatabase = this.assets.open("example.imgdb").use {
  AugmentedImageDatabase.deserialize(session, it)
}

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

नया खाली डेटाबेस बनाना

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

JavaKotlin
AugmentedImageDatabase imageDatabase = new AugmentedImageDatabase(session);
val imageDatabase = AugmentedImageDatabase(session)

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

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

JavaKotlin
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);
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 सेशन को कॉन्फ़िगर करें. इसके लिए, सेशन कॉन्फ़िगरेशन को उस डेटाबेस पर सेट करें जिसे आपको ट्रैक करना है:

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

सेशन के दौरान, ARCore कैमरे की इमेज के फ़ीचर पॉइंट को इमेज डेटाबेस में मौजूद फ़ीचर पॉइंट से मैच करके इमेज ढूंढता है.

मैच होने वाली इमेज पाने के लिए, अपने फ़्रेम अपडेट लूप में अपडेट किए गए AugmentedImage के लिए पोल करें.

JavaKotlin
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().
    }
  }
}
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

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