Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Native Camera for Android & iOS [Open Source]

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

  1. TheZhe

    TheZhe

    Joined:
    May 12, 2020
    Posts:
    4
    Hello @yasirkula,
    I'm running into a minor issue. If I record a video and my gallery contains no videos, then the video saves to my android gallery under the "camera" album and not the temporaryCachePath. Otherwise, RecordVideo() works normally.
     
  2. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,886
    Can you check Logcat to see if an error message is logged there?
     
  3. TheZhe

    TheZhe

    Joined:
    May 12, 2020
    Posts:
    4
    Here are my logs. The highlighted one shows the video saving to my DCIM folder.
     

    Attached Files:

  4. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,886
  5. TheZhe

    TheZhe

    Joined:
    May 12, 2020
    Posts:
    4
    My apologies. I'm still an android newbie. Here are all the errors since I started up my app. The gallery was initially empty. Then, I captured a video and checked the gallery twice.
     

    Attached Files:

  6. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,886
    Can you try replacing Plugins/NativeCamera/Android/NativeCamera.aar with the one inside the attached zip archive?
     

    Attached Files:

  7. TheZhe

    TheZhe

    Joined:
    May 12, 2020
    Posts:
    4
    It works now. Thank you so much!
     
  8. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,886
    Glad to hear it!
     
  9. Sadzikk

    Sadzikk

    Joined:
    Dec 8, 2016
    Posts:
    7
    Hello, overall love the product, works great, but I'd like to allow users to take several photos in a row and when it comes to 8th or 9th photo the app starts to slow down, on the log cat i see skipped frames and warning that too much work is put on a single core. Any tips to handle making several photos with this plugin?

    Warnings starts to popup when I'm calling the TakePicture method, I pretty much used ur example code.
     
  10. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,886
    AFAIK, calling NativeCamera functions consecutively shouldn't be a problem. If you generate Textures along the way, perhaps the memory consumed by those Textures is causing problems? You can try commenting out the Texture generation part in your code and see if it makes and difference. I'd also recommend you to use Profiler with Development Build to see if it helps pinpoint the issue.
     
  11. salman2135

    salman2135

    Joined:
    May 31, 2018
    Posts:
    1
    Hello, I have the following script that executes on Button click but the camera app is not launching. Thanks a lot in advance! I love your other plugins too:)
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5.  
    6. public class screen : MonoBehaviour
    7. {
    8. private bool isSharing = false;
    9.  
    10. public void ShareSocialMedia()
    11. {
    12.     isSharing = true;
    13. }
    14.  
    15. void LateUpdate()
    16. {
    17.     if (isSharing == true)
    18.     {
    19.         isSharing = false;
    20.  
    21.         StartCoroutine(TakePicture(512));
    22.     }
    23. }
    24.  
    25. IEnumerator TakePicture( int maxSize )
    26. {
    27.     yield return new WaitForEndOfFrame();
    28.     NativeCamera.RequestPermission();
    29.     NativeCamera.Permission permission = NativeCamera.TakePicture( ( path ) =>
    30.     {
    31.         Debug.Log( "Image path: " + path );
    32.         if( path != null )
    33.         {
    34.             // Create a Texture2D from the captured image
    35.             Texture2D texture = NativeCamera.LoadImageAtPath( path, maxSize );
    36.             if( texture == null )
    37.             {
    38.                 Debug.Log( "Couldn't load texture from " + path );
    39.                 return;
    40.             }
    41.  
    42.             // Assign texture to a temporary quad and destroy it after 5 seconds
    43.             GameObject quad = GameObject.CreatePrimitive( PrimitiveType.Quad );
    44.             quad.transform.position = Camera.main.transform.position + Camera.main.transform.forward * 2.5f;
    45.             quad.transform.forward = Camera.main.transform.forward;
    46.             quad.transform.localScale = new Vector3( 1f, texture.height / (float) texture.width, 1f );
    47.          
    48.             Material material = quad.GetComponent<Renderer>().material;
    49.             if( !material.shader.isSupported ) // happens when Standard shader is not included in the build
    50.                 material.shader = Shader.Find( "Legacy Shaders/Diffuse" );
    51.  
    52.             material.mainTexture = texture;
    53.              
    54.             Destroy( quad, 5f );
    55.  
    56.             // If a procedural texture is not destroyed manually,
    57.             // it will only be freed after a scene change
    58.             Destroy( texture, 5f );
    59.         }
    60.     }, maxSize );
    61.  
    62.     Debug.Log( "Permission result: " + permission );
    63. }
    64. }
     
  12. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,886
    Can you see any meaningful error messages in logcat?
     
  13. ryanclark_unity

    ryanclark_unity

    Joined:
    Apr 22, 2019
    Posts:
    3
    I'm running into the same issue Immerse_Enterprise experienced back in December where it's always returning Permission.Denied after initially asking for permission on Google Pixel device.

    Phone: Pixel 4a
    Android: 10
    Unity: 2019.1.14f1

    Code (CSharp):
    1.  
    2. Debug.LogError("Before NativeCamera.CheckPermission");
    3. NativeCamera.Permission cameraPermission = NativeCamera.CheckPermission();
    4. Debug.LogError("After NativeCamera.CheckPermission");
    5. Debug.LogError($"cameraPermission = {cameraPermission}");
    Exception in Logcat:

    Code (CSharp):
    1.  
    2. 06-03 09:13:32.393 30710 30762 E Unity   : Before NativeCamera.CheckPermission
    3. 06-03 09:13:32.393 30710 30762 E Unity   : UnityEngine.Logger:Log(LogType, Object)
    4. 06-03 09:13:32.393 30710 30762 E Unity   : uFighter_Core.UI.UFaceComponent:SelectImage(Int32)
    5. 06-03 09:13:32.393 30710 30762 E Unity   : uFighter_Core.UI.UFaceComponent:HandleTutorialClose(Int32)
    6. 06-03 09:13:32.393 30710 30762 E Unity   : System.Action`1:Invoke(T)
    7. 06-03 09:13:32.393 30710 30762 E Unity   : UnityEngine.Events.UnityAction:Invoke()
    8. 06-03 09:13:32.393 30710 30762 E Unity   : UnityEngine.Events.UnityEvent:Invoke()
    9. 06-03 09:13:32.393 30710 30762 E Unity   : UnityEngine.EventSystems.EventFunction`1:Invoke(T1, BaseEventData)
    10. 06-03 09:13:32.393 30710 30762 E Unity   : UnityEngine.EventSystems.ExecuteEvents:Execute(GameObject, BaseEventData, EventFunction`1)
    11. 06-03 09:13:32.393 30710 30762 E Unity   : UnityEngine.EventSystems.StandaloneInputModule:ProcessTouchPress(PointerEventData, Boolean, Boolean)
    12. 06-03 09:13:32.393 30710 30762 E Unity   : UnityEngine.EventSystems.StandaloneInputModule:ProcessTouchEvents()
    13. 06-03 09:13:32.393 30710 30762 E Unity   : UnityEngine.EventSystems.StandaloneInputModule:Process()
    14. 06-03 09:13:32.393 30710 30762 E Unity   :
    15. 06-03 09:13:32.393 30710 30762 E Unity   : (Filename: ./Runtime/Export/Debug/Debug.bindings.h Line: 48)
    16. 06-03 09:13:32.393 30710 30762 E Unity   :
    17. 06-03 09:13:32.393 30710 30762 D Unity   : > NewStringUTF()
    18. 06-03 09:13:32.393 30710 30762 D Unity   : > ExceptionOccurred()
    19. 06-03 09:13:32.393 30710 30762 D Unity   : > NewStringUTF()
    20. 06-03 09:13:32.393 30710 30762 D Unity   : > ExceptionOccurred()
    21. 06-03 09:13:32.393 30710 30762 D Unity   : > CallStaticObjectMethod(0x332a, 0xd15697d0
    22. 06-03 09:13:32.393 30710 30762 D Unity   : ? getMethodID("com.unity3d.player.UnityPlayerActivity", "getClass", "()Ljava/lang/Object;", non-static)
    23. 06-03 09:13:32.393 30710 30762 D Unity   : ! Class Object.getClass();
    24. 06-03 09:13:32.393 30710 30762 D Unity   : > ExceptionOccurred()
    25. 06-03 09:13:32.393 30710 30762 D Unity   : > DeleteLocalRef()
    26. 06-03 09:13:32.393 30710 30762 D Unity   : > DeleteLocalRef()
    27. 06-03 09:13:32.393 30710 30762 D Unity   : > FromReflectedMethod()
    28. 06-03 09:13:32.393 30710 30762 D Unity   : > ExceptionOccurred()
    29. 06-03 09:13:32.393 30710 30762 D Unity   : > DeleteLocalRef()
    30. 06-03 09:13:32.393 30710 30762 D Unity   : > CallObjectMethod(0x36ca, 0x70813558)
    31. 06-03 09:13:32.393 30710 30762 D Unity   : > ExceptionOccurred()
    32. 06-03 09:13:32.393 30710 30762 D Unity   : > GetObjectClass()
    33. 06-03 09:13:32.394 30710 30762 D Unity   : > ExceptionOccurred()
    34. 06-03 09:13:32.394 30710 30762 D Unity   : > NewGlobalRef()
    35. 06-03 09:13:32.394 30710 30762 D Unity   : > NewGlobalRef()
    36. 06-03 09:13:32.394 30710 30762 D Unity   : > DeleteLocalRef()
    37. 06-03 09:13:32.394 30710 30762 D Unity   : > DeleteLocalRef()
    38. 06-03 09:13:32.394 30710 30762 D Unity   : > NewStringUTF()
    39. 06-03 09:13:32.394 30710 30762 D Unity   : > ExceptionOccurred()
    40. 06-03 09:13:32.394 30710 30762 D Unity   : > NewStringUTF()
    41. 06-03 09:13:32.394 30710 30762 D Unity   : > ExceptionOccurred()
    42. 06-03 09:13:32.394 30710 30762 D Unity   : > CallStaticObjectMethod(0x332a, 0xd15697d0
    43. 06-03 09:13:32.394 30710 30762 D Unity   : ? getMethodID("java.lang.Class", "getName", "()Ljava/lang/String;", non-static)
    44. 06-03 09:13:32.394 30710 30762 D Unity   : ! String Class.getName();
    45. 06-03 09:13:32.394 30710 30762 D Unity   : > ExceptionOccurred()
    46. 06-03 09:13:32.394 30710 30762 D Unity   : > DeleteLocalRef()
    47. 06-03 09:13:32.394 30710 30762 D Unity   : > DeleteLocalRef()
    48. 06-03 09:13:32.394 30710 30762 D Unity   : > FromReflectedMethod()
    49. 06-03 09:13:32.394 30710 30762 D Unity   : > ExceptionOccurred()
    50. 06-03 09:13:32.394 30710 30762 D Unity   : > DeleteLocalRef()
    51. 06-03 09:13:32.394 30710 30762 D Unity   : > CallStringMethod(0x380a, 0x7081c928)
    52. 06-03 09:13:32.394 30710 30762 D Unity   : > ExceptionOccurred()
    53. 06-03 09:13:32.394 30710 30762 D Unity   : > DeleteGlobalRef()
    54. 06-03 09:13:32.394 30710 30762 D Unity   : > DeleteGlobalRef()
    55. 06-03 09:13:32.394 30710 30762 D Unity   : > NewStringUTF()
    56. 06-03 09:13:32.394 30710 30762 D Unity   : > ExceptionOccurred()
    57. 06-03 09:13:32.394 30710 30762 D Unity   : > NewStringUTF()
    58. 06-03 09:13:32.394 30710 30762 D Unity   : > ExceptionOccurred()
    59. 06-03 09:13:32.394 30710 30762 D Unity   : > CallStaticObjectMethod(0x332a, 0xd15697d0
    60. 06-03 09:13:32.394 30710 30762 D Unity   : ? getMethodID("com.yasirkula.unity.NativeCamera", "CheckPermission", "(Lcom.unity3d.player.UnityPlayerActivity;)I", static)
    61. 06-03 09:13:32.394 30710 30762 D Unity   : ! int NativeCamera.CheckPermission(Context);
    62. 06-03 09:13:32.394 30710 30762 D Unity   : > ExceptionOccurred()
    63. 06-03 09:13:32.394 30710 30762 D Unity   : > DeleteLocalRef()
    64. 06-03 09:13:32.394 30710 30762 D Unity   : > DeleteLocalRef()
    65. 06-03 09:13:32.394 30710 30762 D Unity   : > FromReflectedMethod()
    66. 06-03 09:13:32.394 30710 30762 D Unity   : > ExceptionOccurred()
    67. 06-03 09:13:32.394 30710 30762 D Unity   : > DeleteLocalRef()
    68. 06-03 09:13:32.394 30710 30762 D Unity   : > CallStaticIntMethod(0x36d6, 0xe78c74d8
    69. 06-03 09:13:32.395 30710 30762 D Unity   : > ExceptionOccurred()
    70. 06-03 09:13:32.397 30710 30762 E Unity   : After NativeCamera.CheckPermission
    71. 06-03 09:13:32.397 30710 30762 E Unity   : UnityEngine.Logger:Log(LogType, Object)
    72. 06-03 09:13:32.397 30710 30762 E Unity   : uFighter_Core.UI.UFaceComponent:SelectImage(Int32)
    73. 06-03 09:13:32.397 30710 30762 E Unity   : uFighter_Core.UI.UFaceComponent:HandleTutorialClose(Int32)
    74. 06-03 09:13:32.397 30710 30762 E Unity   : System.Action`1:Invoke(T)
    75. 06-03 09:13:32.397 30710 30762 E Unity   : UnityEngine.Events.UnityAction:Invoke()
    76. 06-03 09:13:32.397 30710 30762 E Unity   : UnityEngine.Events.UnityEvent:Invoke()
    77. 06-03 09:13:32.397 30710 30762 E Unity   : UnityEngine.EventSystems.EventFunction`1:Invoke(T1, BaseEventData)
    78. 06-03 09:13:32.397 30710 30762 E Unity   : UnityEngine.EventSystems.ExecuteEvents:Execute(GameObject, BaseEventData, EventFunction`1)
    79. 06-03 09:13:32.397 30710 30762 E Unity   : UnityEngine.EventSystems.StandaloneInputModule:ProcessTouchPress(PointerEventData, Boolean, Boolean)
    80. 06-03 09:13:32.397 30710 30762 E Unity   : UnityEngine.EventSystems.StandaloneInputModule:ProcessTouchEvents()
    81. 06-03 09:13:32.397 30710 30762 E Unity   : UnityEngine.EventSystems.StandaloneInputModule:Process()
    82. 06-03 09:13:32.397 30710 30762 E Unity   :
    83. 06-03 09:13:32.397 30710 30762 E Unity   : (Filename: ./Runtime/Export/Debug/Debug.bindings.h Line: 48)
    84. 06-03 09:13:32.397 30710 30762 E Unity   :
    85. 06-03 09:13:32.400 30710 30762 E Unity   : cameraPermission = Denied
    86.  
     
  14. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,886
    Can you try disabling Minification and Managed Stripping Level?
     
  15. ryanclark_unity

    ryanclark_unity

    Joined:
    Apr 22, 2019
    Posts:
    3
    I've disabled stripping (was previously low) and have no minification enabled but am experiencing the same issue :(

    I've also tried the updated NativeCamera.arr from the post on May 16th and have the issue.
     
  16. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,886
  17. ryanclark_unity

    ryanclark_unity

    Joined:
    Apr 22, 2019
    Posts:
    3
    I've tried putting the tools tags in for storage and camera permissions but it seems to be something else in the manifest which I haven't narrowed down yet.

    Putting Native Camera into a fresh project shows it's working just fine on my Pixel 4. There seems to be a conflict in the game project I'm working on though. I'll post what the issue was that I ran into when I find it!

    -- Update --
    Turns out it is fixed by adding a replace tag to a WRITE_EXTERNAL_STORAGE permission. The generated manifest had a maxApiVersion entry from another plugin that when removed fixes Native Camera. Thanks @yasirkula !

    Code (csharp):
    1.  
    2. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" tools:node="replace" />
    3.  
     
    Last edited: Jun 3, 2020
    yasirkula likes this.
  18. arnewo36

    arnewo36

    Joined:
    Jun 4, 2019
    Posts:
    2
    Hello,

    I have a problem initializing NativeCamera. I downloaded the NativeCamera.unitypackage from Github and installed it to the regular way. The Plugin is in Assets/Plugins/NativeCamera.

    I implemented a C# script based on the example provided on Github ( TakePicture(int maxSize) method).
    But Unity throws

    error CS0246: The type or namespace name 'NativeCamera' could not be found (are you missing a using directive or an assembly reference?)
    error CS0103: The name 'NativeCamera' does not exist in the current context


    When I move NativeCarmea.cs from the Plugin-Folder to my Script folder, it's fine in Editor but when I try to build a Android-Build, I get

    error CS0246: The type or namespace name 'NativeCameraNamespace' could not be found (are you missing a using directive or an assembly reference?)

    I use Unity 2019.4.3f1 on Win10 with VS Community 2019 v16.6.3.

    Is there something I forgot to do ?
     
  19. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,886
    You are probably using Assembly Definition Files. You should add NativeCamera.Runtime.asmdef to your own Assembly Definition File's Referenced Assemblies list.
     
    arnewo36 likes this.
  20. arnewo36

    arnewo36

    Joined:
    Jun 4, 2019
    Posts:
    2
    :oops: Thank you, yasirkula, works now !
     
    yasirkula likes this.
  21. thaison

    thaison

    Joined:
    Jul 21, 2014
    Posts:
    3
    thanks for your great asset, work well on my phone. I wonder is it possible to add some image object in the camera preview image before taking the picture.
     
  22. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,886
    Unfortunately not, customizing the camera UI/preview/feed isn't possible with NativeCamera.
     
  23. Lancevn

    Lancevn

    Joined:
    Dec 22, 2018
    Posts:
    3
    This plugin is a great find and the support you provide is impressive! Thank you. I am needing to synchronize recording video along with gyro and timecode data. This plugin is working great to record video and I believe you are catching the Stop Recording event of the native recorder to return control back to the user. Would it be possible to add a Start Recording unity event so we could have a couple listeners get a callback? That is a slightly different design pattern than you use generally so a single async callback would also work. Any consideration or alternative ideas are welcome. I don't see many feature requests in this forum so I hope this isn't bad form. I just think this request is consistent with the overall scope and would really help my case.
     
  24. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,886
    When RecordVideo is called, NativeCamera launches the device's default camera app on Android and uses UIImagePickerController on iOS. Unfortunately, neither of these solutions have any StartedRecording/StoppedRecording callbacks. They return a result to NativeCamera only after user stops recording and confirms to send the recorded video back to Unity.
     
  25. Lancevn

    Lancevn

    Joined:
    Dec 22, 2018
    Posts:
    3
    Wow that was fast. Thanks! Yes those APIs are limited. Well there might be a way to get a reasonable but not frame accurate approximation by watching the cache folder changes.
     
  26. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,886
    For the best results, you may want to use a plugin that renders the native camera feed inside Unity (i.e. handles the camera stuff in managed C# at runtime), instead of using the device's native camera app.
     
  27. MrFigment

    MrFigment

    Joined:
    Jan 5, 2017
    Posts:
    27
    Is there a way to use the camera but without leaving the application (without having to access the camera application and have scaling problems)? something like using raw image directly?
    upload_2020-9-2_19-14-17.png
     
  28. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,886
    Unfortunately NativeCamera doesn't have this feature :/
     
  29. rogueyoshi

    rogueyoshi

    Joined:
    Sep 8, 2018
    Posts:
    13
    Should this plugin have any compatibility issues with ARFoundation? Sometimes our ARSession gets completely wiped after returning from NativeCamera.TakePicture(), but not always (mostly on iOS). We believe this is due to a race condition where ARSession is not getting the camera device back from NativeCamera.

    Also, should NativeCamera.IsCameraBusy() always be returning false on Android, even when the camera is in use by the Asset itself? We were going to attempt to pause the ARSession (ARSession.enabled = false) and resume when the camera is available again, but it appears IsCameraBusy doesn't actually poll the state on Android (as a matter of fact, I should ask, how does this work on iOS exactly)?

    We're going to attempt to delay unpausing the ARSession by a few frames (or seconds) and seeing it that remedies it, but we would like for an API to see if the camera resource is available.
     
  30. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,886
    IsCameraBusy() is only implemented in iOS, you are right. On iOS, it returns true while NativeCamera's camera window (UIImagePickerController, technically) is still visible. It doesn't really check if any other app/component is using the camera.

    I think the idea of delaying ARSession by a few frames or seconds sounds promising. Since IsCameraBusy won't be useful in your case, you can start that delay inside the CameraCallback function.
     
  31. rogueyoshi

    rogueyoshi

    Joined:
    Sep 8, 2018
    Posts:
    13
    Can you add APIs to check if the device resource is free? About to test a build with a delay.
     
  32. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,886
    I might but it is currently not a priority for me.
     
  33. Camad

    Camad

    Joined:
    May 19, 2019
    Posts:
    5
    Hello there! Thank you for your plugin!
    Tell me how I can rotate the resulting image 90 degrees to the right so that it is vertical.
    Please give me the code if you can
     
  34. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,886
    You can't rotate an image with NativeCamera but you can use LoadImageAtPath to create a Texture2D from an image in correct orientation.
     
  35. Camad

    Camad

    Joined:
    May 19, 2019
    Posts:
    5
    Code (CSharp):
    1.  public void TakePicture(int maxSize)
    2.     {
    3. NativeCamera.Permission permission = NativeCamera.TakePicture((path) =>
    4.         {
    5. if (path != null)
    6.             {
    7. NativeGallery.GetImageProperties(path);
    8. Texture2D texture = NativeCamera.LoadImageAtPath(path, maxSize);
    9. if (texture == null)
    10.                 {
    11. Debug.Log("Couldn't load texture from " + path);
    12. return;
    13.                 }
    14. Main.M.getImgProduct(path);
    15.             }
    16. }, maxSize);
    17. }
    18. public void getImgProduct(string path)
    19.     {
    20. Sprite sp = LoadNewSprite(path);
    21. loadImg.GetComponent<Image>().sprite = sp;
    22. productChangeParent.transform.GetChild(2).GetComponent<Image>().sprite = sp;
    23. imgPath = path;
    24.     }
    25.  
    26. public static Sprite LoadNewSprite(string FilePath, float PixelsPerUnit = 100.0f, SpriteMeshType spriteType = SpriteMeshType.FullRect)
    27.     {
    28. Texture2D SpriteTexture = LoadTexture(FilePath);
    29. Sprite NewSprite = Sprite.Create(SpriteTexture, new Rect(0, 0, SpriteTexture.width, SpriteTexture.height), new Vector2(0, 0), PixelsPerUnit, 0, spriteType);
    30.  
    31. return NewSprite;
    32.     }
    33. public static Texture2D LoadTexture(string FilePath)
    34.     {
    35. Texture2D Tex2D;
    36. byte[] FileData;
    37.  
    38. if (File.Exists(FilePath))
    39.         {
    40. FileData = File.ReadAllBytes(FilePath);
    41. Tex2D = new Texture2D(2, 2); // Create new "empty" texture
    42. if (Tex2D.LoadImage(FileData)) // Load the imagedata into the texture (size is set automatically)
    43. return Tex2D; // If data = readable -> return texture
    44.         }
    45. return null; // Return null if load failed
    46.     }
    47.  
    that's what I use, here's my code.
    Thank you for your quick response!


    P.S.
    Oh, right. I don't use your function...
     
    Last edited: Sep 23, 2020
  36. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,886
    Even though you state that you don't use LoadImageAtPath, I can see it in your code. I don't understand, does it not load the image in correct orientation?

    P.S. Nevermind, just saw that your main code is using Texture2D.LoadImage instead of LoadImageAtPath.
     
  37. rogueyoshi

    rogueyoshi

    Joined:
    Sep 8, 2018
    Posts:
    13
    @Camad you should look into yasir's other plugin, ImageCropper. It has free rotation.

    @yasirkula we're having some serious issues on iOS and AR Foundation. NativeCamera takes a long time to start coming from an ARSession (ARKit throws a camera-access error, before NativeCamera eventually gets camera access).
     
  38. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,886
    I haven't tried the plugin in an AR project. Perhaps NativeCamera and ARSession are both trying to access the camera at the same time. Can you manually disable the AR camera before using NativeCamera so that the camera is freed?
     
  39. thetomatodev

    thetomatodev

    Joined:
    Apr 20, 2020
    Posts:
    11
    I have been using this plugin without the WRITE_EXTERNAL_STORAGE before but now it doesn't work without that tag. Has there been any change related to this?
     
  40. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,886
  41. unity_wFR_NKY5Vs9DsA

    unity_wFR_NKY5Vs9DsA

    Joined:
    Sep 8, 2020
    Posts:
    2
    Hello! Love this asset! I was wondering if there was a way to determine the length of the video the user created after they are done?
     
  42. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,886
    You can use
    NativeCamera.GetVideoProperties(videoPath).duration
    to get the video duration in milliseconds.
     
  43. Mann94

    Mann94

    Joined:
    Sep 18, 2015
    Posts:
    4
    Hello,
    can I use this to access the Time-of-Flight camera of my Samsung Galaxy S20+ ?
    In Android Studio I do this to get the id of the ToF sensor:

    Code (CSharp):
    1.    private String getBackDepthCameraID() {
    2.         try {
    3.             for (String camera : cameraManager.getCameraIdList()) {
    4.                 CameraCharacteristics chars = cameraManager.getCameraCharacteristics(camera);
    5.                 final int[] capabilities = chars.get(CameraCharacteristics.REQUEST_AVAILABLE_CAPABILITIES);
    6.                 boolean facingBack = chars.get(CameraCharacteristics.LENS_FACING) == CameraMetadata.LENS_FACING_BACK;
    7.                 boolean depthCapable = false;
    8.                 for (int capability : capabilities) {
    9.                     boolean capable = capability == CameraMetadata.REQUEST_AVAILABLE_CAPABILITIES_DEPTH_OUTPUT;
    10.   /*************/
    11.     }
     
  44. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,886
    I'm not sure. I call Android's standard ACTION_VIDEO_CAPTURE and ACTION_IMAGE_CAPTURE intents and don't know what will happen in your case.
     
  45. Butti10

    Butti10

    Joined:
    Nov 17, 2020
    Posts:
    3
    Boa tarde amigo parabéns pelo trabalho, bom eu sou novo aqui como eu poderia colocar este plugin em um botão para tirar foto? Se puder me dizer quais scripts tenho que anexar a camera ou ao canvas, agradeço desde ja
     
  46. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,886
    You can make the TakePicture function in the example code public and assign it to a button.
     
  47. Butti10

    Butti10

    Joined:
    Nov 17, 2020
    Posts:
    3
    [QUOTE = "yasirkula, post: 6532547, membro: 56169"] Você pode tornar a função TakePicture no código de exemplo pública e atribuí-la a um botão. [/ QUOTE]
    AMIGO OBRIGADO, você teria uma dica preciso que capturar um objeto com essa camera, pensei em criar um script quando clicar no botão abria uma cena, você acha que abriria a camera tambem?
     
  48. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,886
    I'm not sure what you mean by capturing an object with the camera. This plugin open's Android/iOS device's camera hardware and takes an image from real life. If that's what you want, then as I said, you could use the example code's TakePicture function. Simply add that function to your script.
     
  49. vortexapps2

    vortexapps2

    Joined:
    Dec 11, 2020
    Posts:
    1
    Hello Yasirkula, i have been using your amazing plugin in my app in unity 2018, last week i donwloaded 2020 unity version but there is a lot of bugs and problems , so i started to make it from cero , so, the native camera is working, but wen i want to make an screenshot after taking the photo i dont get the capture saved on my phone galery, ive been using this code to take screenshot but in this newer version this does not working, could you help me please?

    PD: i updated the native camera plugin

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using System.IO;
    using UnityEngine.UI;


    public class takeScrenshBton : MonoBehaviour
    {

    public void TakeAshot()
    {
    // yield return CaptureScreenshot();
    Invoke("CaptureScreenshot", 0f);

    }

    public void CaptureScreenshot()
    {
    StartCoroutine(CaptureScreenshotCoroutine());

    }

    private IEnumerator CaptureScreenshotCoroutine()
    {
    // We should only read the screen buffer after rendering is complete
    yield return new WaitForEndOfFrame();

    // Create a texture the size of the screen, RGB24 format
    int width = Screen.width;
    int height = Screen.height;
    Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false);

    // Read screen contents into the texture
    tex.ReadPixels(new Rect(0, 0, width, height), 0, 0);
    tex.Apply();

    // Encode texture into PNG
    byte[] bytes = tex.EncodeToPNG();
    Object.Destroy(tex);


    string timeStamp = System.DateTime.Now.ToString("dd-MM-yyyy-HH-mm-ss");
    string filename = "Thepic_" + timeStamp + ".png";

    string fileLocation = Path.Combine(Application.persistentDataPath, filename);
    File.WriteAllBytes(fileLocation, bytes);

    string myFolderLocation = "/storage/emulated/0/Photoapp/";

    if (!Directory.Exists(myFolderLocation))

    //if it doesn't, create it
    Directory.CreateDirectory(myFolderLocation);

    string myScreenshotLocation = myFolderLocation + filename;
    System.IO.File.Move(fileLocation, myScreenshotLocation);

    using (AndroidJavaClass jcUnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
    using (AndroidJavaObject joActivity = jcUnityPlayer.GetStatic<AndroidJavaObject>("currentActivity"))
    using (AndroidJavaObject joContext = joActivity.Call<AndroidJavaObject>("getApplicationContext"))
    using (AndroidJavaClass jcMediaScannerConnection = new AndroidJavaClass("android.media.MediaScannerConnection"))
    using (AndroidJavaClass jcEnvironment = new AndroidJavaClass("android.os.Environment"))
    using (AndroidJavaObject joExDir = jcEnvironment.CallStatic<AndroidJavaObject>("getExternalStorageDirectory"))
    {
    jcMediaScannerConnection.CallStatic("scanFile", joContext, new string[] { myScreenshotLocation }, null, null);
    }

    }
     
  50. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,886
    I'd recommend you to use NativeGallery to save the image to Gallery. It should work.