If you are creating a game with ARToolKit on Unity you most likely use the Unity 3D plugin from the ARToolKit.org website. Unfortunately, that release is a bit old and does not contain such features like autofocus on Android. Also, the default video resolution when you run your game is 320×240 which leads to a very bad user experience like jittering (shaking) of the 3D model you want to put on the ARMarker. So I’m going to show you how you can fix that very quick.
Autofocus
Luckily the autofocus feature has already been implemented into the plugin and is available on GitHub. Lucky again it was already merged to the master branch as well. So all we need to do is to clone the ARUnity5 Git repository and build the source code from there.
Build UnityARPlayer
To build the ARPlayer we need to follow these steps:
- Clone the repository into an empty directory
- git clone https://github.com/artoolkit/arunity5.git
- Start AndroidStudio (Tested with 2.2)
- Select Open an existing Android Studio project
- On the right of the Android Studio window there is a little Gradle-tab. You need to select that:
- Then you need to open the section for UnityARPlayer/Tasks/other/jarRelease
- The jar file is created into \[Git-repository-directory]/src/Android_Unity_Player_Source/AndroidStudioProj/UnityARPlayer/build/libs/UnityARPlayer.jar
- Copy that to your UnityProject/Assets/Plugins/Android and replace the existing version. (You might want to take a copy for safety 🙂 )
More detailed information about building the UnityARPlayer can be found here.
Video resolution
As for the video resolution, you would need to access the preference activity which comes with the ARPlayer. Sadly, due to the lack of the system menu button in the modern phones and the full-screen/immersive mode of the Unity games, that is hidden. Additionally, for a great game I can imagine that you do not want to bother the user with selecting a video resolution. To solve that I propose to set the video resolution to a fixed value that suits you best. For a simple example, I enhanced the UnityARPlayer code to select the highest video resolution available on the phone. That code can be found on my GitHub repository.
//Look for highest supported preview size and use that one. List<Camera.Size> previewSizes = parameters.getSupportedPreviewSizes(); for (Camera.Size size: previewSizes) { Log.d("CameraSurface","PreviewSize: " + size.width + "x"+ size.height + "/n"); } Camera.Size highestPreviewSize = parameters.getSupportedPreviewSizes().get(0); Log.d("CameraSurface","PreviewSize: " + highestPreviewSize.width + "x"+ highestPreviewSize.height + "/n"); parameters.setPreviewSize(highestPreviewSize.width,highestPreviewSize.height);
If you now build the UnityARPlayer again and replace it in your project you should have a crystal clear video and the autofocus function.
Conclusion
Most of the features you need for a great game with ARToolKit, Unity3D and Android are already there, but somehow hidden and require manual build steps to enable them. You now have the knowledge to find and use those features which enables you to create great games with ARToolKit and Unity 3D.
Please let me know if that works for you as good as it works for me and tell me your tricks with Unity 3D and ARToolKit.
Bonus
If you read till the end here is a little bonus for you ;). I already created a UnityARPlayer.jar (deprecated see Update 1) that has the above features enabled. So go ahead and use that if you like.
Update 1:
Thanks to all the community feedback we found out that not all phone treat the video resolution the same way. Because of that, I added a for-each loop to loop over the available video resolutions and pick the one with the highest video-width. I hope that this solves the issues for all of you.
Thanks so much for your feedback. Here is the new UnityARPlayer.jar (Deprecated see Update 2) file for you.
Update 2:
I realized that the app sometimes crashes when put to the background and back to active again. UnityARPlayer.jar second update fixes this issue.
The code changes can be seen here: https://github.com/ThorstenBux/arunity5/commit/804741d437eb1b444ac6c553e25e2467971e7fe3
Happy coding
Hello!
I change UnityARPlayer.jar in plugins/Android/.
App doesnt start on phone.
What is wrong?
Hi sergeyko86,
The best thing to analyze that is to have a look at the logcat output. Please connect your device with your machine and open AndroidStudio before starting the app from Unity3D. Then please look that the Android Monitor (logcat) output for error messages and send me those.
Thanks
11-21 20:25:26.221: E/AndroidRuntime(27072): FATAL EXCEPTION: main
11-21 20:25:26.221: E/AndroidRuntime(27072): Process: com.Artoolkit.demo, PID: 27072
11-21 20:25:26.221: E/AndroidRuntime(27072): java.lang.Error: FATAL EXCEPTION [main]
11-21 20:25:26.221: E/AndroidRuntime(27072): Unity version : 5.4.1f1
11-21 20:25:26.221: E/AndroidRuntime(27072): Device model : Sony D5503
11-21 20:25:26.221: E/AndroidRuntime(27072): Device fingerprint: Sony/D5503/D5503:5.1.1/14.6.A.1.236/2031203603:user/release-keys
11-21 20:25:26.221: E/AndroidRuntime(27072): Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v4/content/ContextCompat;
11-21 20:25:26.221: E/AndroidRuntime(27072): at org.artoolkit.ar.unity.UnityARPlayerActivity.onCreate(UnityARPlayerActivity.java:127)
11-21 20:25:26.221: E/AndroidRuntime(27072): at android.app.Activity.performCreate(Activity.java:5990)
11-21 20:25:26.221: E/AndroidRuntime(27072): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
11-21 20:25:26.221: E/AndroidRuntime(27072): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2332)
11-21 20:25:26.221: E/AndroidRuntime(27072): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442)
11-21 20:25:26.221: E/AndroidRuntime(27072): at android.app.ActivityThread.access$800(ActivityThread.java:156)
11-21 20:25:26.221: E/AndroidRuntime(27072): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351)
11-21 20:25:26.221: E/AndroidRuntime(27072): at android.os.Handler.dispatchMessage(Handler.java:102)
11-21 20:25:26.221: E/AndroidRuntime(27072): at android.os.Looper.loop(Looper.java:211)
11-21 20:25:26.221: E/AndroidRuntime(27072): at android.app.ActivityThread.main(ActivityThread.java:5389)
11-21 20:25:26.221: E/AndroidRuntime(27072): at java.lang.reflect.Method.invoke(Native Method)
11-21 20:25:26.221: E/AndroidRuntime(27072): at java.lang.reflect.Method.invoke(Method.java:372)
11-21 20:25:26.221: E/AndroidRuntime(27072): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)
11-21 20:25:26.221: E/AndroidRuntime(27072): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)
11-21 20:25:26.221: E/AndroidRuntime(27072): Caused by: java.lang.ClassNotFoundException: Didn’t find class “android.support.v4.content.ContextCompat” on path: DexPathList[[zip file “/data/app/com.Artoolkit.demo-1/base.apk”],nativeLibraryDirectories=[/data/app/com.Artoolkit.demo-1/lib/arm, /vendor/lib, /system/lib]]
11-21 20:25:26.221: E/AndroidRuntime(27072): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
11-21 20:25:26.221: E/AndroidRuntime(27072): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
11-21 20:25:26.221: E/AndroidRuntime(27072): at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
11-21 20:25:26.221: E/AndroidRuntime(27072): … 14 more
11-21 20:25:26.221: E/AndroidRuntime(27072): Suppressed: java.lang.ClassNotFoundException: android.support.v4.content.ContextCompat
11-21 20:25:26.221: E/AndroidRuntime(27072): at java.lang.Class.classForName(Native Method)
11-21 20:25:26.221: E/AndroidRuntime(27072): at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
11-21 20:25:26.221: E/AndroidRuntime(27072): at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
11-21 20:25:26.221: E/AndroidRuntime(27072): at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
11-21 20:25:26.221: E/AndroidRuntime(27072): … 15 more
11-21 20:25:26.221: E/AndroidRuntime(27072): Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available
The key in that message is this error
Caused by: java.lang.ClassNotFoundException: Didn’t find class “android.support.v4.content.ContextCompat” on path: DexPathList[[zip file “/data/app/com.Artoolkit.demo-1/base.apk”],nativeLibraryDirectories=[/data/app/com.Artoolkit.demo-1/lib/arm, /vendor/lib, /system/lib]]
11-21 20:25:26.221: E/AndroidRuntime(27072): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
You need to integrate the library support-v4-23.3.0.aar into your Unity project. The path to this file is the same as in the Git repository:
ContextCompat
Let me know if that helps
If i use arunity5/arunity5.unitypackage
11-21 23:55:53.478: E/mm-camera-intf(353): mm_camera_open: opened, break out while loop
11-21 23:55:53.482: I/cald(353): 14626207781608 excal_hal_ctrl.cpp (1783) 21833 I [EXC] [COMP] [HalCtrl 010] – HalCtrl::changeInitToReady 0
11-21 23:55:53.491: I/libcamera(353): HAL_openCameraHardware: X created hardware=0xb544fc00
11-21 23:55:53.493: I/Avaiable focus modes:(21766): auto
11-21 23:55:53.493: I/Avaiable focus modes:(21766): infinity
11-21 23:55:53.493: I/Avaiable focus modes:(21766): macro
11-21 23:55:53.493: I/Avaiable focus modes:(21766): continuous-picture
11-21 23:55:53.493: I/Avaiable focus modes:(21766): continuous-video
11-21 23:55:53.494: W/ServiceManager(353): Permission failure: com.sonyericsson.permission.CAMERA_EXTENDED from uid=10263 pid=21766
11-21 23:55:53.494: W/ServiceManager(353): Permission failure: com.sonyericsson.permission.CAMERA_EXTENDED from uid=10263 pid=21766
11-21 23:55:53.495: E/cald(353): 14626221634316 excal_cam_ctrl.cpp (3338) 21831 E [EXC] [COMP] [CamCtrl 056] Not support autoFocus range[0x1] supported[0xe]
11-21 23:55:53.496: E/libcamera(353): setDynamicParameterValues: focus-mode unknown setting. (focus = 0, focusRange = 0)
11-21 23:55:53.500: D/AndroidRuntime(21766): Shutting down VM
11-21 23:55:53.503: E/AndroidRuntime(21766): FATAL EXCEPTION: main
11-21 23:55:53.503: E/AndroidRuntime(21766): Process: com.DvaVinta.Resolution, PID: 21766
11-21 23:55:53.503: E/AndroidRuntime(21766): java.lang.Error: FATAL EXCEPTION [main]
11-21 23:55:53.503: E/AndroidRuntime(21766): Unity version : 5.4.1f1
11-21 23:55:53.503: E/AndroidRuntime(21766): Device model : Sony D5503
11-21 23:55:53.503: E/AndroidRuntime(21766): Device fingerprint: Sony/D5503/D5503:5.1.1/14.6.A.1.236/2031203603:user/release-keys
11-21 23:55:53.503: E/AndroidRuntime(21766): Caused by: java.lang.RuntimeException: setParameters failed
11-21 23:55:53.503: E/AndroidRuntime(21766): at android.hardware.Camera.native_setParameters(Native Method)
11-21 23:55:53.503: E/AndroidRuntime(21766): at android.hardware.Camera.setParameters(Camera.java:1932)
11-21 23:55:53.503: E/AndroidRuntime(21766): at org.artoolkit.ar.unity.CameraSurface.surfaceChanged(CameraSurface.java:162)
11-21 23:55:53.503: E/AndroidRuntime(21766): at android.view.SurfaceView.updateWindow(SurfaceView.java:591)
11-21 23:55:53.503: E/AndroidRuntime(21766): at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:176)
11-21 23:55:53.503: E/AndroidRuntime(21766): at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:948)
11-21 23:55:53.503: E/AndroidRuntime(21766): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1974)
11-21 23:55:53.503: E/AndroidRuntime(21766): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1065)
11-21 23:55:53.503: E/AndroidRuntime(21766): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5901)
11-21 23:55:53.503: E/AndroidRuntime(21766): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
11-21 23:55:53.503: E/AndroidRuntime(21766): at android.view.Choreographer.doCallbacks(Choreographer.java:580)
11-21 23:55:53.503: E/AndroidRuntime(21766): at android.view.Choreographer.doFrame(Choreographer.java:550)
11-21 23:55:53.503: E/AndroidRuntime(21766): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
11-21 23:55:53.503: E/AndroidRuntime(21766): at android.os.Handler.handleCallback(Handler.java:739)
11-21 23:55:53.503: E/AndroidRuntime(21766): at android.os.Handler.dispatchMessage(Handler.java:95)
11-21 23:55:53.503: E/AndroidRuntime(21766): at android.os.Looper.loop(Looper.java:211)
11-21 23:55:53.503: E/AndroidRuntime(21766): at android.app.ActivityThread.main(ActivityThread.java:5389)
11-21 23:55:53.503: E/AndroidRuntime(21766): at java.lang.reflect.Method.invoke(Native Method)
11-21 23:55:53.503: E/AndroidRuntime(21766): at java.lang.reflect.Method.invoke(Method.java:372)
11-21 23:55:53.503: E/AndroidRuntime(21766): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)
11-21 23:55:53.503: E/AndroidRuntime(21766): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)
Hi sergeyko86,
I removed one of your messages as the content was exactly the same. I hope you don’t mind.
The error above seems to be that we are either not allowed to set the defined focus mode or that the defined focus mode is not available on your device.
11-21 23:55:53.494: W/ServiceManager(353): Permission failure: com.sonyericsson.permission.CAMERA_EXTENDED from uid=10263 pid=21766
11-21 23:55:53.494: W/ServiceManager(353): Permission failure: com.sonyericsson.permission.CAMERA_EXTENDED from uid=10263 pid=21766
11-21 23:55:53.495: E/cald(353): 14626221634316 excal_cam_ctrl.cpp (3338) 21831 E [EXC] [COMP] [CamCtrl 056] Not support autoFocus range[0x1] supported[0xe]
11-21 23:55:53.496: E/libcamera(353): setDynamicParameterValues: focus-mode unknown setting. (focus = 0, focusRange = 0)
I don’t have a Sony D5503 at hand to test that what you can do is to set the permission
Or try and debug your code by printing out the available focus modes to see if Sony numbers them differently.
W/Unity(16917): UnityPlayerNativeActivity has been deprecated, please update your AndroidManifest to use UnityPlayerActivity instead
It looks like that Unity deprecated the UnityPlayerNativeActvity which is used by the ARUnityPlayer. That mean that one would need to change the Java classes and derive from UnityPlayerActivity instead of UnityPlayerNativeActivity. I’m going to change that.
After replace UnityARPlayer.jar, and build the apk, have this error.
Any idea, what is wrong?
11-23 12:15:37.501 16095-16095/? D/TidaProvider: TidaProvider()
11-23 12:15:37.605 16095-16095/? W/Unity: UnityPlayerNativeActivity has been deprecated, please update your AndroidManifest to use UnityPlayerActivity instead
11-23 12:15:37.661 16095-16095/? D/AndroidRuntime: Shutting down VM
11-23 12:15:37.662 16095-16095/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.teszt.demo, PID: 16095
java.lang.Error: FATAL EXCEPTION [main]
Unity version : 5.4.2f2
Device model : Xiaomi Redmi 3S
Device fingerprint: Xiaomi/land/land:6.0.1/MMB29M/6.9.22:user/release-keys
Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v4/content/ContextCompat;
at org.artoolkit.ar.unity.UnityARPlayerActivity.onCreate(UnityARPlayerActivity.java:127)
at android.app.Activity.performCreate(Activity.java:6303)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2376)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2483)
at android.app.ActivityThread.access$900(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1349)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5441)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)
Caused by: java.lang.ClassNotFoundException: Didn’t find class “android.support.v4.content.ContextCompat” on path: DexPathList[[zip file “/data/app/com.teszt.demo-1/base.apk”],nativeLibraryDirectories=[/data/app/com.teszt.demo-1/lib/arm, /data/app/com.teszt.demo-1/base.apk!/lib/armeabi-v7a, /vendor/lib, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
at org.artoolkit.ar.unity.UnityARPlayerActivity.onCreate(UnityARPlayerActivity.java:127)
at android.app.Activity.performCreate(Activity.java:6303)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2376)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2483)
at android.app.ActivityThread.access$900(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1349)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5441)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)
Suppressed: java.lang.ClassNotFoundException: android.support.v4.content.ContextCompat
at java.lang.Class.classForName(Native Method)
at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
… 14 more
Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available
Hi, it is a bit of a pain to read stack traces in comments :(. I need to find a solution for that.
As for this error. It looks like that Unity deprecated the UnityPlayerNativeActvity which is used by the ARUnityPlayer. That mean that one would need to change the Java classes and derive from UnityPlayerActivity instead of UnityPlayerNativeActivity. I’m going to change that.
Hello, i managed to get it working without any errors. I replaced my UnityARPlayer with the one you put in the bonus section. I also added the code that you’ve added in the ARController (maybe the errors are happening because people are just replacing the .jar file and not the actual code inside ARController). Unfortunately, my screen resolution is soooooooo bad and pixelated..its like having a 50×50 and stretch it on a 1280×720 screen resolution. Also, the auto-focus didnt seem to work in the new jar file.
So my question is, why is the auto-focus not working (i just simply wait for the auto focus to focus.. or tap the screen..but nothing happens) and how can i fix my pixelated camera ? Note that i did change the values.xml to 1280×720 as well.
Ok a little update, it seems like the auto-focus is actually working, i just simply didn’t notice it because of the pixelised blurriness! but the picture resolution and the aspect ratio is completely wrong.. if someone has any idea how to fix it on android that would be awesome.. if we fix it we can then post a complete working example!
Here’s a preview of my values.xml file since it’s the only file i know of that can change the resolution on android phones:
–
–
Rear camera
–
1280×720 (16:9)
–
0
–
1280×720
ARTK_for_Unity_on_Android_Lib
Settings
Camera settings
1280×720
Change camera
Camera resolution
As you can see i changed everything to 1280×720 and the ratio to 16:9, but when run..its really blurry and the picture seems to be squashed..the ratio is not working properly.
Hi, unfortunately making changes to the values.xml file won’t work as I hard-coded the selection of the highest resolution in the code:
https://github.com/ThorstenBux/arunity5/blob/communitySupport/src/Android_Unity_Player_Source/AndroidStudioProj/UnityARPlayer/src/main/java/org/artoolkit/ar/unity/CameraSurface.java#L159
But it looks like not all phone handle value ‘0’ as the highest resolution. Looks like that was not a production proof solution. Sorry for that.
The reason I did it that way is because otherwise I would have had to make changes to the Android_Unity_Res_Lib_AS_Proj because currently ARToolKit5 handles all the preferences and settings using this project. https://github.com/ThorstenBux/arunity5/tree/communitySupport/src/Android_Unity_ResLib_AS_Proj
I try and find a better solution for selecting the highest resolution.
See section ‘Update 1’ for a possible solution.
I still have error with this new .jar file.
Any idea what do i do wrong?
11-27 07:37:26.561 15820-15820/com.example.demo E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.demo, PID: 15820
java.lang.Error: FATAL EXCEPTION [main]
Unity version : 5.4.2f2
Device model : ECOO ECOO_E05
Device fingerprint: alps/full_bd6753_65u_a_l1/bd6753_65u_a_l1:5.1/LMY47D/1447819751:user/dev-keys
Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v4/content/ContextCompat;
at org.artoolkit.ar.unity.UnityARPlayerActivity.onCreate(UnityARPlayerActivity.java:127)
at android.app.Activity.performCreate(Activity.java:6113)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1112)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2481)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2608)
at android.app.ActivityThread.access$800(ActivityThread.java:178)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5637)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
Caused by: java.lang.ClassNotFoundException: Didn’t find class “android.support.v4.content.ContextCompat” on path: DexPathList[[zip file “/data/app/com.example.demo-1/base.apk”],nativeLibraryDirectories=[/data/app/com.example.demo-1/lib/arm, /vendor/lib, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
at org.artoolkit.ar.unity.UnityARPlayerActivity.onCreate(UnityARPlayerActivity.java:127)
at android.app.Activity.performCreate(Activity.java:6113)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1112)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2481)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2608)
at android.app.ActivityThread.access$800(ActivityThread.java:178)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5637)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
Suppressed: java.lang.ClassNotFoundException: android.support.v4.content.ContextCompat
at java.lang.Class.classForName(Native Method)
at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
… 15 more
Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available
You need to integrate the library support-v4-23.3.0.aar into your Unity project. The path to this file is the same as in the Git repository:
ContextCompat
Do you have that in your project?
Let me know if that helps
So, it is working but i find some bug. With the latest UnityARPlayer.jar the camera is lagging with lower categories phone.
The biggest problem is, the model (cube) is distort, depends on the camera rotation. (just turned around the phone portrait/landscape)
Sometimes the cube height is half, or sometimes almost double.
The basic screen/camera ratio was 4:3, but this UnityARPlayer.jar the screen/camera ratio is changed, it is in full screen now.
Any idea what to do with this?
Hi Hilo,
the fullscreen goes together with the highest resolution as the resolution from your camera mostly matches the highest resolution of your display. That it lacks on older phones is to be expected as they lack the power of processing high-resolution camera streams together with augmented reality.
For low resolution phones you can go and change the code here: https://github.com/ThorstenBux/arunity5/blob/communitySupport/src/Android_Unity_Player_Source/AndroidStudioProj/UnityARPlayer/src/main/java/org/artoolkit/ar/unity/CameraSurface.java#L157
to select a lower resolution.
As for the rotation support. I merged in the branch from here: https://github.com/artoolkit/arunity5/tree/development-implement-device-rotation-support which seemed to have the rotation support integrated. As it looks it might not have been finished yet.
Hi,
Thank you for the detailed solution. It worked for me. you help me save a lot of time :). @SHARBEL has mentioned it right that apart from changing the UnityARPlayer jar file, The AR Controller code has to be updated too. Also, 1 more thing which I was facing but have resolved is : other might have to take a note is to update the files in “Assets\Plugin\Android” with compatible files provided in “arunity5-master\src\Unity\Assets\Plugins\Android” . As @SERGEYKO86 had missed the file : “support-v4-23.3.0.aar” in Android folder, there are chances that some might also miss “libs” folder which has updated code for Camera.
So, AutoFocus is working fine for me but issue I face now is, Android App has stopped detecting the marker whereas if I use the webcam then the markers are instantaneously detected. Any suggestions if I am missing something?
Its all working great! the new Jar file in the UPDATE 1 section works great…great resolution + autofocus!
Just make sure guys that you don’t only replace the Jar file, but also update the ARController that THOR_BUX has on his github! copy paste the missing code and integrate the library support-v4-23.3.0. That should be all i hope! it worked for me
Also make sure that the Plugins/Android/AndroidManifest has the same bundle name as in your player settings
Hi @SHARBEL,
I have covered all points you mentioned and did it again, Also, my camera has been tagged as “Main Camera”.
Upon creating a android build I do get the camera and auto-focus also works (as I can see the Focus happening on Text of image), but the camera has stopped detecting the marker/image.
I am using NFT Database image in AR Marker, and AR-Foreground and AR-Background layers have also been assigned properly.
Have you tried NFT database after using Auto-foucs too?
@THOR_BUX: Can you please share if something is being missed?
Hi Amit,
I haven’t tried with NFT yet. Will do so soon and let you know.
Hi Amit, I am also facing this issue. Camera gets started but the markers are not getting detected. Did you found any solution?
@SHARBEL : have you tried NFT Database based tracked after implementing auto-focus?
The solution Thor_Box provided for AutoFocus works like charm but even after spending more than 24 hours, unable to find why camera has stopped tracking the marker/image.
Hi Amit,
it looks like the NFT tracker in ARToolKit5 is not optimized for high video resolution. It does track sometimes but most of the times it does not. If you need NFT tracking you are better of reducing the video resolution and go for 720p probably.
where can i set this fixed resolution correct in your Code ?
Hi JO12,
here is the link where I iterate over all available resolutions and set the camera to the highest resolution available.
https://github.com/ThorstenBux/arunity5/blob/communitySupport/src/Android_Unity_Player_Source/AndroidStudioProj/UnityARPlayer/src/main/java/org/artoolkit/ar/unity/CameraSurface.java#L157
If you aim to set the resolution manually you can do that here and remove the loop:
https://github.com/ThorstenBux/arunity5/blob/communitySupport/src/Android_Unity_Player_Source/AndroidStudioProj/UnityARPlayer/src/main/java/org/artoolkit/ar/unity/CameraSurface.java#L157
Just be aware that your devices need to support the set resolution :).
Let me know if that works for you
Hey, the loop was gone to far. It ends up with a Resolution 2560×1440, which is not supported by my Device(LG G2). I set it fix to 1280×720 and it worked. Thanks for the tip.
I really need help here. Resolution is too high for my phone to run it decently, I just don’t know how to remove the loop and set a fixed resolution. How should the code look?
It works on Lallipop 5.1 sony z1 compact in landscape orientation
Thanks!!
I didnt see perspetive.
https://drive.google.com/file/d/0B-6QygUJIqf6MFlmWExOdVQ5WmlwTF9TaWtVOEtuVDVaOTg0/view?usp=sharing
If i change libARWrapper.so from your git (plugins/libs/). Projection is good but dont see video
https://drive.google.com/open?id=0B-6QygUJIqf6RGRJYXBVaGcxX28
I have save problem, Did you have any solutions?
If ucheck Use native GL texturing I see good picture and good marker object perspective.
But very very slow rendering video and object.
Can you help me?
I am facing zoom issue at start of Application. Whenever I start my app after unity splash screen the camera zooms for 30-40 seconds and then gets normal. The video is also jittering a lot.
Thank you for this.
When i change to higher camera resolution unity scene and video are not in same perspective. I guess it is aspect ratio problem. Where can i chanege that? ty
Hi, is it possible to change the “FOCUS_MODE_CONTINUOUS_PICTURE” to “FOCUS_MODE_FIXED” inside CameraSurface.java on runtime from Unity?
Hi zidaine,
the easy and general answer is yes. But you would need to enable that manually using a C# function and call Java from that:
I’m happy to help. Ust let me know if you need anything.
Hi everyone!
I tried the guide and it works great!! Thanks!!
this were my steps:
download the UnityARPlayer.jar (Update 2) and replace it on Assets/Android/Plugins/Android
Copy the code of ARController.cs from GitHub to your ARController.cs(not ARControllerEditor.cs) as it.
Open your manifest file of Assets/Android/Plugins/Android and check that your package name is the same as shows in the Unity editor.
And that’s all! Autofocus, FullHD Video working (on moto g HD Video Only) and Landscape rotation working.
Tested on Xiaomi redmi note 3 pro (kate)(android 6.0.1 and android 7.1.1Custom rom), moto g 1st gen(android 4.4). and Xperia M4(android 6.0.1)
using the ARToolKit 5.3.2.unitypackage version
Hi, I did follow all the instructions on two different phones :
The first one, a Nexus 5X on latest 7.1.1 Android, just displays a white screen with the name of the application and nothing else happens.
The second one, a cheap-ass Android phone on 4.4.2 Android just crashes.
What I’m trying to do is build the SimpleScene example with auto resolution and auto focus enabled, which seems to be the way that it should work. All that i’m able to do is build the SimpleScene with the separate application to set up the camera. However, even with the resolution put to the maximum, the image is blurry due to the lack of focus.
What I did is download ARUnity, import it into my project, change ARController.cs and UnityARPlayer.jar with the updated ones. Did anyone succeed with this method on a 7.1.1 Android phone or did I miss a step ?
I’ll put aside all the previous issues I had from now. If I succeed in the (little) thing I’d like to do, I will open tickets on GitHub to solve these issues.
This defintely works!
Tried it on One plus 3T & video stream is at full HD resolution, autofocus works as well. However, marker detection is not flawless. Tracking is lost quite often & is not reliable. Is it because, very high resolution video feed is processed & that is causing problems with marker detection? Perhaps that is why it is advisable to have slightly low res video feed. Thanks for this changelist though 🙂
Hi, yes a high resolution causes the marker detection and tracking to be not that reliable anymore. Better would be to use a reduced video resolution.
Thank you for this.
When i change to higher camera resolution unity scene and video are not in same perspective. I guess it is aspect ratio problem ?
Hi! Thanks for your help!, it worked to me but i want to know how to set up a medium video resolution, maybe 720p to have a 16:9 resolution, but a good tracking. can you explain us in a easy way to understand pls? Thx!
You could just add the fixed resolution to this line of code
parameters.setPreviewSize(highestPreviewSize.width,highestPreviewSize.height);
Eg:
parameters.setPreviewSize(1280,720)
And don’t forget to recompile 🙂
how about in unity ?
first time using unity and i dont know what i must do
You cannot change the resolution and autofocus from Unity. You must edit the Android-Plugin that is used by Unity.
but, how ?
As described above
in c# language, please help me
What do you mean?
Hello Thorsten,
i added changes like you did.
After I installed the app on my Epson BT-300 this error follows:
Skipped rendering frame because GfxDevice is in invalid state (device lost)
[EGL] Failed to create window surface: EGL_BAD_ALLOC: EGL failed to allocate resources for the requested operation.
What do I need to change?
Thanks in advance
Hi Markus,
unfortunately, I don’t have a BT-300 to test with. That is why I fear I cannot help you to get it working with this device. I’d love to get my hands on one, though.
Thank you for this.
When i change to higher camera resolution unity scene and video are not in same perspective. I guess it is aspect ratio problem ?
I don’t have experience in Android development anyway, and I’m not familiar with the build system for the UnityARPlayer project.
Hello Thorsten,
Thank you very much for this article and I have succeed by replacing the UnityARPlayer.jar from the update2 and change the package name in AndroidManifest.xml, then I downloaded the support-v4-23.3.0.aar from your GitHub repository (guys you can find it here).
It works perfectly except two bad effects in my case:
– In the highest resolution, the FPS of my Nexus 5 is very low and I could see the refresh one line scanning by another on the screen.
– In my case it’s an NFT project so after replacing the UnityARPlayer.jar file, my program didn’t have the functionality for the pattern-tracking.
For the first point, I think I need to build the .jar file myself and try to reduce a little bit of the resolution myself. But for the second one I don’t know actually what to do. So if any one has any solution for the NFT part, pls leave a message here or send me an email: zuokun.ouyang@etu.univ-orleans.fr
Thanks a lot!!!!
Same problem here, resolution is perfect and auto focus works even better as expected.
Such a shame none of my NFT markers appear after the update.
Hi, what do you mean by no NFT markers appear? Are they not trackable anymore?
I think YAROLSAV means when using an NFT marker for tracking, his program doesn’t recognize it.
And same problem for me.
I had the same problem. when the resolution is low, the NFT marker can be recognized. but now, the NFT marker is loss.
how to solve this problem?
Great tutorial @THOR_BUX thanks for sharing and updating it too.
Do you have any thoughts on how to improve the resolution and autofocus for iOS devices or am I missing something obvious in your tutorial?
In case you are interested I’m working on AR enhanced games for blind and visually impaired and look forward to sharing any break throughs. Good luck with everything you are doing and thanks again for sharing your code.
Hi,
thank you. I’d love to collaborate with you on your projects. Just let me know how that might work.
And, no, you haven’t overlooked anything. I didn’t publish any code for the iOS plugin.
Kind regards
Thorsten Bux
OK great look forward to working out a way to work together. I’m just sat here trying to get our prototype app so that the camera is the right way up on an iphone. anyhow you can reach me via audazzle or urfx at audazzle dot com .
got the orientation solved by setting this in the start method of ARController.cs
SetContentForScreenOrientation(false);
before StartAR .
off to get some kip