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

🐛 Android - Taking picture with automatic flashlight #569

Closed
3 of 4 tasks
Hajan39 opened this issue Nov 2, 2021 · 13 comments · Fixed by #1466
Closed
3 of 4 tasks

🐛 Android - Taking picture with automatic flashlight #569

Hajan39 opened this issue Nov 2, 2021 · 13 comments · Fixed by #1466
Labels
🤖 android Issue affects the Android platform 🐛 bug Something isn't working

Comments

@Hajan39
Copy link

Hajan39 commented Nov 2, 2021

What were you trying to do?

When I want to take the first picture in the darkness, the automatic flash is not working on Android. The second image is OK, the flash is flashing.

Reproduceable Code

import { Camera } from 'react-native-vision-camera';

const cameraRef = useRef<Camera>(null);
const takePicture = () => {
    if (allowedNext) {
      setAllowedNext(false);

      cameraRef.current
        ?.takePhoto({
          flash: 'auto',
          qualityPrioritization: 'speed',
        }).then(x=> console.log(x));
}r
return(
<>
{device ? (
        <Camera
          ref={cameraRef}
          style={{
            opacity: takingPicture ? 1 : 0,
            ...wh,
            backgroundColor: '#f8f9fa',
          }}
          device={device}
          isActive={true}
          preset={'photo'}
          zoom={currentZoom}
          onError={onError}
          onTouchEnd={x => device?.supportsFocus && onTouch(x.nativeEvent)}
          torch={device.hasTorch && torch ? 'on' : 'off'}
          hdr={true}
          onInitialized={cameraReady}
          enableZoomGesture={true}
          photo={true}
        />
      ) : (
        <Text>No device found</Text>
      )}
        <TouchableOpacity onPress={props.takePicture}  />
</>)

What happened instead?

The first image is without flash and the second image is with flash. The same darkness, the same spot, 2s later.

Relevant log output

No response

Device

Samsung s10e, Huawei P30lite

VisionCamera Version

2.9.3

Additional information

@Hajan39 Hajan39 added the 🐛 bug Something isn't working label Nov 2, 2021
@Hajan39
Copy link
Author

Hajan39 commented Nov 19, 2021

@mrousavy any update on this?

@mrousavy
Copy link
Owner

Hey! I haven't had time to look into this yet.

@Hajan39
Copy link
Author

Hajan39 commented Jan 21, 2022

Hello, would it be possible to let me know if there is a plan to fix this, please? I always have the first picture black and others are fine.

@mrousavy
Copy link
Owner

mrousavy commented Jan 21, 2022

Hey again! I still haven't had time to investigate this, I am currently really busy with the agency I'm running (https://margelo.io). You can hire us to fix it, or put a bugbounty on this issue, but I don't really have any free time at the moment.

Also, I don't have neither of those devices and for me (Huawei P10) the flash correctly fires.

Maybe it is also a native CameraX library issue and you could try replicate it in a native Android app, and if it is, post an issue in the Google Issue Tracker for CameraX.

@mrousavy mrousavy added the 🤖 android Issue affects the Android platform label Jan 21, 2022
@Hajan39
Copy link
Author

Hajan39 commented Jan 22, 2022

To be precise, it is only the first attempt. The flash is not working only for the first picture when you open an app in complete darkness. Others are fine, but only the first one is not working.

@Hajan39
Copy link
Author

Hajan39 commented Jan 26, 2022

@mrousavy I think I found a solution - it seems that this code is not working correctly in the CameraView+TakePhoto.kt:

imageCapture!!.flashMode = when (flashMode) {
"on" -> ImageCapture.FLASH_MODE_ON
"off" -> ImageCapture.FLASH_MODE_OFF
"auto" -> ImageCapture.FLASH_MODE_AUTO
else -> throw InvalidTypeScriptUnionError("flash", flashMode ?: "(null)")
}

It looks like the flashMode should be set when creating the imageCapture !or! it has to be rebuilt somehow - sorry, I am not a Kotlin developer, but I am trying my best.
I have tested Android app and the flash is working great, just because it is set directly to the ImageCapture.Builder().

I have changed your code in CameraView.kt this:

val flashMode = "auto"
val modeFlash = when (flashMode) {
"on" -> ImageCapture.FLASH_MODE_ON
"off" -> ImageCapture.FLASH_MODE_OFF
"auto" -> ImageCapture.FLASH_MODE_AUTO
else -> throw InvalidTypeScriptUnionError("flash", flashMode ?: "(null)")
}
imageCaptureBuilder.setFlashMode(modeFlash)

And it is working! I have no answer to the problem, but this is a solution. Can you please help me add flashMode as a parameter in the object?
Thank you.

@TasigurIshtar
Copy link

Hello,
I can confirm same behavior on Xiaomi Mi9t.
Flash on Auto & dark room.
First shot is without flash, the others are with flash.

@Hajan39
Copy link
Author

Hajan39 commented Jan 26, 2022

@mrousavy Please have a look at this change - it is working correctly
main...Hajan39:main

@mrousavy
Copy link
Owner

Well yeah, but we want to set flash mode per photo capture, not on the Camera in the React lifecycle.

@Hajan39
Copy link
Author

Hajan39 commented Jan 26, 2022

OK, but as you can see, this is not working correctly on more devices. I think you will find more devices ...
AFAIK this has a late initialization, and it changes on the second attempt. Every time you set an "auto" flash mode, the first image is black. I am not sure if this is the problem of CameraX, but this approach seems to be wrong.

@alexstanbury
Copy link
Contributor

alexstanbury commented Mar 30, 2022

FWIW I'm seeing the same issue on a Xiaomi Mi9 and Samsung Galaxy S7

@2sem
Copy link

2sem commented May 19, 2023

some device flash not working...
os problem?

@mrousavy
Copy link
Owner

Hey! I've rewritten the entire Android codebase of VisionCamera from CameraX to Camera2 in the efforts of ✨ VisionCamera V3.

I just now completed the Camera2 rewrite and I believe the core structure is running, but there might be some edge cases to iron out. Can you try and test the PR #1674 for me to see if you can still reproduce this issue here?

Here's an instruction on how you can test that: #1674 (comment)

If the issue cannot be reproduced with that version/PR anymore, then hoorayy, I fixed it! 🎉
Otherwise please let me know and I'll keep this issue open to keep track of it.

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🤖 android Issue affects the Android platform 🐛 bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants