Search Unity

Native Camera for Android & iOS [Open Source]

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

  1. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    ironbitUnity likes this.
  2. AdrianoAlemanno

    AdrianoAlemanno

    Joined:
    Nov 6, 2018
    Posts:
    6
    Hello there, first of all I want to thank you for this plugin.
    I'm just having an issue: it looks like the plugin works only in development mode, if the build is not in development I have no reactions. I tested only on android until now.
    I have the same issue also with your "Unity native gallery plugin", so I'm pretty sure I'm missing something, I'm a beginner in unity, but I honestly don't know what.
    I would really appreciate any kind of support. Thank you
     
  3. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    If you are using ProGuard, try adding the following line to ProGuard filters:
    -keep class com.yasirkula.unity.* { *; }
    . Otherwise, please debug your app with logcat to see if there are any error messages.
     
  4. AdrianoAlemanno

    AdrianoAlemanno

    Joined:
    Nov 6, 2018
    Posts:
    6
    It looks like the issue is somehow related to build app directly from unity.
    I tried to export the project and build the app using android Studio; I had to fix some issue but not directly related to your plugin and now everything works correctly whether I run or debug.

    Maybe it could be something related to pause unity engine because when I click on button to open camera I have this messages:
    W/Unity: Timeout while trying to pause the Unity Engine.
    D/ViewRootImpl@9fcf3c2[UnityPlayerActivity]: MSG_WINDOW_FOCUS_CHANGED 0
     
  5. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    The timeout message is expected with NativeCamera, so the issue should be something else. Do you see any other error messages after clicking the button to open the camera? Maybe something like "Content provider couldn't be found"?
     
  6. AdrianoAlemanno

    AdrianoAlemanno

    Joined:
    Nov 6, 2018
    Posts:
    6
    Ok, so this is the issue:
    12-17 14:58:02.854 25036 25091 E Unity : AndroidJavaException: java.lang.ClassNotFoundException: com.yasirkula.unity.NativeGalleryPermissionReceiver
    12-17 14:58:02.854 25036 25091 E Unity : java.lang.ClassNotFoundException: com.yasirkula.unity.NativeGalleryPermissionReceiver
    12-17 14:58:02.854 25036 25091 E Unity : at java.lang.Class.classForName(Native Method)
    12-17 14:58:02.854 25036 25091 E Unity : at java.lang.Class.forName(Class.java:453)
    12-17 14:58:02.854 25036 25091 E Unity : at java.lang.Class.forName(Class.java:378)
    12-17 14:58:02.854 25036 25091 E Unity : at com.unity3d.player.UnityPlayer.nativeRender(Native Method)
    12-17 14:58:02.854 25036 25091 E Unity : at com.unity3d.player.UnityPlayer.c(Unknown Source:0)
    12-17 14:58:02.854 25036 25091 E Unity : at com.unity3d.player.UnityPlayer$e$2.queueIdle(Unknown Source:72)
    12-17 14:58:02.854 25036 25091 E Unity : at android.os.MessageQueue.next(MessageQueue.java:394)
    12-17 14:58:02.854 25036 25091 E Unity : at android.os.Looper.loop(Looper.java:142)
    12-17 14:58:02.854 25036 25091 E Unity : at com.unity3d.player.UnityPlayer$e.run(Unknown Source:32)
    12-17 14:58:02.854 25036 25091 E Unity : Caused by: java.lang.ClassNotFoundException: Didn't find class "com.yasirkula.unity.NativeGalleryPermissionReceiver" on path: DexPathList[[zip file "/data/app/com.AVRSalento.ArGeo_test-xj1_3RRPZPfj9NWheJaC5Q==/base.apk"],nativeLibraryDirectories=[/data/app/com.AVRSalento.ArGeo_test-xj1_3RRPZPfj9NWheJaC5Q==/lib/arm, /data/app/com.AVRSalento.ArGeo

    It's the same error for "Native Gallery" and "Native Camera"
    Unity 2017.4.9f1(on macOS High Sierra)
    Build system: Gradle
    development build: false
    Testing device: Samsung Galaxy S8 (Android 8.0.0)
     
  7. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Is Proguard selected/enabled in Player Settings-Minify? If so, you need to add
    -keep class com.yasirkula.unity.* { *; }
    to the Proguard filters.
     
    RobBrooks likes this.
  8. AdrianoAlemanno

    AdrianoAlemanno

    Joined:
    Nov 6, 2018
    Posts:
    6
    It was because of Proguard. Sorry if I didn't check before but I had no idea what is it and where to find it.
    Thank you so much!
     
    yasirkula likes this.
  9. gciapponirealmore

    gciapponirealmore

    Joined:
    Sep 13, 2018
    Posts:
    5
    Hi, thank you for taking the time to develop and share this asset.

    I'm wondering if there is any way to have the front facing camera active when calling TakePicture(). It used to be possible in an earlier version using SetDefaultCamera(), but it looks like that's been removed.

    Thankyou !
     
  10. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    NativeCamera did not include SetDefaultCamera in earlier versions. Did you perhaps confuse it with another camera plugin?

    P.S. Currently, NativeCamera doesn't support opening the front facing camera by default.
     
  11. gciapponirealmore

    gciapponirealmore

    Joined:
    Sep 13, 2018
    Posts:
    5
    It definitely is your plugin, but since it is a legacy project I inherited, my guess is that it was probably modified by somebody long gone.
    Sorry for bothering you, then !
     
  12. joesziveri

    joesziveri

    Joined:
    Dec 18, 2018
    Posts:
    6
    How do we actually change the path? I can't seem to figure it out with the sample code. Also not sure whats wrong but after I take a picture the rectangle stays pink. No errors are thrown in debug, it runs smoothly, there is just a small pink rectangle that is displayed on the screen for about 3 seconds. And yes, I updated the Android Manifest
    Here is a screenshot of the debug when running the sample code on my phone
     
    Last edited: Dec 22, 2018
  13. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    @joesziveri Sorry to hear about the pink rectangle issue. I haven't encountered such an issue myself and I currently have no idea why it would happen. If you downloaded the asset from Asset Store, can you also try importing it from the GitHub repository and see if the issue persists?

    BTW, you can't change the path of the captured image. It is always saved at the same path; you should instead move it to the desired location in your code.
     
  14. joesziveri

    joesziveri

    Joined:
    Dec 18, 2018
    Posts:
    6
    Howdy, I did some researching and it seems the purple square had something to do with the shaders being applied in the sample code. Instead of using the temporary quad, I just added a UI Image and set the texture of that instead and it worked perfectly. Thanks for the quick reply!
     
    yasirkula likes this.
  15. DevionNL

    DevionNL

    Joined:
    Jun 29, 2018
    Posts:
    7
    Heya, we've hit a snag with using the nativecamera on iphone 8's - seems to work fine on 6 and 10's. Is there something that we're missing that needs to be applied?
     
  16. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Not that I know of. Are there any error messages or anything that could help me point out the issue?
     
  17. DevionNL

    DevionNL

    Joined:
    Jun 29, 2018
    Posts:
    7
    Nothing of the sort - one of testers with an Iphone 8 clicked to request the nativecamera - it flew past the method without opening the cam, as if ios ignored it - or didn't have permission. alas no more information found at this point.
     
  18. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Is it possible that they'd denied the camera permission at first attempt? Permission dialog is only shown once after the app is installed. If permission is denied, user has to give permission from the Settings app.
     
  19. DevionNL

    DevionNL

    Joined:
    Jun 29, 2018
    Posts:
    7
    I'll give that a shot - thanks!
     
  20. joeyBerger

    joeyBerger

    Joined:
    Jan 10, 2019
    Posts:
    2
    Great plugin- but I'm having trouble getting the the texture to look correct. I'm simply passing the texture to another function and setting a texture that is the max and width of the screen to the recently taken picture. The picture looks to be super zoomed in, and is a blocked color. Here is the code in that function:

    if (!Application.isEditor){screenImage.GetComponent<Image>().sprite = Sprite.Create(passedTexture, new Rect(0,0,passedTexture.width,passedTexture.height), new Vector2(0.5f, 0.5f));}
     
  21. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Maybe the Image component has a gray tint color and/or is bigger than the screen? Please make sure that the Image's Color property is opaque white and enable Preserve Aspect to see if anything changes. If that doesn't work, I don't really have any ideas because the texture comes from the native camera app and is not modified by NativeCamera.
     
  22. joeyBerger

    joeyBerger

    Joined:
    Jan 10, 2019
    Posts:
    2
    Thanks for the reply. The sprite that is inheriting the new texture is definitely not the problem. I seem to be unable to get the newly taken picture's texture to display properly. I'm using an iPhone 6s on 10.3.3- seen any problems with that configuration? Thanks!
     
  23. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Is it also the same when you assign the texture to a RawImage object, i.e. not use an Image component and sprites.
     
  24. novaVision

    novaVision

    Joined:
    Nov 9, 2014
    Posts:
    518
    Is there a way to get texture to apply to RawImage in UI element for preview?
     
  25. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    You can use the NativeCamera.LoadImageAtPath function to load the image into a Texture2D and then assign it to a RawImage. If you are asking about a live camera feed, then it is not possible.
     
  26. hansadler

    hansadler

    Joined:
    May 12, 2014
    Posts:
    50
    I got this asset to work fine on iOS, but on Android I get a crash after taking a second photo. The crash occurs right after the camera activity closes. Any idea how to fix this?
     
  27. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Are there any error messages in logcat? Can you also add some Debug.Log's to your code to pinpoint the line which causes the crash?
     
  28. hansadler

    hansadler

    Joined:
    May 12, 2014
    Posts:
    50
    Hello, I attached a logcat.
    This line was logged right before I called "02-16 10:30:57.101 14823 14873 V ABOT : OnTakePhotoClick"

    The following lines are logged from C# after taking the photo
    02-16 10:31:07.876 14823 14873 V ABOT : OnApplicationPause False
    02-16 10:31:07.879 14823 14873 V ABOT : pause_time 0.172646983333333
    02-16 10:31:07.908 14823 14873 V ABOT : Image path: /data/user/0/com.wingboat.AllowanceBot/cache/IMG_camera.jpg

    Then I see this line in the log
    02-16 10:31:11.281 776 6073 I ActivityManager: Process com.wingboat.AllowanceBot (pid 14823) has died: fore TOP

    Can you see anything in my log that would help me get past this problem?

    Btw, I'm not getting Debug.Log in the logcat output anymore. I had to add a native logging function to log the lines above. Do you know how to make Debug.Log output appear in logcat?

    Thank you for your help!
     

    Attached Files:

  29. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Honestly, I can't see anything useful in logcat. I've googled "has died: fore TOP" but couldn't find any useful information there, either. It might help if you can find the problematic line by inserting Debug.Log's to every single line. I can always see Debug.Log messages if I use Android Studio's logcat window or launch "adb logcat -s Unity" command in command line (cmd).
     
  30. wangyucheng1992

    wangyucheng1992

    Joined:
    Feb 19, 2019
    Posts:
    59
    hello, I have a problem: When I open the camera(IOS) but want to go back so i clicked candel but the callback(CameraCallBack()) will called once Each frame (path=null) and we can not do anything in APP,
    The device is iphoneXR,Do you have any good ideas? Thank you vary much!



    upload_2019-2-19_15-45-2.png
     
  31. wangyucheng1992

    wangyucheng1992

    Joined:
    Feb 19, 2019
    Posts:
    59
    thank you!I have solved the problem
     
    yasirkula likes this.
  32. rahulkainthola

    rahulkainthola

    Joined:
    Feb 20, 2019
    Posts:
    3
    hello , i need help with native camera pulgin want to store clicked image directly in my phone gallery not in temp cache.can anyone help me with this ?
     
  33. yasirkula

    yasirkula

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

    rahulkainthola

    Joined:
    Feb 20, 2019
    Posts:
    3
    hey, @yasirkula thanks for instant reply and suggestion. i am using NativeGallery.SaveImageToGallery like this with native camera script but... getting the following error in android console.


    ArgumentException: Texture '' is not readable, the texture memory can not be accessed from scripts. You can make the texture readable in the Texture Import Settings.
    at (wrapper managed-to-native) UnityEngine.ImageConversion.EncodeToPNG(UnityEngine.Texture2D)
    at NativeGallery.GetTextureBytes (UnityEngine.Texture2D texture, System.Boolean isJpeg) [0x00002] in D:\Unity pro\Puzzle Game\Assets\Plugins\NativeGallery\NativeGallery.cs:452
    at NativeGallery.SaveImageToGallery (UnityEngine.Texture2D image, System.String album, System.String filenameFormatted, NativeGallery+MediaSaveCallback callback) [0x00058] in D:\Unity pro\Puzzle Game\Assets\Plugins\NativeGallery\NativeGallery.cs:234

    with this code :-

    Code (CSharp):
    1.  private void TakePicture(int maxSize)
    2.     {
    3.         NativeCamera.Permission permission = NativeCamera.TakePicture((path) =>
    4.         {
    5.             Debug.Log("Image path: " + path);
    6.  
    7.             if (path != null)
    8.             {
    9.                 Texture2D texture = NativeCamera.LoadImageAtPath(path, maxSize);
    10.                 NativeGallery.SaveImageToGallery(texture, "GalleryTest", "My img {0}.png");
    11.     }
    12.         }, maxSize);
    13.  
    14.         Debug.Log("Permission result: " + permission);
    15.     }
     
  35. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Try replacing lines 9 and 10 with this:

    NativeGallery.SaveImageToGallery(path, "GalleryTest", "My img {0}.png");
     
    rahulkainthola likes this.
  36. rahulkainthola

    rahulkainthola

    Joined:
    Feb 20, 2019
    Posts:
    3
    hey, @yasirkula thanks man, replacing path did the job. Thanks again
     
    yasirkula likes this.
  37. zelmax

    zelmax

    Joined:
    Mar 16, 2018
    Posts:
    10
    Hey bro @yasirkula im having an issue with the camera, when the native camera lounches if i dont take a picture and if i press the "Back button" of my phone the app crashes, how can i configure that button? i would like to code it if i press the back button reset the scene
     
  38. LS0

    LS0

    Joined:
    Mar 8, 2019
    Posts:
    3
    Having issues recording a video on Android.
    I copied the sample code from the GitHub readme, but the logs just show:

    Code (csharp):
    1. Permission result: Granted
    2. Image path:
    3. Permission result: Granted
    4. Video path:
     
  39. Toni_Doublet

    Toni_Doublet

    Joined:
    Jan 18, 2013
    Posts:
    6
    Hey, thanks for your plugin, it's awesome!
    I'd like to know if there is a way to select either video or picture from the native camera application?
    Currently, I only seem to be able to select Picture or Video before launching the native camera application.
     
  40. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    @zelmax Pressing back button isn't supposed to crash the app. Does the app crash entirely and not restart afterwards or does it simply restart after pressing the back button? If it is the latter, then it might be a low memory (RAM) issue. On which Android/iOS version does this issue occur? Is this behaviour persistent across different devices/emulators?

    @unity_cTq6CR9L2kwxAw Honestly, this is the first time I see this issue. Assuming that your device has a camera and a camera application (Android's preinstalled Camera app works), this is unexpected. Can you replicate the issue on other devices?

    @Toni_Doublet Maybe NativeGallery will work for you?
     
  41. zelmax

    zelmax

    Joined:
    Mar 16, 2018
    Posts:
    10
    @yasirkula I mean , when i start the camera and I don't take a picture, simply press the back button of my phone , the camera close and my raw image get blank / white, I would like that, when I press the back button of my phone the scene restart it's possible? I'm using Android 5.1.1
     
  42. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Inside the callback function of NativeCamera, you can check if string.IsNullOrEmpty(path) is true. If so, you can restart the scene via SceneManager.LoadScene.
     
  43. zelmax

    zelmax

    Joined:
    Mar 16, 2018
    Posts:
    10
    Thanks a lot bro , it works
     
  44. Toni_Doublet

    Toni_Doublet

    Joined:
    Jan 18, 2013
    Posts:
    6
    @yasirkula, unfortunately the NativeGallery does not help for this... My issue is that I can't switch with from picture to video with the NativeCamera plugin at record time.
    I attached a screenshot that shows my native camera application switch system.
    Thanks a lot!
     

    Attached Files:

  45. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Why would you want to switch between photo and video? There are separate functions for each functionality (TakePicture and RecordVideo) and in general, you would need the user to either take a picture or record a video for a specific task.
     
  46. LS0

    LS0

    Joined:
    Mar 8, 2019
    Posts:
    3
    @yasirkula, is there anything special I need to have done with permissions? I'm pretty sure I followed your setup instructions fully, but when I press the record side of the screen, no permissions dialog comes up yet the logs say permission granted and then returns a null path

    The same issue is on both my android devices, which have perfectly working cameras otherwise.

    (ps i was @unity_cTq6CR9L2kwxAw)
     
  47. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Does this issue also occur on a fresh new project with the example code? I can't think of any reasons for this behaviour.
     
  48. hitarthdoc1994

    hitarthdoc1994

    Joined:
    Dec 17, 2015
    Posts:
    15
    Hey @yasirkula,
    I have used your package in my project. An excellent and needed utility.
    But I do have a concern/question that needs your attention.
    The game crashes after I use the Native Camera TakePicture and LoadImageAtPath a few times.
    I am assuming Android closes the game for some memory constraint.
    Do I need to call
    Code (CSharp):
    1. Resources.UnloadUnusedAssets ()
    after loading image from path?
    Or is there a different way to prevent Android from closing the game in the background while taking a picture.
    Thanks in advance and thank you again for keeping the plugin Open Source.
    Regards,
    Hitarth
     
  49. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    If you don't specify a sufficiently small maxSize in LoadImageAtPath, created textures may consume a lot of memory because they are not compressed in any way. And if you keep creating new textures without destroying the previous unused ones (via Destroy), then these textures can quickly fill up the entire free memory on the mobile device and lead to a crash.
     
  50. hitarthdoc1994

    hitarthdoc1994

    Joined:
    Dec 17, 2015
    Posts:
    15
    I replace references of the images that are loaded. So I expect the GC to collect and free memory.

    The images loaded are not even kept as a reference in the scene.

    Here is the code for that.

    Code (CSharp):
    1. //First Click image and store image path
    2. public void Clickselfie ( int nextState = (int)UIStates.ConfirmSelfie )
    3. {
    4.     this.nextState = (UIStates)nextState;
    5.     NativeCamera.TakePicture ( ReceiveSelfiePath );
    6. }
    7.  
    And then later.

    Code (CSharp):
    1. public void LoadNewImageForEditing ()
    2. {
    3.     Destroy ( photo );
    4.     photo = NativeCamera.LoadImageAtPath ( PlayerPrefs.GetString ( "selfiePath" ), markTextureNonReadable: false, generateMipmaps: false );
    5.     float aspect = photo.width / (float)photo.height;
    6.  
    7.     TextureScale.Bilinear ( photo, Mathf.FloorToInt ( 1280 * aspect ), 1280 );
    8.     photo.Apply ();
    9.  
    10.     photoImage.sprite = Sprite.Create ( photo, new Rect ( 0, 0, photo.width, photo.height ), Vector2.one * 0.5f );
    11.     photoImage.preserveAspect = false;
    12.     photoImage.rectTransform.sizeDelta = new Vector2 ( Mathf.FloorToInt ( 1024 * aspect ), 1024 );
    13.     photoImage.rectTransform.anchoredPosition = Vector2.zero;
    14.     result = new Texture2D ( result.width, result.height, TextureFormat.RGB24, false );
    15.  
    16.     Resources.UnloadUnusedAssets ();
    17. }
    My concern is that while I am taking a picture, the game gets a soft close by the OS.
    I have checked the log a few times but i don't get an error from the app about a close.