hjam40 / Camera.MAUI

A CameraView Control for preview, take photos and control the camera options
MIT License
458 stars 75 forks source link

Cameras not being detected on published app, works just fine when build set to Debug. #8

Closed MosheMalatji closed 1 year ago

MosheMalatji commented 1 year ago

I am using the camera view to take pictures, it works well when I set the build to Debug however as soon as I publish the app and change build to Release the app crashes. It's almost as if it no longer detects the cameras. I have tried it with this sample (https://github.com/jfversluis/MauiCameraMauiSample) as well with the same issues, is there something I am doing wrong ?

See screenshots attached.

cameraMauiRelease

CameraMauiDebug

taublast commented 1 year ago

It looks like you're missing camera permissions inside your manifest. In case you are compiling versus api 33 (android:targetSdkVersion="33"):

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"  android:maxSdkVersion="28"/>
<uses-permission android:name="android.permission.CAMERA" />

Btw you could connect you device and launch release build while having been typed in terminal "adb logcat" to see in logs why you are crashing.

Now to be able to save your photos You'd need to manually request Storage access permissions for android below 10 only. Those above do not need them to save and read their own photos (compiling vs api >= 33 only), moreover if you request this permission for android 13 you will not get a prompt and the permission will always be refused, so..

MosheMalatji commented 1 year ago

Thank you for your suggestion I will check the logcat however I can confirm that I am requesting camera permissions, the dialog does appear at the beginning, I've tested on 2 devices a Poco M3 Pro(Android 12) and a Xiaomi Redmi 8A(Android 11) with the same result.

see android manifest below

<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <application android:allowBackup="true" android:icon="@mipmap/icon" android:roundIcon="@mipmap/icon_round" android:supportsRtl="true">
        <provider android:name="androidx.core.content.FileProvider"
          android:authorities="${applicationId}.fileprovider"
          android:exported="false"
          android:grantUriPermissions="true">

            <meta-data android:name="android.support.FILE_PROVIDER_PATHS"
                           android:resource="@xml/file_paths"></meta-data>
        </provider>
    </application>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
    <uses-feature android:name="android.hardware.location" android:required="false" />
    <uses-feature android:name="android.hardware.location.gps" android:required="false" />
    <uses-feature android:name="android.hardware.location.network" android:required="false" />  <queries>
        <intent>
            <action android:name="android.media.action.IMAGE_CAPTURE" />
        </intent>
    </queries>
</manifest>
PeterVanVogelpoel commented 1 year ago

I have the same issue. Camera not running in Release. In Debug no problem. (Same problem with test app here)

taublast commented 1 year ago

What is the android version of devices that are failing? What is android sdk you are compile with?

Again, if you are compiling vs api33 (android 13) remove <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"> from manifest, use only

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"  android:maxSdkVersion="28"/>
<uses-permission android:name="android.permission.CAMERA" />

if you are compiling for lower api you must have both read and write permissions declarations inside manifest.

Now about the code inside app. You have to check current android version using DeviceInfo. If it's android >=13 do nothing. If it's lower android: A - If you compiled vs api33: ask user for permissions Permissions.StorageWrite if this device android is < 10 B - If you compiled vs lower api ask user for permissions Permissions.StorageWrite if this device is running < 13

Again you have to manually ask user for storage write permissions (Essentials Permissions.RequestAsync), having declaration in the manifest is not enough. cameraview will not do that for you, and such code is missing in the example app at the moment.

PeterVanVogelpoel commented 1 year ago

Hi, I'm targetting vs 33.

uses-sdk android:minSdkVersion="29" android:targetSdkVersion="33"

Taking out or in uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" makes no difference.

Debug all fine. In Release the app is collapsing while starting the camera. Same in your Camera.MAUI.Test

taublast commented 1 year ago

1 on which version of android does crash, is it real device or emulator? 2 what exactly does adb log say for the crash? 3 have you implemented any code for Permissions.StorageWrite?

PeterVanVogelpoel commented 1 year ago

It happens on my device Samsung Galaxy Note20 Ultra (Android 13). Read or Write_External_Storage makes no difference. (Not need it anyhow) I don't know how it works with adb log.

And again. Your Camera.MAUI.Test has the same problem. In Debug all fine. In Release camera not woring.

PeterVanVogelpoel commented 1 year ago

