Key Detection to capture an item with android.hardware.camera2

I am currently implementing Android Camera 2 as a Preview in a surface viewer on the Android side which then i use custom renderer to show it on my app. Is there a way for me to capture an image when user press on either of the volume button?

Answers

  • JuniorJiangJuniorJiang Member, Xamarin Team Xamurai
    edited December 2018

    Hi @ncmj1998 you can monitor the status of the volume button to capture an image.

    public override bool OnKeyDown([GeneratedEnum] Keycode keyCode, KeyEvent e)
    {
        switch (keyCode)
        {
            // VOLUME_DOWN
            case Keycode.VolumeDown:
                //if want VOLUME_DOWN to capture an image , add method here.
                return true;
    
            //VOLUME_UP
            case Keycode.VolumeUp:
                //if want VOLUME_UP to capture an image , add method here.
                return true;
        }
    
        return base.OnKeyDown(keyCode, e);
    }
    
  • ncmj1998ncmj1998 Member ✭✭

    @JuniorJiang hey.

    The codes work however if i put it at MainActivity , the detection is for the whole app. However, i only want the detection to be available when I'm at the page where I have a custom renderer to show a camera preview. I tried putting the code on the activity where I initiliase the renderer but the keycode doesnt seem to get detected .

  • JuniorJiangJuniorJiang Member, Xamarin Team Xamurai

    Ok @ncmj1998 ,if not work in other activity, try to add OnKeyDown method to your custom view not in activity.And view need to set property:SetFocusable(ViewFocusability.Focusable), and you can post code of the activity.

Sign In or Register to comment.