Search Unity

NatDevice - Media Device API

Discussion in 'Assets and Asset Store' started by Lanre, Dec 17, 2015.

?

Should we add exposure controls in v1.3? This means dropping support for iOS 7

Poll closed Jun 10, 2016.
  1. Yes

    9 vote(s)
    75.0%
  2. No

    3 vote(s)
    25.0%
  1. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,973
    NatCam doesn't support linear lighting.
    On some models of devices (some S7 Edge and S8 models), we see lag introduced by the graphics driver when NatCam is reading back data from the GPU. The current workaround is to disable the `PreviewData` flag in NatCam > Plugins > Managed > Platforms > Android > NatCamAndroid.cs. This workaround will disable the CaputreFrame function. We have the full fix coming in the next release.
     
  2. SniperED007

    SniperED007

    Joined:
    Sep 29, 2013
    Posts:
    345
    Thanks that has made it much faster.

    Are there any plans to support Linear lighting?
     
    Lanre likes this.
  3. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,973
    I will look into adding support for linear rendering.
     
  4. SniperED007

    SniperED007

    Joined:
    Sep 29, 2013
    Posts:
    345
    2 more questions.

    1) Taking a photo on takes a couple of seconds, is that to be expected? (Samsung S8+)
    2) Is it possible to include UI in a photo?
     
  5. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,973
    Depending on the photo resolution and the lighting conditions, yes.
    No. Photos are captured from the camera.
     
  6. unitycodergp

    unitycodergp

    Joined:
    Feb 1, 2017
    Posts:
    47
    Hi,
    I'm on Natcam 1.5f3 and i've this problem with minicam on Android :
    If i set Preview resolution with Highest and Photo resolution with Medium it's ok but with superior resolution (HD + Full or Highest) i obtain a purple capture on Android :(
    Any idea plz ?
     
  7. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,973
    OpenGL ES doesn't support textures of that large size, so the texture is not created (and looks purple). You must reduce your photo resolution.
     
  8. unitycodergp

    unitycodergp

    Joined:
    Feb 1, 2017
    Posts:
    47
    i just use NatCam.CapturePhoto to take a screenshot of the screen. What's the problem ?
     
  9. henriqueranj

    henriqueranj

    Joined:
    Feb 18, 2016
    Posts:
    177
    Hello @Lanre , as promised I sent you an email with the crash/issue reports triggered by NatCam and NatCorder. Feel free to email me back if you need more information.
     
  10. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,973
    CapturePhoto takes a photo from the camera, not the screen. It is not a screenshot function.
     
  11. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,973
    Seen, thanks!
     
  12. younger12

    younger12

    Joined:
    Aug 8, 2017
    Posts:
    2
    Hello
    I use webcam in Orbbec Astra Pro(kind of depth sensor like kinect) and it has problem.
    It is written 30fps in Orbbec official site and also works 30fps in windows Camera app but doesn't it in unity.
    If I load it by webCamTexture, Its maximum fps is just 10.
    So, NatCam can solve it?
    If you can't be sure it, could you send me example program for test?
    Thank you.
     
  13. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,973
    NatCam does not have native support on Windows; it falls back to WebCamTexture. So you will not see any improvement by using NatCam.
     
  14. EnoxSoftware

    EnoxSoftware

    Joined:
    Oct 29, 2014
    Posts:
    1,566
    Hi Lanre.
    I ran the MiniCam example on the iOS platform (iPhone 6, iPhone 8), but I encountered the problem that the camera image is displayed as an image rotated by 180 degrees when it is in the landscape orientation.
    This problem seems to occur also in NatCam 2.0f2.
     

    Attached Files:

  15. TobiasW

    TobiasW

    Joined:
    Jun 18, 2011
    Posts:
    91
    Hello @Lanre ! I have a project that worked with NatCam 2.0f1 on my Sony Xperia Z3 Compact, but crashes on start with NatCam 2.0f2. What can I give you to help diagnose the problem?
     
  16. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,973
    I am looking into this now, will roll out a fix tomorrow. Thanks for bringing it to my attention.
     
  17. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,973
    Can you email me the full logs from logcat? Thank you.
     
  18. TobiasW

    TobiasW

    Joined:
    Jun 18, 2011
    Posts:
    91
    Sure. Any Unity build settings I should make? E.g. "development build", I assume?
     
  19. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,973
    Yes, you can enable development build. NatCam will always log verbosely, so just send me the logs from logcat.
     
  20. re-cheid

    re-cheid

    Joined:
    Apr 10, 2017
    Posts:
    34
    We are facing the same problem, also at Screen orientation PortraitUpsideDown the camera preview is 90° off on both Android and iOS.
     
    re-ckart and Lanre like this.
  21. TobiasW

    TobiasW

    Joined:
    Jun 18, 2011
    Posts:
    91
    Sent!
     
  22. EnoxSoftware

    EnoxSoftware

    Joined:
    Oct 29, 2014
    Posts:
    1,566
    I just now made sure that the problem was fixed with the new version.
    Thank you for the quick response.
     
  23. DM2016_ZJH

    DM2016_ZJH

    Joined:
    Nov 27, 2016
    Posts:
    4
    Hi @Lanre ,
    I update from NatCam 2.0f2 to NatCam 2.0f3, the camera image will be delayed, but NatCam 2.0f2 is OK.Can you help with this?
     
  24. rsdabb

    rsdabb

    Joined:
    Aug 30, 2017
    Posts:
    8
    Hello @Lanre!
    I'm trying to create a video with sound, created a script from the example and hung it on the listener.
    But when I run the application in the editor, I show errors:

    Internal_CreateGameObject can only be called from the main thread.
    Constructors and field initializers will be executed from the loading thread when loading a scene.
    Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
    UnityEngine.GameObject:.ctor(String)
    NatCamU.Dispatch.DispatchUtility:.cctor()
    NatCorderU.Core.NatCorder:.cctor() (at Assets/NatCorder/Plugins/Managed/NatCorder.cs:118)
    NatCorderU.Core.NatCorder:.cctor() (at Assets/NatCorder/Plugins/Managed/NatCorder.cs:118)
    AudioGetter:OnAudioFilterRead(Single[], Int32) (at Assets/NatCorder/Examples/ReplayCam/AudioGetter.cs:48)
    AudioGetter:OnAudioFilterRead(Single[], Int32) (at Assets/NatCorder/Examples/ReplayCam/AudioGetter.cs:47)


    And

    UnityException: Internal_CreateGameObject can only be called from the main thread.
    Constructors and field initializers will be executed from the loading thread when loading a scene.
    Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
    UnityEngine.GameObject..ctor (System.String name) (at /Users/builduser/buildslave/unity/build/artifacts/generated/bindings_old/common/Core/GameObjectBindings.gen.cs:393)
    NatCamU.Dispatch.DispatchUtility..cctor ()
    Rethrow as TypeInitializationException: An exception was thrown by the type initializer for NatCamU.Dispatch.DispatchUtility
    NatCorderU.Core.NatCorder..cctor () (at Assets/NatCorder/Plugins/Managed/NatCorder.cs:118)
    Rethrow as TypeInitializationException: An exception was thrown by the type initializer for NatCorderU.Core.NatCorder
    AudioGetter.OnAudioFilterRead (System.Single[] samples, Int32 channels) (at Assets/NatCorder/Examples/ReplayCam/AudioGetter.cs:47)


    Script AudioGetter.cs:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using NatCorderU.Core;
    4.  
    5. /*
    6. * We attach this script to an AudioSource/AudioListener
    7. * and forward the audio to NatCorder
    8. */
    9.  
    10. [RequireComponent(typeof(AudioSource))]
    11. public class AudioGetter : MonoBehaviour, IAudioSource
    12. {
    13.     public int sampleRate
    14.     {
    15.         get
    16.         {
    17.             return AudioSettings.outputSampleRate;
    18.         }
    19.     }
    20.  
    21.     public int sampleCount
    22.     {
    23.         get
    24.         {
    25.             int buffLen, numBuffer;
    26.             AudioSettings.GetDSPBufferSize(out buffLen, out numBuffer);
    27.             return buffLen;
    28.         }
    29.     }
    30.  
    31.     public int channelCount { get { return (int)AudioSettings.speakerMode; } }
    32.  
    33.  
    34.  
    35.     public void Dispose()
    36.     {
    37.         Debug.Log("Disposed, but not implemented");
    38.     }
    39.  
    40.  
    41.     private void OnAudioFilterRead(float[] samples, int channels)
    42.     {
    43.         long timestamp = (long)(AudioSettings.dspTime * 1e+9f);
    44.         NatCorder.CommitSamples(samples, timestamp);
    45.         Debug.Log("tick");
    46.     }
    47. }
    48.  
    Script ReplayCam:
    Code (CSharp):
    1. namespace NatCorderU.Examples {
    2.  
    3.     using UnityEngine;
    4.     using UnityEngine.UI;
    5.     using System.Collections;
    6.     using Core;
    7.     using UnityEngine.Video;
    8.  
    9.     public class ReplayCam : MonoBehaviour {
    10.         //public VideoPlayer videoPlayer;
    11.         public VideoViewer videoViewer;
    12.         //public Animator recAnimator;
    13.         public bool recordMicrophoneAudio;
    14.         public AudioSource audioSource;
    15.         //public AudioListener mainListener;
    16.         public Text textShowPath;
    17.         public bool process;
    18.  
    19.         public Transform listenerTransform;
    20.  
    21.         void Start()
    22.         {
    23.             videoViewer = GetComponent<VideoViewer>();
    24.             process = false;
    25.         }
    26.         public void StartRecording () {
    27.             // Create a recording configuration
    28.             const float DownscaleFactor = 2f / 3;
    29.             var configuration = new Configuration((int)(Screen.width * DownscaleFactor), (int)(Screen.height * DownscaleFactor));
    30.             // Start recording with microphone audio
    31.             if (recordMicrophoneAudio)
    32.             {
    33.                 StartMicrophone();
    34.                 Replay.StartRecording(Camera.main, configuration, OnReplay, audioSource, true);
    35.                 //Replay.StartRecording(Camera.main, configuration, OnReplay, mainListener, true);
    36.             }
    37.             // Start recording without microphone audio
    38.             else
    39.             {
    40.                 AudioGetter audioGetter = listenerTransform.GetComponent<AudioGetter>();
    41.                 Replay.StartRecording(Camera.main, configuration, OnReplay, audioGetter);
    42.                 //Replay.StartRecording(Camera.main, configuration, OnReplay);
    43.             }
    44.             //recAnimator.SetBool("ShowIndicator", true);
    45.             process = true;
    46.         }
    47.  
    48.         private void StartMicrophone () {
    49.             #if !UNITY_WEBGL || UNITY_EDITOR // No `Microphone` API on WebGL :(
    50.             // If the clip has not been set, set it now
    51.             if (audioSource.clip == null) {
    52.                 audioSource.clip = Microphone.Start(null, true, 60, 48000);
    53.                 while (Microphone.GetPosition(null) <= 0) ;
    54.             }          
    55.             // Play through audio source
    56.             audioSource.timeSamples = Microphone.GetPosition(null);
    57.             audioSource.loop = true;
    58.             audioSource.Play();
    59.             #endif
    60.         }
    61.  
    62.         public void StopRecording () {
    63.             if (recordMicrophoneAudio) audioSource.Stop();
    64.             Replay.StopRecording();
    65.             //recAnimator.SetBool("ShowIndicator", false);
    66.             process = false;
    67.         }
    68.  
    69.         void OnReplay (string path) {
    70.             Debug.Log("Saved recording to: "+path);
    71.             //
    72.             textShowPath.text = path;
    73.  
    74.             videoViewer.ViewAndPlayVideo(path);
    75.             //https://forum.unity.com/threads/native-gallery-for-android-ios-open-source.519619/
    76.             //NativeGallery.SaveVideoToGallery(path, "Beavers AR", "video{0}.mp4");
    77.             // Playback the video
    78.             #if UNITY_IOS
    79.             //Handheld.PlayFullScreenMovie("file://" + path);
    80.             #elif UNITY_ANDROID
    81.             //Handheld.PlayFullScreenMovie(path);
    82.             #endif
    83.         }
    84.     }
    85. }
     
    Last edited: Jul 10, 2018
  25. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,973
    Perfect!
     
  26. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,973
    How do you mean it is delayed? What device are you testing on?
     
  27. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,973
    You're posting on the wrong thread ;). Anyway, the fix is simple. Create the AudioGetter right before StartRecording and pass it in:
    Code (CSharp):
    1. AudioGetter getter = sceneListener.AddComponent<AudioGetter>();
    2. Replay.StartRecording(..., getter);
     
  28. rsdabb

    rsdabb

    Joined:
    Aug 30, 2017
    Posts:
    8
    @Lanre
    Thank you so much:)
     
    Lanre likes this.
  29. DM2016_ZJH

    DM2016_ZJH

    Joined:
    Nov 27, 2016
    Posts:
    4
    We watch a fast moving things via enable the front-facing camera and will be feel the image is delay and distortion.
    Which is discovered by NatCam 2.0f1 update to NatCam 2.0f3 version.
    for example, when i recording my finger and fast moving by the front-facing camera. the image display is delayed and distortion while compare with real fingers moving.
    It's just NatCam 2.0f1 version is working well. i was compiled Scripting Backend=IL2CPP and runing Android OS 8.0.0 / API-26 (HUAWEISTF-AL10/341(C00)) .
    More details, you could refer to following two demos.

    https://github.com/EnoxSoftware/Nat....1/NatCamWithOpenCVForUnityExample_v1.0.1.apk,
    https://github.com/EnoxSoftware/Nat...2/NatCamWithOpenCVForUnityExample_v1.0.2.apk,
    NatCamWithOpenCVForUnityExample_v1.0.1.apk was using NatCam 2.0f1 which is working well ,without delay and distortion.
    but NatCamWithOpenCVForUnityExample_v1.0.2.apk had delay and distortion issues。
    the following information is from the two simples with my cellphone.

    ### System Info ###
    OpenCVForUnity version = opencvforunity 2.2.8 (3.4.1-dev)
    Build Unity version = 2017.3.0f3
    Build target = Android
    Scripting backend = IL2CPP
    operatingSystem = Android OS 8.0.0 / API-26 (HUAWEISTF-AL10/341(C00))
    iPhone.generation =
    deviceModel = HUAWEI STF-AL10
    deviceName = <unknown>
    deviceType = Handheld
    graphicsDeviceName = Mali-G71
    graphicsDeviceVendor = ARM
    processorType = ARMv7 VFPv3 NEON
    graphicsMemorySize = 2048
    systemMemorySize = 5729
    graphicsDeviceID = 0
    graphicsDeviceType = OpenGLES3
    graphicsDeviceVendorID = 0
    graphicsDeviceVersion = OpenGL ES 3.2 v1.r8p0-00cet0.c14e014a87b26dd123ddd0d6c57eeff3
    graphicsMultiThreaded = False
    graphicsShaderLevel = 50
    copyTextureSupport = Basic, Copy3D, DifferentTypes, TextureToRT, RTToTexture
    supportsAccelerometer = True
    supportsGyroscope = True
    supportsVibration = True
    supportsLocationService = True
    ###################
    ### System Info ###
    OpenCVForUnity version = opencvforunity 2.2.9 (3.4.1-dev)
    Build Unity version = 2017.3.0f3
    Build target = Android
    Scripting backend = IL2CPP
    operatingSystem = Android OS 8.0.0 / API-26 (HUAWEISTF-AL10/341(C00))
    iPhone.generation =
    deviceModel = HUAWEI STF-AL10
    deviceName = <unknown>
    deviceType = Handheld
    graphicsDeviceName = Mali-G71
    graphicsDeviceVendor = ARM
    processorType = ARMv7 VFPv3 NEON
    graphicsMemorySize = 2048
    systemMemorySize = 5729
    graphicsDeviceID = 0
    graphicsDeviceType = OpenGLES3
    graphicsDeviceVendorID = 0
    graphicsDeviceVersion = OpenGL ES 3.2 v1.r8p0-00cet0.c14e014a87b26dd123ddd0d6c57eeff3
    graphicsMultiThreaded = False
    graphicsShaderLevel = 50
    copyTextureSupport = Basic, Copy3D, DifferentTypes, TextureToRT, RTToTexture
    supportsAccelerometer = True
    supportsGyroscope = True
    supportsVibration = True
    supportsLocationService = True
    ###################
     
  30. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,973
    Hi there. Sorry about this, I didn't get an email notification of your first post. I cannot reproduce any delay or distortion in any of the APK's. I'm able to achieve smooth 30FPS with a 1920x1080 preview on my S7 Edge. Can you email me a video showing the delay you describe, along with full logs from logcat?
     
  31. jpenery

    jpenery

    Joined:
    Jul 13, 2018
    Posts:
    5
    A couple of questions)
    1 - When I ask NatCam for the vertical FOV of the camera, I get a strange value. The correct value, according to the formula given https://stackoverflow.com/questions...ivalent-of-camera-parameters-gethorizontalvie (and verified experimentally) is 41.25078, but NatCam says it's 53.2988. Perhaps it doesn't take into account some aspect ratio correction, but I haven't been able to reverse engineer that number to determine why it's incorrect.

    Where is NatCam getting this vertical FOV number from? The horizontal FOV it gives seems to be correct.

    Correct FOV is important for any AR/VR application, so the world rotation matches the camera preview rotation, otherwise objects appear to drift in relation to the world when rotating the device.

    2 - Is there a way to figure out which device NatCam chooses when you select RearCamera and there are multiple available? i.e., which WebCamDevice does it correspond to from the WebCamTexture.devices array? Is it always just the first one, or can it vary?

    Thanks!
     
  32. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,973
    If you are on Android, then the FOV value is gotten directly from the camera API. NatCam uses the camera1 API to ensure greater compatibility, not camera2.
    NatCam doesn't rely on WebCamDevice for identifying cameras. For populating DeviceCamera.FrontCamera and DeviceCamera.RearCamera, NatCam uses the first camera it finds with that position. You can see the implementation in the static constructor of the DeviceCamera class.
     
  33. Colin_MacLeod

    Colin_MacLeod

    Joined:
    Feb 11, 2014
    Posts:
    334
    I'm using NatCam to preview on a texture. I need to release it temporarily and then start playing again.

    I need to switch to another native plugin (actually, Voxelbusters Cross Platform Native Plugins) to perform some functions.

    When I call NatCam.Release(), I can call VB CPNP and that's fine, but how do I start NatCam again afterwards?

    NatCam.Play() seems to throw an exception.
     
  34. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,973
    This is most likely because VB CPNP does not properly release the camera (what NatCam.Release does). Reach out to their customer support.
     
  35. Colin_MacLeod

    Colin_MacLeod

    Joined:
    Feb 11, 2014
    Posts:
    334
    Oh? But I’m finding I get an exception even if I don’t call anything else.

    To test I used 2 UI buttons. One calls NatCam.Release(), the other NatCam.Play().

    Should that work? The second button throws an exception

    (Thanks so much for the fast reply. I’m not at my computer and can post the exception stack trace when I get back. Just wanted to make sure I’d implemented the correct sequence of calls.)
     
  36. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,973
    Can you post the stack trace when you can? This should not be happening.
     
  37. Colin_MacLeod

    Colin_MacLeod

    Joined:
    Feb 11, 2014
    Posts:
    334
    Ah ok. Will do.

    Thanks. I thought I was making a mistake in the interface calls.
     
  38. Colin_MacLeod

    Colin_MacLeod

    Joined:
    Feb 11, 2014
    Posts:
    334
    Here's the stack trace from logcat on a Samsung Tab 4 tablet:

    Code (CSharp):
    1. E  [ 5338] Unity    AndroidJavaException: java.lang.NullPointerException
    2. E  [ 5338] Unity    java.lang.NullPointerException
    3. E  [ 5338] Unity       at com.yusufolokoba.natcam.NatCam.play(NatCam.java:61)
    4. E  [ 5338] Unity       at com.unity3d.player.UnityPlayer.nativeRender(Native Method)
    5. E  [ 5338] Unity       at com.unity3d.player.UnityPlayer.c(Unknown Source)
    6. E  [ 5338] Unity       at com.unity3d.player.UnityPlayer$e$2.queueIdle(Unknown Source)
    7. E  [ 5338] Unity       at android.os.MessageQueue.next(MessageQueue.java:207)
    8. E  [ 5338] Unity       at android.os.Looper.loop(Looper.java:131)
    9. E  [ 5338] Unity       at com.unity3d.player.UnityPlayer$e.run(Unknown Source)
    10. E  [ 5338] Unity      at UnityEngine.AndroidJNISafe.CheckException () [0x00000] in <filename unknown>:0
    11. E  [ 5338] Unity      at UnityEngine.AndroidJNISafe.CallVoidMethod (IntPtr obj, IntPtr methodID, UnityEngine.jvalue[] args) [0x00000] in <filename unknown>:0
    12. E  [ 5338] Unity      at UnityEngine.AndroidJavaObject._Call (System.String methodName, System.Object[] args) [0x00000] in <filename unknown>:0
    13. E  [ 5338] Unity      at UnityEngine.AndroidJavaObject.Call (System.String methodName, System.Object[] args) [0x00000] in <filename unknown>:0
    14. E  [ 5338] Unity      at NatCamU.Core.Platforms.NatCamAndroid.Play () [0x00000] in <filename
    15.  
    Here is the stack trace running on Unity Editor:

    Code (CSharp):
    1. IndexOutOfRangeException: Array index is out of range.
    2. NatCamU.Core.Platforms.NatCamLegacy.Play () (at Assets/NatCam/Plugins/Managed/Platforms/NatCamLegacy.cs:74)
    3. NatCamU.Core.NatCam.Play (NatCamU.Core.DeviceCamera camera) (at Assets/NatCam/Plugins/Managed/NatCam.cs:75)
    4. InkMage.DeviceCameraController.Resume () (at Assets/_Project/Scripts/Camera/DeviceCameraController.cs:118)
    5. UnityEngine.Events.InvokableCall.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:166)
    6. UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:58)
    7. UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:36)
    8. UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:45)
    9. UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:50)
    10. UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:261)
    11. UnityEngine.EventSystems.EventSystem:Update()
     
  39. Colin_MacLeod

    Colin_MacLeod

    Joined:
    Feb 11, 2014
    Posts:
    334
    The first button litterally just calls
    Code (CSharp):
    1. NatCam.Release()
    and the second
    Code (CSharp):
    1. NatCam.Play()
    .
     
  40. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,973
    Chances are that you never set a camera for NatCam to play from, so NatCam.Camera is null. You can pass in a camera to NatCam.Play like so: `NatCam.Play(DeviceCamera.RearCamera);`
     
  41. Colin_MacLeod

    Colin_MacLeod

    Joined:
    Feb 11, 2014
    Posts:
    334
    I do set the camera in Awake.

    Code (CSharp):
    1. private void Awake()
    2.         {
    3.             Log("Awake");
    4.             _background = GetComponent<RawImage>();
    5.             Assert.IsNotNull(_background, "DeviceCameraImage requires a RawImage component");
    6.             _aspectRatioFitter = GetComponent<AspectRatioFitter>();
    7.             Assert.IsNotNull(_aspectRatioFitter, "DeviceCameraImage requires a AspectRatioFitter component");
    8.  
    9.             if (DeviceCamera.RearCamera == null)
    10.             {
    11.                 if (DeviceCamera.FrontCamera == null)
    12.                 {
    13.                     LogWarning("No device camera is available");
    14.                 }
    15.                 else
    16.                 {
    17.                     LogWarning("No rear camera defined - switching to front camera");
    18.                     NatCam.Camera = DeviceCamera.FrontCamera;
    19.                 }
    20.             }
    21.             else
    22.             {
    23.                 Log("Found rear camera");
    24.                 NatCam.Camera = DeviceCamera.RearCamera;
    25.             }
    26.         }
    27.         private void Start()
    28.         {
    29.             Log("Start - NatCam.Camera: " + NatCam.Camera);
    30.             if (NatCam.Camera != null)
    31.             {
    32.                 // Set the preview resolution to Full HD
    33.                 NatCam.Camera.PreviewResolution = CameraResolution._1920x1080;
    34.                 // Set the photo resolution to highest
    35.                 NatCam.Camera.PhotoResolution = CameraResolution.Highest;
    36.                 // Start the camera preview
    37.                 NatCam.Play(NatCam.Camera);
    38.                 // Get notified when the preview actually starts
    39.                 NatCam.OnStart += OnStartCamera;
    40.             }
    41.             // see whether the camera can be flipped
    42.             if (DeviceCamera.FrontCamera != null && DeviceCamera.RearCamera != null)
    43.             {
    44.                 Log("OnStartCamera - Invoking Flip Camera ENABLED");
    45.                 _flipCameraEnabled.Invoke();
    46.             }
    47.             else
    48.             {
    49.                 Log("OnStartCamera - Invoking Flip Camera DISABLED");
    50.                 _flipCameraDisabled.Invoke();
    51.             }
    52.         }

    Do I need to do this explicitly when calling Play?
     
  42. Colin_MacLeod

    Colin_MacLeod

    Joined:
    Feb 11, 2014
    Posts:
    334
    PS: I've just tested this, and before NatCam.Release(), NatCam.Camera is set to a value. Straight after NatCam.Release() it is null.

    Should I go through all the steps in my Awake routine after NatCam.Release()?
     
  43. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,973
    After calling Release, the camera is nullified since NatCam actually releases the hardware cameras. Also, I don't recommend calling NatCam from Awake because NatCam's functions should be called from the main thread (the only exception is CaptureFrame, which is designed to be called from any thread) and Unity doesn't guarantee that Awake is called on the main thread.
     
  44. Colin_MacLeod

    Colin_MacLeod

    Joined:
    Feb 11, 2014
    Posts:
    334
    OK - got it.

    As you can see I was only setting NatCam.Camera there but I have moved that to Start.

    Should I remove the OnStart listener before Release and reinstate it after? I saw in the docs you add the listener after NatCam.Play - is there a reason it's not defined before?

    With this version, it's working in the Editor but not on my android device. Stack trace is:

    Code (CSharp):
    1. E  [ 7941] Unity    NatCam Error: Failed to open camera 0 with error: Fail to connect to camera service
    2. D  [ 7941] dalvikvm GC_FOR_ALLOC freed 45K, 29% free 8128K/11348K, paused 37ms, total 37ms
    3. D  [ 7941] dalvikvm GC_FOR_ALLOC freed 454K, 32% free 8128K/11804K, paused 29ms, total 29ms
    4. E  [ 7941] Unity    AndroidJavaException: java.lang.NullPointerException
    5. E  [ 7941] Unity    java.lang.NullPointerException
    6. E  [ 7941] Unity       at com.yusufolokoba.natcam.NatCam.play(NatCam.java:63)
    7. E  [ 7941] Unity       at com.unity3d.player.UnityPlayer.nativeRender(Native Method)
    8. E  [ 7941] Unity       at com.unity3d.player.UnityPlayer.c(Unknown Source)
    9. E  [ 7941] Unity       at com.unity3d.player.UnityPlayer$e$2.queueIdle(Unknown Source)
    10. E  [ 7941] Unity       at android.os.MessageQueue.next(MessageQueue.java:207)
    11. E  [ 7941] Unity       at android.os.Looper.loop(Looper.java:131)
    12. E  [ 7941] Unity       at com.unity3d.player.UnityPlayer$e.run(Unknown Source)
    13. E  [ 7941] Unity      at UnityEngine.AndroidJNISafe.CheckException () [0x0008c] in /Users/builduser/buildslave/unity/build/Runtime/Export/AndroidJNISafe.cs:24
    14. E  [ 7941] Unity      at UnityEngine.AndroidJNISafe.CallVoidMethod (IntPtr obj, IntPtr methodID, UnityEngine.jvalue[] args) [0x00011] in /Users/builduser/buildslave/unity/build/Runtime/Export/AndroidJNISafe.cs:357
    15. E  [ 7941] Unity      at UnityEngine.AndroidJavaObject._Call (System.String methodName, System.Object[] args) [0x00038] in /Users/builduser/buildslave/unity/build/Runtime/Export/AndroidJavaImpl.cs:254
    16. E  [ 7941] Unity      at UnityEngine.AndroidJava
    17.  
    Here's what I have now in the code for the 2 buttons, Pause and Resume:

    Code (CSharp):
    1.  
    2.        public void Pause()
    3.         {
    4.             NatCam.OnStart -= OnStartCamera;
    5.             NatCam.Release();
    6.         }
    7.         public void Resume()
    8.         {
    9.             Debug.Log("BEFORE PLAY");
    10.             if (DeviceCamera.RearCamera == null)
    11.             {
    12.                 if (DeviceCamera.FrontCamera != null)
    13.                 {
    14.                     Debug.Log("FRONT CAMERA");
    15.                     NatCam.Camera = DeviceCamera.FrontCamera;
    16.                 }
    17.             }
    18.             else
    19.             {
    20.                 Debug.Log("REAR CAMERA");
    21.                 NatCam.Camera = DeviceCamera.RearCamera;
    22.             }
    23.             if (NatCam.Camera != null)
    24.             {
    25.                 // Set the preview resolution to Full HD
    26.                 // NatCam.Camera.PreviewResolution = CameraResolution._1920x1080;
    27.                 // Start the camera preview
    28.                 NatCam.Play(NatCam.Camera);
    29.                 // Get notified when the preview actually starts
    30.                 NatCam.OnStart += OnStartCamera;
    31.             }
    32.             Debug.Log("AFTER PLAY");
    33.         }
    34.  
    35.  
     
  45. Colin_MacLeod

    Colin_MacLeod

    Joined:
    Feb 11, 2014
    Posts:
    334
    Sorry, Lanre!!

    I think I test the wrong version that last time. Apologies!

    I built it again and it's fine on the Android device too.
     
    Lanre likes this.
  46. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,973
    That sounds good. Sorry for the late response.
     
  47. Colin_MacLeod

    Colin_MacLeod

    Joined:
    Feb 11, 2014
    Posts:
    334
    No, you're fantastic. It's a great plugin - exactly what we needed for our project.

    Thank you so much.
     
    Lanre likes this.
  48. unity_oKIWeM438DnzCA

    unity_oKIWeM438DnzCA

    Joined:
    Aug 6, 2018
    Posts:
    1
    Sry i may know how to adjust the resolution of the cam same like Ipad
    ?
     
  49. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,973
    You can set the preview resolution of the camera like so:
    Code (CSharp):
    1. NatCam.Camera.PreviewResolution = new CameraResolution(width, height);
    You must note that the camera isn't guaranteed to support the resolution you set, in which case NatCam will set the closest supported resolution that is actually supported. So if your new device doesn't support the iPad camera resolution, the resolution on your new device will be different.
     
  50. DerrickBarra

    DerrickBarra

    Joined:
    Nov 19, 2013
    Posts:
    210
    Hi @Lanre , I've noticed some null references within NatCam when attempting to use it on my Galaxy S8. This is what occurs when I take a picture or begin video capture. NatCam is working normally in Unity Editor. If needed I can email the script to you.


    08-10 13:02:57.977 8393 8494 E Unity : java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List android.hardware.Camera$Parameters.getSupportedPictureSizes()' on a null object reference
    08-10 13:02:57.977 8393 8494 E Unity : at com.yusufolokoba.natcam.Utilities.ClosestPhotoResolution(Utilities.java:27)
    08-10 13:02:57.977 8393 8494 E Unity : at com.yusufolokoba.natcam.NatCamDevice.setPhotoResolution(NatCamDevice.java:141)
    08-10 13:02:57.977 8393 8494 E Unity : at com.unity3d.player.UnityPlayer.nativeRender(Native Method)
    08-10 13:02:57.977 8393 8494 E Unity : at com.unity3d.player.UnityPlayer.c(Unknown Source:0)
    08-10 13:02:57.977 8393 8494 E Unity : at com.unity3d.player.UnityPlayer$e$2.queueIdle(Unknown Source:72)
    08-10 13:02:57.977 8393 8494 E Unity : at android.os.MessageQueue.next(MessageQueue.java:394)
    08-10 13:02:57.977 8393 8494 E Unity : at android.os.Looper.loop(Looper.java:142)
    08-10 13:02:57.977 8393 8494 E Unity : at com.unity3d.player.UnityPlayer$e.run(Unknown Source:32)
    08-10 13:02:57.977 8393 8494 E Unity : at UnityEngine.AndroidJNISafe.CheckException () [0x0008c] in /Users/builduser/buildslave/unity/build/Runtime/Export/AndroidJNISafe.cs:24
    08-10 13:02:57.977 8393 8494 E Unity : at Unity