Search Unity

Native Camera for Android & iOS [Open Source]

Discussion in 'Assets and Asset Store' started by yasirkula, May 2, 2018.

  1. wellzito

    wellzito

    Joined:
    Mar 29, 2017
    Posts:
    6

    After the latest updates, do you think it's possible to start recording automatically nowadays? The stop was simple to do, just set the time I want. But I wanted to auto start the recording.
     
  2. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Try adding
    if( !imageMode ) [imagePicker startVideoCapture];
    after this line and see how it goes. This isn't possible on Android.
     
  3. wellzito

    wellzito

    Joined:
    Mar 29, 2017
    Posts:
    6

    Hi, thanks for replying! Too bad you can't have the automatic fire control on Android. However, I wanted to know if it is possible to have control of the time counter after I manually press the record button. In this case, I want to set and save the countdown.
     
  4. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    If you're asking about retrieving the remaining value of maxDuration (e.g. 2 seconds of maximum-5-second-long recording has passed), I didn't see a function for it. On iOS, these are the only available functions provided by the operation system: https://developer.apple.com/documentation/uikit/uiimagepickercontroller?language=objc. On Android, you can't rely on anything at all since different stock/app store camera apps can behave differently and there isn't a unified API for them. Some camera apps don't even respect maxDuration :eek:

    If instead you're talking about a 3-2-1 countdown, I haven't seen a method to get its current value or set its duration. Please see the link I've provided for all available functions.
     
  5. shoib_akthar

    shoib_akthar

    Joined:
    Oct 21, 2021
    Posts:
    4
    Hi @yasirkula ,Is it possible to prompt only camera permission but not access to images and video in Android as I'm not storing anything in the device.Im only using camera For AR.
     
  6. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Some Android devices allegedly store the full-resolution image/video in Gallery instead of the app cache, so we need the Gallery permission to retrieve it from there.
     
  7. shoib_akthar

    shoib_akthar

    Joined:
    Oct 21, 2021
    Posts:
    4
    I understand but is possible to only show camera access?
     
  8. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    You must modify the source code in Android Studio and rebuild NativeCamera.aar for it.
     
  9. eagleeyez

    eagleeyez

    Joined:
    Mar 21, 2013
    Posts:
    406
    Hi there, why is there no unity demo scene in the package? That would be great thanks.
     
  10. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
  11. eagleeyez

    eagleeyez

    Joined:
    Mar 21, 2013
    Posts:
    406
  12. eagleeyez

    eagleeyez

    Joined:
    Mar 21, 2013
    Posts:
    406
    Just make a demo, or is that so hard?
     
  13. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
  14. eagleeyez

    eagleeyez

    Joined:
    Mar 21, 2013
    Posts:
    406
    :) Anyone maybe have a small example of just a simple full screen camera and have that picture saved to a unity texture. I am then going to use another asset to send this picture over wifi to a unity app running on the pc
    thank you.
     
    yasirkula likes this.
  15. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    You can add this function to a script in your scene and connect it to e.g. a button's On Click event:
    Code (CSharp):
    1. public void TakePicture( int maxSize = 1024 )
    2. {
    3.     NativeCamera.TakePicture( ( path ) =>
    4.     {
    5.         if( path != null )
    6.         {
    7.             // 1) Either send the image file at path to another PC directly (image size might be a few MBs depending on camera quality)
    8.            
    9.             // 2) Or, create a Texture2D (scaled down if needed to satisfy maxSize pixels in each dimension) from the image file and send it to another PC
    10.             Texture2D texture = NativeCamera.LoadImageAtPath( path, maxSize );
    11.             if( texture == null )
    12.             {
    13.                 Debug.Log( "Couldn't load texture from " + path );
    14.                 return;
    15.             }
    16.            
    17.         }
    18.     }, maxSize );
    19. }
     
  16. Riken_rds

    Riken_rds

    Joined:
    Apr 4, 2017
    Posts:
    49
    I am facing issue in android 13 .
    While using camera for taking pictures it's redirect to native permission screen event is permission is granted .this happens only in Android 13.
     
  17. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
  18. Shreenath1322

    Shreenath1322

    Joined:
    Apr 7, 2016
    Posts:
    7
    is it possible to open both the front and back camera at the same time in unity?
     
  19. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
  20. waqarahmed1

    waqarahmed1

    Joined:
    Mar 21, 2023
    Posts:
    1
    Hi @yasirkula
    Awesome package and easy to use.
    Is it possible to turn off the switching of camera on IOS, in my use case I want only front camera ?
    Is it possible to save the images and videos in "Files" folder using NativeGallery. I can save the images and videos in "Photos" but I want to save them in "Files" as well ?
    Is it possible to give the desired name to a file before saving it using NativeGallery ?
    Kindly please help.
     
  21. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Hi @waqarahmed1, for the first question, please see this thread. You can't save files to Files using NativeGallery but I believe you can do that with NativeFilePicker. You can specify a custom filename in NativeGallery's save function but if you'd like, you can also File.Copy the returned path to a new path with the desired name.
     
  22. Liminal-Ridges

    Liminal-Ridges

    Joined:
    Oct 21, 2015
    Posts:
    256
    hey i get null path after taking the image
     
  23. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Can you spot useful logs, is it reproducable on an empty project, does something visibly go wrong in the camera app?
     
  24. Liminal-Ridges

    Liminal-Ridges

    Joined:
    Oct 21, 2015
    Posts:
    256
    its a very basic URP project, just a single script with your example code a few posts above. Dont thing its a permission thing cause the camera opens but then returns empty path..
     
  25. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    It unfortunately doesn't happen on my Android phone (Galaxy S8). My guess is that it happens on some specific devices and doesn't output useful logs to logcat. I don't think NativeCamera is compatible with that device for reasons I'm unaware of.
     
  26. Liminal-Ridges

    Liminal-Ridges

    Joined:
    Oct 21, 2015
    Posts:
    256
    This is beyond disappointing :/
     
  27. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Yeah, I'm using the standard way of opening the camera. It's annoying that some devices just don't want to comply with Android's standards. They usually don't provide useful information in logcat either. They just fail to work.
     
  28. Kasi2302

    Kasi2302

    Joined:
    Sep 30, 2020
    Posts:
    25
    Hello,

    first thank you very much YASIRKULA for your great work !!!

    I must add a Line in AndroidManifest.xml otherwise Gradle failed.

    <provider
    android:name="com.yasirkula.unity.NativeCameraContentProvider"
    android:authorities="MysterySoft_HonigMetPictureLogger"
    tools:replace="android:authorities" <-- New added Line !!!
    android:exported="false"
    android:grantUriPermissions="true" />

    I don't understand it, does anyone know why it is ?



    NativeCamera01.jpg


    NativeCamera02.jpg
     
  29. Kasi2302

    Kasi2302

    Joined:
    Sep 30, 2020
    Posts:
    25
    My complete AndroidManifest.xml

    Code (JavaScript):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" package="com.MysterySoft.HMPictureLogger" android:installLocation="preferExternal" android:versionCode="1" android:versionName="1.0">
    3.     <uses-feature android:name="android.hardware.camera" android:required="true" />
    4.     <uses-permission android:name="android.permission.INTERNET" />
    5.     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    6.     <supports-screens android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:xlargeScreens="true" android:anyDensity="true" />
    7.     <application android:theme="@android:style/Theme.NoTitleBar.Fullscreen" android:icon="@drawable/app_icon" android:label="@string/app_name" android:debuggable="false" android:allowBackup="false" android:fullBackupContent="false" android:requestLegacyExternalStorage="true">
    8.         <activity android:name="com.unity3d.player.UnityPlayerActivity" android:label="@string/app_name">
    9.             <intent-filter>
    10.                 <action android:name="android.intent.action.MAIN" />
    11.                 <category android:name="android.intent.category.LAUNCHER" />
    12.             </intent-filter>
    13.             <meta-data android:name="unityplayer.UnityActivity" android:value="true" />
    14.         </activity>
    15.         <provider
    16.             android:name="com.yasirkula.unity.NativeCameraContentProvider"
    17.             android:authorities="MysterySoft_HonigMetPictureLogger"
    18.             tools:replace="android:authorities"
    19.             android:exported="false"
    20.             android:grantUriPermissions="true" />
    21.     </application>
    22. </manifest>
     
  30. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    @Kasi2302 Hi! You mustn't declare NativeCamera's provider in your own AndroidManifest. It's added by the plugin automatically. Can you try removing it from your AndroidManifest?
     
  31. Kasi2302

    Kasi2302

    Joined:
    Sep 30, 2020
    Posts:
    25
    S
    Sorry I don't understand o_O
     
  32. Kasi2302

    Kasi2302

    Joined:
    Sep 30, 2020
    Posts:
    25
    you mean remove this lines ?

    Code (JavaScript):
    1. <provider
    2.             android:name="com.yasirkula.unity.NativeCameraContentProvider"
    3.             android:authorities="MysterySoft_HonigMetPictureLogger"
    4.             tools:replace="android:authorities"
    5.             android:exported="false"
    6.             android:grantUriPermissions="true" />
     
  33. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
  34. Kasi2302

    Kasi2302

    Joined:
    Sep 30, 2020
    Posts:
    25
    Thank you very much YASIRKULA :)

    It works :)

    But I don't understand it o_O

    Because so often I read add the lines to your AndroidManifest.xml

    Is it because I am using a newer Unity version or the latest Android API ?
     
    yasirkula likes this.
  35. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    I've removed that requirement in a previous update. Now, you mustn't do it :D
     
  36. Kasi2302

    Kasi2302

    Joined:
    Sep 30, 2020
    Posts:
    25
    Thank you very much YASIRKULA :)
     
    yasirkula likes this.
  37. Kasi2302

    Kasi2302

    Joined:
    Sep 30, 2020
    Posts:
    25
    Sorry, I have a question again. :(

    How can I fix this Problem with NativeCamera on Xcode ?

    Xcode_Screenshot1.jpg

    Xcode_Screenshot2.jpg
     
  38. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    @Kasi2302 You can either ignore those or change line 109 to
    return 1;
    and delete line 117. To persist these changes, you must make these modifications to Plugins/NativeCamera/iOS/NativeCamera.mm.
     
  39. Kasi2302

    Kasi2302

    Joined:
    Sep 30, 2020
    Posts:
    25
    @yasirkula Thank you very much I read already the post on stackoverflow you implement already the issue for ios 10+ but i'm not sure what can I delete, I'm new in xcode, its so very much complicated with apples modifications alway :(
     
  40. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Which parts got you confused?
     
  41. Kasi2302

    Kasi2302

    Joined:
    Sep 30, 2020
    Posts:
    25
    @yasirkula All parts on xcode :D:D:D i never programming on xcode before, i get my first apple developer account yesterday :D:D:D

    Android is much easier like apple ... :):):)

    now after i fix the last 2 errors , thank you very much again to you, i don't know how i can put my app in App store to test my app on diffrent devices :(:(:(
     
    yasirkula likes this.
  42. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Happy to help! BTW those 2 aren't errors, they're warnings. So if they come up again, you don't need to do anything since they're harmless.

    AFAIK, testing is possible in 3 different ways: TestFlight, installing to test devices from Xcode via USB cable, Ad Hoc distribution via 3rd party web sites.
     
  43. Kasi2302

    Kasi2302

    Joined:
    Sep 30, 2020
    Posts:
    25
    @yasirkula Thank you very very much yasirkula for your great work and for your help always. :):):)

    I watch a youtube tutorial already how to publish my app on app store.

    But you are the best !!! :):):)
     
    yasirkula likes this.
  44. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Thanks, you too!
     
  45. unity_V1WlANEebEQloQ

    unity_V1WlANEebEQloQ

    Joined:
    Jul 1, 2019
    Posts:
    9
    I am building my app but its gives me error.How can i solve that.
    upload_2024-1-2_13-14-2.png upload_2024-1-2_13-14-46.png
     

    Attached Files:

  46. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    @unity_V1WlANEebEQloQ You can delete the provider entry from your own AndroidManifest.xml. It's added automatically during build.
     
  47. unity_V1WlANEebEQloQ

    unity_V1WlANEebEQloQ

    Joined:
    Jul 1, 2019
    Posts:
    9
    Thank you so much, JazakAllah it worked. But now on my mobile camera app stuck when i click photo i don't go back to the app after selecting photo. WHat could be the reason for this.
     

    Attached Files:

  48. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Honestly, this is the very first time I'm hearing about this issue. Can you try a non-Development build, restarting the device, or testing on a different device?
     
  49. unity_V1WlANEebEQloQ

    unity_V1WlANEebEQloQ

    Joined:
    Jul 1, 2019
    Posts:
    9
    It worked for me after setting permission function in start.
    JazakAllah for making such cool stuff, you are making many developers lkife easier. May Allah bless you ease in your life and hereafter.
    Thanks again.
     
    yasirkula likes this.
  50. ktpttd

    ktpttd

    Joined:
    Aug 2, 2017
    Posts:
    12
    Thanks for amazing free asset.
    I got an error, after taking a picture. Stay at the confirmation screen without doing anything for about 2 minutes. Then it will automatically turn off that screen and return to the app.