Back Camera on mobile - SOLVED!
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
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
see #74
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
i cant get it to work here. im likely missing something
@curtello do you mind doing a pull request ?
I'll get on it
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.
what do you mean by pull out?
` 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();
`
Awesome stuff man. I get you now. Will integrate it
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.
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.
May I ask what you used to implement your app.
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.
and what web framework? or at least can I see your repository for the example
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.
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
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
it's not yet done so i wanted to see other people's implementations
@MrRaam could I get your email so I can contact you
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 .