Sign up for our live Camera Kit office hour on July 17 @ 9:30am (PT) / 4:30pm (GMT)

How to configure a Session to start a specific Lens (Android)

Harrison Shapley
Harrison Shapley Posts: 8
edited August 2022 in General #1

I'm having trouble finding good documentation for how to configure a Session. This sample code snippet doesn't work for me:

cameraKitSession.getLenses().getRepository().get(new Available(LENS_GROUP_ID), available -> {
            Log.d(TAG, "Available lenses: " + available);
            Lenses.whenHasFirst(available, lens -> cameraKitSession.getLenses().getProcessor().apply(lens, result -> {
                Log.d(TAG,  "Apply lens [" + lens + "] success: " + result);
            }));
        });

because the Lenses class doesn't resolve. But my use case is slightly different anyway - I want to go straight to a specific Lens ID, and I don't need a carousel. How do I achieve that? Thanks.

Comments

  • stevenxu
    stevenxu Posts: 612 👻

    @Harrison Shapley Thanks for the post. Sharing with our team to get you a response as soon as we can. Hang tight

  • Related question / different way of asking the question: How can I get/create a Lens object?

  • stevenxu
    stevenxu Posts: 612 👻

    @Harrison Shapley
    One of our sample apps here demos a use case that is very similar to what you are asking

  • Thanks. I can get it to compile with that code, but it's crashing. The crash logs are obfuscated - the best I get is:
    java.lang.NullPointerException: Attempt to invoke interface method 'void t0.a.b(java.lang.String, java.lang.Object[])' on a null object reference

    Here is my code:

    session.lenses.repository.get(LensesComponent.Repository.QueryCriteria.Available(LENS_GROUP_ID)) { available ->
                        available.whenHasFirst { lens ->
                            session.lenses.processor.apply(lens)
                        }
                    }
    

    It seems to be crashing on the whenHasFirst line.

  • But I also have the question: If I have a Lens Group with multiple lenses, but want to apply a specific lens (not the first one), how would I do that?

  • I was able to solve both issues after playing around a bit. For others struggling with this, here is what I ended up with:

    session.lenses.repository.get(LensesComponent.Repository.QueryCriteria.ById(WATCH_LENS_ID, LENS_GROUP_ID)) { result ->
      result.whenHasFirst { lens ->
        session.lenses.processor.apply(lens) 
      }
    }
    
  • stevenxu
    stevenxu Posts: 612 👻
    edited August 2022 #8

    @Harrison Shapley Glad to hear you were able to solve your issue!! (Best feeling). Thanks for closing the loop. Let me know if you have any other open issues related to this topic.

  • I'd like to do the same thing on iOS, is there example code for iOS? Right now I can get my app to launch but it starts on the empty filter circle and I have to swipe/tap to get the lens to launch, where I'd prefer if it launched into the lens without the user having to do anything.

  • stevenxu
    stevenxu Posts: 612 👻

    @Jeremy Bailey Looking into this...

  • arashp
    arashp Posts: 52 🔥🔥

    Hi @Jeremy Bailey-- the carousel starts on the empty circle because CarouselView.selectedItem is initialized to EmptyItem(), but you can initialize it to any Lens you like:

    var selectedItem = CarouselItem(lensId: lensId, groupId: myGroupId)
    
  • Jeremy Bailey
    Jeremy Bailey Posts: 2
    edited October 2022 #12

    ah yes thank you @arashp ! that worked! BUT... it only selects the lens, for some reason it doesn't appear – I have to swipe back to the empty circle and then back again for the filter to appear.