AR.js icon indicating copy to clipboard operation
AR.js copied to clipboard

Back Camera on mobile - SOLVED!

Open hipstermojo opened this issue 9 years ago • 42 comments

I think I have a solution to the camera preference when using mobile.

The navigator.getUserMedia function is deprecated and you should instead use the newer MediaDevices.getUserMedia function. The link for the docs.

in your Threex.ArToolkitSource.js file at line 143 should then be changed to

var constraints = { video: { 
                   facingMode: "environment"  
                   mandatory: {
                                        maxWidth: _this.parameters.sourceWidth,
		                       maxHeight: _this.parameters.sourceHeight
		    		}
};

And if someone wants to instead use the 'selfie' camera they change "environment" to "user". You can even make a button to choose between the two.

However this might result in you changing a lot of code possibly

hipstermojo avatar Jun 01 '17 12:06 hipstermojo

ahhhh! great. i had some kludge there. i will incorporate that asap.

how does it work if i have only user facing camera ? e.g. on a laptop with the internal camera

jeromeetienne avatar Jun 03 '17 18:06 jeromeetienne

see #74

jeromeetienne avatar Jun 03 '17 18:06 jeromeetienne

if statements maybe, hehe 😂 . Since AR.js is primarily for bringing AR on the phone, it only makes sense to have this feature instead. Every phone has a back camera. So actually for laptops, that is only necessary during development so the developer can just manually configure that part out

hipstermojo avatar Jun 03 '17 18:06 hipstermojo

i cant get it to work here. im likely missing something

@curtello do you mind doing a pull request ?

jeromeetienne avatar Jun 04 '17 10:06 jeromeetienne

I'll get on it

hipstermojo avatar Jun 04 '17 12:06 hipstermojo

Adding to this, we can also pull out the camera devices from mobile and give the user an option to choose either front or back camera when the screen opens. I have tried this earlier and it works like magic.

MrRaam avatar Jun 04 '17 13:06 MrRaam

what do you mean by pull out?

hipstermojo avatar Jun 04 '17 13:06 hipstermojo

` function init() { var videoSelect = document.querySelector('select#videoSource'); var selectors = [videoSelect];

                         function gotDevices(deviceInfos) {
                                   // Handles being called several times to update labels. Preserve values.
                                                var values = selectors.map(function(select) {
                                                   return select.value;

                                                });
                selectors.forEach(function(select) {
                                       while (select.firstChild) {
                                        select.removeChild(select.firstChild);
                            }
                        });

Pull all the available cameras from the device ( PC , Tab , Laptop or mobiles )

for (var i = 0; i !== deviceInfos.length; ++i) {

                             var deviceInfo = deviceInfos[i];
                             var option = document.createElement('option');
                             option.value = deviceInfo.deviceId;

if (deviceInfo.kind === 'videoinput') {

 option.text = deviceInfo.label || 'camera ' + (videoSelect.length + 1);
                             videoSelect.appendChild(option);

} else {
                               console.log('Some other kind of source/device: ', deviceInfo);
                                     }}

                           selectors.forEach(function(select, selectorIndex) {
                                   if (Array.prototype.slice.call(select.childNodes).some(function(n) {
                                         return n.value === values[selectorIndex]    })) {
                                                        select.value = values[selectorIndex];  }  }); }
                                                                                                  
                                                                                                           

            navigator.mediaDevices.enumerateDevices().then(gotDevices).catch(handleError);


                     function gotStream(stream) {
                            window.stream = stream; // make stream available to console
                            video.srcObject = stream;
                             // Refresh button list in case labels have become available
                            return navigator.mediaDevices.enumerateDevices();
                       }

              function start() {
                          if (window.stream) {
                                      window.stream.getTracks().forEach(function(track) {
                                                      track.stop();
                                                                                   });
                                                                                                   }

var videoSource = videoSelect.value; var constraints = { video: { deviceId: videoSource ? { exact: videoSource } : undefined } }; navigator.mediaDevices.getUserMedia(constraints). then(gotStream).then(gotDevices).catch(handleError); }

        videoSelect.onchange = start;

        start();

choosecam `

MrRaam avatar Jun 04 '17 14:06 MrRaam

Awesome stuff man. I get you now. Will integrate it

hipstermojo avatar Jun 04 '17 14:06 hipstermojo

Thanks @curtello , It should work, It's working for my application. However, thinking from the end user perspective we can keep the back camera as default.

Note: if we use the above code there is no need for polyfill I believe.

MrRaam avatar Jun 04 '17 14:06 MrRaam

Just like I had said, the choice of camera wouldn't exactly make sense because ~99% of people will just want to use the back camera constantly so maybe only when building the project will the choosing of camera become necessary. Now the equivalent of this code in the AFrame implementation is what can bring problems, should the camera in use be the selfie camera.

hipstermojo avatar Jun 04 '17 14:06 hipstermojo

May I ask what you used to implement your app.

hipstermojo avatar Jun 04 '17 14:06 hipstermojo

Yes sometimes it depends upon the concept, some concept needs front cam and some don't. I used threejs and a bit of babylon initially to test out few things.

MrRaam avatar Jun 04 '17 15:06 MrRaam

and what web framework? or at least can I see your repository for the example

hipstermojo avatar Jun 04 '17 15:06 hipstermojo

Sure, I like your curiosity. Web frameworks being a bit of jquery, larval and others.. Let's keep this thread for this issue. Ping me out for other quiries.

MrRaam avatar Jun 04 '17 15:06 MrRaam

i wanted to use Express but wanted to know how other people went about the whole issue of making a whole web app out of it. Isn't pinging you being intrusive 😂 . are you on gitter? I don't see why this conversation needs to be publicized

hipstermojo avatar Jun 04 '17 15:06 hipstermojo

I wouldn't mind :) , yep gitter. can i see a working link of this demo in Ar.js? https://www.youtube.com/watch?v=EuF-k0ajsjk

MrRaam avatar Jun 04 '17 15:06 MrRaam

it's not yet done so i wanted to see other people's implementations

hipstermojo avatar Jun 04 '17 15:06 hipstermojo

@MrRaam could I get your email so I can contact you

hipstermojo avatar Jun 05 '17 15:06 hipstermojo

Sure, [email protected] On Jun 5, 2017 9:36 PM, "curtello" [email protected] wrote:

@MrRaam https://github.com/mrraam could I get your email so I can contact you

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/jeromeetienne/AR.js/issues/86#issuecomment-306220923, or mute the thread https://github.com/notifications/unsubscribe-auth/Ab0zlF96DxqW4J0ogPC8lzszg-g3jANKks5sBCDEgaJpZM4Ns62a .

MrRaam avatar Jun 05 '17 16:06 MrRaam