Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tapToFocus not working #1771

Open
cryinrain69 opened this issue Mar 24, 2025 · 8 comments
Open

tapToFocus not working #1771

cryinrain69 opened this issue Mar 24, 2025 · 8 comments

Comments

@cryinrain69
Copy link

Hi.
I am using OpenGlView (preview) with GenericStream.
I use the following code to set focus when tapping on the screen but it does not get focus (even though it receives the tap event).

preview.setOnTouchListener { v, event ->
	if (event.action == MotionEvent.ACTION_UP) {
		(genericStream.videoSource as? Camera2Source)?.let { camera2Source ->
			Loggers.e(
				"preview setOnTouchListener",
				"isAutoFocusEnabled: ${camera2Source.isAutoFocusEnabled()}"
			)
			camera2Source.tapToFocus(event)
		}
	}
	false
}

I tried setting enableAutoFocus(), disableAutoFocus() but still no change.
Tested on devices: Samsung S22 Ultra (Android 14), Samsung S20 Plus (Android 13)

@pedroSG94
Copy link
Owner

Hello,

Which library version are you using?
Try with versions 2.5.9 and 2.5.8

@cryinrain69
Copy link
Author

Hi.
I am using code from master branch.
I just tried on version 2.5.8 and 2.5.9 but the result is still the same.

@pedroSG94
Copy link
Owner

OK, I will check it

@pedroSG94
Copy link
Owner

Hello,

I tested it with master branch using Camera2Source in rotation example and it is working as expected.
To totally notice if all is working fine try this:

  • Disable autofocus using disableAutoFocus method
  • Put your hand in a distance where focus is possible but the hand is not focused
  • Click the screen to focus your hand.

Video example:
https://github.com/user-attachments/assets/6da97d27-812c-48e0-96bc-0000fca4287a

Code modifications to test in app example (used for video example):

//In CameraFragment class, modify switch camera to disable and enable autofocus
    bSwitchCamera.setOnClickListener {
      when (val source = genericStream.videoSource) {
        is Camera1Source -> source.switchCamera()
        is Camera2Source -> {
          if (source.isAutoFocusEnabled()) {
            source.disableAutoFocus()
          } else {
            source.enableAutoFocus()
          }
        }
        is CameraXSource -> source.switchCamera()
      }
    }

//In RotationActivity class call tap to focus
  override fun onTouch(view: View, motionEvent: MotionEvent): Boolean {
    (cameraFragment.genericStream.videoSource as? Camera2Source)?.let {
      return it.tapToFocus(motionEvent)
    }
    if (filterMenu.spriteGestureController.spriteTouched(view, motionEvent)) {
      filterMenu.spriteGestureController.moveSprite(view, motionEvent)
      filterMenu.spriteGestureController.scaleSprite(motionEvent)
      return true
    }
    return false
  }

@cryinrain69
Copy link
Author

Hi.
After setting disableAutoFocus(), and calling tapToFocus(), it seems to automatically turn autoFocus back on.
Then if I call tapToFocus() again, there will be no change.

@pedroSG94
Copy link
Owner

Hello,

Are you using camera2? Because in my case the autofocus is not enabled after call tapToFocus. As you can see in the video, If you never tap the screen the focus is not set up.

@cryinrain69
Copy link
Author

Hi.
I am using Camera2Source, here is the code I am using and log.
I have already called disableAutoFocus() on initialization.

preview.setOnTouchListener { v, event ->
	if (event.action == MotionEvent.ACTION_DOWN) {
		(genericStream.videoSource as? Camera2Source)?.let { camera2Source ->
			Loggers.e(
				"preview setOnTouchListener",
				"isAutoFocusEnabled A: ${camera2Source.isAutoFocusEnabled()}"
			)
			val tapToFocus = camera2Source.tapToFocus(event)

			Handler(Looper.getMainLooper()).postDelayed({
				Loggers.e(
					"preview setOnTouchListener",
					"isAutoFocusEnabled B: ${camera2Source.isAutoFocusEnabled()}, tapToFocus: $tapToFocus"
				)
			}, 500)
			tapToFocus
		}
	}
	false
}

And logs:

11:39:55.902 Loggers     vn.app.demolive  E  preview setOnTouchListener: isAutoFocusEnabled A: false
11:39:56.408 Loggers     vn.app.demolive  E  preview setOnTouchListener: isAutoFocusEnabled B: true, tapToFocus: true

I see in the Camera2ApiManager class, it seems you have reset to auto focus mode after calling fun tapToFocus().

Image

@pedroSG94
Copy link
Owner

pedroSG94 commented Apr 8, 2025

Hello,

I added a commit that fix the problem with autofocus to properly work. Anyway, the line that you commented is used to work with tap to focus in all examples. For example official example from Google:
https://github.com/android/camera-samples/blob/main/Camera2Extensions/app/src/main/java/com/example/android/camera2/extensions/fragments/CameraFragment.kt#L1203
https://github.com/android/camera-samples/blob/main/Camera2Extensions/app/src/main/java/com/example/android/camera2/extensions/fragments/CameraFragment.kt#L324

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants