react-native-vision-camera icon indicating copy to clipboard operation
react-native-vision-camera copied to clipboard

🐛 `focus()` does not work and stops auto-focus

Open rkostrab opened this issue 1 year ago • 12 comments

What's happening?

This is similar issue as this https://github.com/mrousavy/react-native-vision-camera/issues/1938 but on Android platform. It seem it just turn off autofocus on Android. In V2 it is working well!

Reproduceable Code

const camera = useRef()

...

<Camera
  ref={camera}
  style={styles.container}
  device={device}
  photo
  zoom={zoom}
  isActive={isScreenFocused && !pictureTaken}
  onInitialized={() => setCameraReady(true)}
  orientation='portrait'
  onError={onError}/>

...

[some button press while camera ready] () => camera.current.focus({ x: 300, y: 300 });

Relevant log output

[no relevant log]

Camera Device

{
  "formats": undefined,
  "sensorOrientation": "landscape-right",
  "hardwareLevel": "full",
  "maxZoom": 20,
  "minZoom": 0.6704425811767578,
  "supportsLowLightBoost": true,
  "neutralZoom": 1,
  "physicalDevices": [
    "wide-angle-camera"
  ],
  "supportsFocus": true,
  "supportsRawCapture": true,
  "isMultiCam": true,
  "name": "BACK (0)",
  "hasFlash": true,
  "hasTorch": true,
  "position": "back",
  "id": "0"
}

Device

Google Pixel 6 Pro

VisionCamera Version

3.3.1

Can you reproduce this issue in the VisionCamera Example app?

I didn't try (⚠️ your issue might get ignored & closed if you don't try this)

Additional information

rkostrab avatar Oct 14 '23 22:10 rkostrab

There's not enough details here. What do you mean it's not working? What's happening instead?

mrousavy avatar Oct 16 '23 10:10 mrousavy

@mrousavy when I call cameraRef.current.focus({ x: XXX, y: YYY }) then camera does not focus object at point XXX,YYY. Instead of that it looks like it just turns off autofocus. No any error log. I am trying to migrate from v2 to v3. In v2 it is focusing correctly.

I created a full replicable code for expo project (copy&paste into App.js). Try:

import "react-native-gesture-handler";
import { useEffect, useRef } from "react";
import { Text, ToastAndroid } from "react-native";
import {
  Camera,
  useCameraDevice,
  useCameraPermission,
} from "react-native-vision-camera";
import {
  TapGestureHandler,
  GestureHandlerRootView,
  State,
} from "react-native-gesture-handler";

export default function App() {
  const camera = useRef();
  const device = useCameraDevice("back");
  const { hasPermission, requestPermission } = useCameraPermission();

  useEffect(() => {
    if (!hasPermission) {
      requestPermission();
    }
  }, [hasPermission]);

  const onFocus = (event) => {
    if (event.nativeEvent.state !== State.ACTIVE) return;
    const point = {
      x: Math.round(event.nativeEvent.x),
      y: Math.round(event.nativeEvent.y),
    };
    try {
      ToastAndroid.show(
        "Focusing (support=" +
          device.supportsFocus +
          ") on point: " +
          JSON.stringify(point),
        1000
      );
      camera.current.focus(point);
    } catch (error) {
      console.error(error.message);
    }
  };

  if (!hasPermission)
    return <Text style={{ flex: 1 }}>{"No persmission"}</Text>;

  return (
    <GestureHandlerRootView style={{ flex: 1 }}>
      <TapGestureHandler onHandlerStateChange={onFocus}>
        <Camera
          ref={camera}
          style={{ flex: 1 }}
          device={device}
          torch={"off"}
          orientation="portrait"
          isActive={hasPermission}
          onError={(error) => console.error(error.message)}
        />
      </TapGestureHandler>
    </GestureHandlerRootView>
  );
}

Here's video of testing that code: https://imgur.com/a/yIZNhFI

rkostrab avatar Oct 16 '23 13:10 rkostrab

Okay yea, I think I know why. Takes some time to fix though

mrousavy avatar Oct 16 '23 13:10 mrousavy

Hey guys, any update on this?

nBanousi avatar Oct 31 '23 12:10 nBanousi

Hey, nope. I did not have any time to investigate this yet as I have many other priorities. If anyone wants this fixed, consider sponsoring me / paying me my consultancy fee so I can dedicate time to investigate this and fix the bug :)

DM me for details

mrousavy avatar Oct 31 '23 13:10 mrousavy

Hey, nope. I did not have any time to investigate this yet as I have many other priorities. If anyone wants this fixed, consider sponsoring me / paying me my consultancy fee so I can dedicate time to investigate this and fix the bug :)

DM me for details

Hi @mrousavy I sponsored a very similar issue on iOS, I wonder if both are due to the same root cause? #2120 Thanks!

rsnr avatar Nov 05 '23 07:11 rsnr

@rsnr Hey - might be! I was on vacation last week and today is Sunday, I'll be back on Tuesday! :)

mrousavy avatar Nov 05 '23 10:11 mrousavy

is there any new information about this issue?

EuSet avatar Nov 23 '23 13:11 EuSet

Not yet, it's on my priority list though! I just fixed a critical synchronization bug here: https://github.com/mrousavy/react-native-vision-camera/pull/2206 and will focus on focus() (pun intended) soon!

mrousavy avatar Nov 23 '23 17:11 mrousavy

Thanks @rsnr for sponsoring this btw 🙏

mrousavy avatar Nov 23 '23 17:11 mrousavy

I'm currently receiving the following error when I call camera.current.focus() on Android. All other camera functions (zoom, flash, takePhoto(), etc.) are working as expected on the same screen. I'm seeing the tap gesture being received and handled properly so I don't think it's related to that.

Captured from Logcat:

com.mrousavy.camera.core.CameraNotReadyError: [session/camera-not-ready] The Camera is not ready yet! Wait for the onInitialized() callback!
at com.mrousavy.camera.core.CameraSession.focus(CameraSession.kt:579)
at com.mrousavy.camera.core.CameraSession.focus(CameraSession.kt:572)
at com.mrousavy.camera.CameraView_FocusKt.focus(CameraView+Focus.kt:8)
at com.mrousavy.camera.CameraViewModule$focus$1.invokeSuspend(CameraViewModule.kt:141)

I'd take the error more at face value but focus() call only fires if onInitialized has already occurred.

Brianwebb22 avatar Nov 29 '23 21:11 Brianwebb22

Yup thanks - focus is just not properly implemented right now. That's my fault, it was a bit more complex than I originally thought and other issues were just higher priority at that time. I'm back from my short vacation and I'll take a look at this very soon, it's pretty high priority on my TODO list.

First some emails to catch up on haha.

Thanks for the sponsorship @Brianwebb22 !

mrousavy avatar Dec 06 '23 15:12 mrousavy

Working on focus this week 💪

mrousavy avatar Jan 15 '24 13:01 mrousavy

@mrousavy how does it look with the focus correction?

MilanRadon avatar Jan 22 '24 16:01 MilanRadon

Didn't finish it yet. had some other issues to look into first and released vision-camera-resize-plugin, which had higher priority for me.

mrousavy avatar Jan 23 '24 14:01 mrousavy

@mrousavy thanks for reply. When y mean will be done?

MilanRadon avatar Jan 23 '24 18:01 MilanRadon

No idea. I don't have it planned for now as it's not a paid feature, we'll see when I have some free time. :)

mrousavy avatar Jan 24 '24 10:01 mrousavy

I finally implemented focus now in #2523!

VisionCamera 3.9.0-beta.3 now includes this feature! :)

mrousavy avatar Feb 08 '24 18:02 mrousavy