Also (https://github.com/jfversluis/MauiCameraMauiSample) has the same problem.

taublast commented 1 year ago

1 this this not my repository 2 does your error happen when you click on Start button or when you open the page with camera? If it happens when you click on button my quick guess you just have to wrap all calls to cameraView.StartCameraAsync(); with a MainThread.BeginInvokeOnMainThread, that could matter if on Release the optimized code launches "async void" handlers in back thread, then calls camera permissions and crashes because this must be done on ui thread only.

PeterVanVogelpoel commented 1 year ago

In Camera.MAUI.Test (Release) nothing happens when click on Start Camera button. In Debug all fine.

taublast commented 1 year ago

My guessing was wrong, for me cameraView.Cameras are just empty on Release. Tried a quick workaround, but no luck.

image

PeterVanVogelpoel commented 1 year ago

I don't need to release tomorrow. So I can continue developing and debugging. I hope you are able to find the issue. I very much appreciate your repository.

quanzhenying commented 1 year ago

Experiencing the same problem,debug mode was ok,but RC version failed. Log: 'cameraView.Cameras.Count' is 0

hjam40 commented 1 year ago

Hi all, I’m changing the entire android handler for the new release and adapt it to Camera2 and not CameraX (as now). I hope that it resolves all android problems.

AmmarAbo commented 1 year ago

Hi All, @hjam40 when do you expect this problem will be solving solve? Thanks

hjam40 commented 1 year ago

Hi,

I have published a new realease 1.3. It changes the android handler and this problems should be resolved. Also It adds recording video feature.

PeterVanVogelpoel commented 1 year ago

Great! It seems to work perfectly. Only the Zoomfactor seems not to work. Is that so?

hjam40 commented 1 year ago

Hi Peter,

with the new handler, zoom only works on API 30 or greater (at least for the moment).

PeterVanVogelpoel commented 1 year ago

Hi Hjam40, I'm using API 33, but the ZoomFactor seems to have no effect. And another thing is that it seems that the scanner does not respond when reading the same QR Code for the second time. Is that intended?

hjam40 commented 1 year ago

Hi Peter,

I'm trying the ZoomFactor in all platforms and it works fine for me... Could you verify the MaxZoomFactor for your selected Camera?

In reference to the scanner, yes, I changed the event behaviour to trigger only if the result is different from the previous but I'm thinking that maybe is not a good idea and I will create a property for set the behaviour as the user prefers.

PeterVanVogelpoel commented 1 year ago

Hi hjam40, sorry to bother you about the ZoomFactor. Wrong coding on my end. Problem solved. And yes, I prefer that the scanner can read the same code multiple times. One more question: I have a square frame for my cameraView (300 x 300). But then the image is stretched in height. Is there a way to solve that? And, what exactly does the BarCodeDetectionFrameRate mean/do?

PeterVanVogelpoel commented 1 year ago

And sometimes I'm getting: Android.Hardware.Camera2.CameraAccessException: 'CAMERA_ERROR (3): endConfigure:740: Camera 0: Error configuring streams: Broken pipe (-32)' Is this my code or something else?

hjam40 commented 1 year ago

Hi Peter,

Could you show me a capture of CameraView (300x300)? I have been days and days testing differents options for TextureView preview streching.... Rigth now, the strech beahviour depends of video resolution selected by "Android" and the Width and Height of the control. You can change the behaviour playing with this properties, You could try changing the size 300x299 or 299x300.

In reference to the error, maybe you are not closing properly the camera before try to use it again... I'm not sure how MAUI disconect the handlers when you go out of the page, but I have seen that the dispose method is not called a lot of times and maybe the camera is in use by a control when another try to use it. The handler releases the camera everytime you call StopPreviewAsync, ensure you call it when you want release a control for use another.

PeterVanVogelpoel commented 1 year ago

Screenshot_20230410_123312

PeterVanVogelpoel commented 1 year ago

Is there a way to detect video resolution selected by Android?

PeterVanVogelpoel commented 1 year ago

And I'm using StopCameraAsync. I don't find StopPreviewAsync.

hjam40 commented 1 year ago

Sorry, my mistake, it's correct StopCameraAsync.

No way (at least I didn't find it) to know the Camera video resolution using in a preview in Android....

TT702 commented 1 year ago

Hi Peter,

Could you show me a capture of CameraView (300x300)? I have been days and days testing differents options for TextureView preview streching.... Rigth now, the strech beahviour depends of video resolution selected by "Android" and the Width and Height of the control. You can change the behaviour playing with this properties, You could try changing the size 300x299 or 299x300.

Having the similar issue after updating to 1.3. The preview seems to be weirdly stretched.

da0f0d5da635c4d2bdf481fbccd3edd abc7b07d60ed24acab14d3e522d8383

PeterVanVogelpoel commented 1 year ago

Hi, I got it all working fine now. Thanks for the hard work.
Except for the stretching of the preview. This was not there in the previous version. I hope this can be resolved.

MosheMalatji commented 1 year ago

Thanks for the fix Hector, it works now with version 1.3. Keep up the good work.

hjam40 commented 1 year ago

Hi,

I have created a new realease 1.3.1 where I have changed the aspect ratio for Android Preview... I hope it works for you.

PeterVanVogelpoel commented 1 year ago

After update I'm getting: System.InvalidCastException: 'Specified cast is not valid.' at StartCameraAsync

hjam40 commented 1 year ago

Hi Peter,

Sorry, but I can't get that error... Could you give me more information? Maybe the line where the exception throws.. You could clone the git repository and run the app on your device.