Tôi đang làm việc trên một dự án với MVC ASP.Net 4 HTML5 (trình duyệt mặc định là google-chrome v29.0.1547.57) Tôi có thể tương tác với các công cụ này và chụp ảnh nhưng chỉ với camera trước, Làm thế nào tôi có thể kích hoạt camera sau? các đặc trưng của Tablet: Samsung Galaxy Tab 2 Tôi hy vọng bạn có thể giúp tôiBật camera sau với HTML5
Trả lời
Check-out https://simpl.info/getusermedia/sources/ cho thấy làm thế nào bạn có thể chọn nguồn sử dụng
MediaStreamTrack.getSources(gotSources);
Sau đó bạn có thể chọn nguồn và vượt qua nó trong khi tùy chọn vào getUserMedia
var constraints = {
audio: {
optional: [{sourceId: audioSource}]
},
video: {
optional: [{sourceId: videoSource}]
}
};
navigator.getUserMedia(constraints, successCallback, errorCallback);
Nó bây giờ là hoàn toàn có sẵn trong Chrome Stable và điện thoại di động (Tính đến v30)
Bản trình diễn có thể được tìm thấy tại https://webrtc.github.io/samples/src/content/devices/input-output/. Điều này sẽ cho phép truy cập vào cả camera phía trước và phía sau.
Nhiều demo mà bạn sẽ tìm thấy dựa trên chức năng chấp nhận:
MediaStreamTrack.getSources()
Tính đến phiên bản Chrome 45 và FireFox 39, bạn sẽ cần phải sử dụng các chức năng:
MediaDevices.enumerateDevices()
Ví dụ:
if (!navigator.mediaDevices || !navigator.mediaDevices.enumerateDevices) {
console.log("enumerateDevices() not supported.");
return;
}
// List cameras and microphones.
navigator.mediaDevices.enumerateDevices()
.then(function(devices) {
devices.forEach(function(device) {
console.log(device.kind + ": " + device.label +
" id = " + device.deviceId);
});
})
.catch(function(err) {
console.log(err.name + ": " + error.message);
});
tài liệu hơn nữa có thể được tìm thấy ở đây: https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/enumerateDevices
Cũng có thể đáng nói đến khi Chrome chỉ bật máy ảnh trên kết nối https. –
//----------------------------------------------------------------------
// Here we list all media devices, in order to choose between
// the front and the back camera.
// videoDevices[0] : Front Camera
// videoDevices[1] : Back Camera
// I used an array to save the devices ID
// which i get using devices.forEach()
// Then set the video resolution.
//----------------------------------------------------------------------
navigator.mediaDevices.enumerateDevices()
.then(devices => {
var videoDevices = [0,0];
var videoDeviceIndex = 0;
devices.forEach(function(device) {
console.log(device.kind + ": " + device.label +
" id = " + device.deviceId);
if (device.kind == "videoinput") {
videoDevices[videoDeviceIndex++] = device.deviceId;
}
});
var constraints = {width: { min: 1024, ideal: 1280, max: 1920 },
height: { min: 776, ideal: 720, max: 1080 },
deviceId: { exact: videoDevices[1] }
};
return navigator.mediaDevices.getUserMedia({ video: constraints });
})
.then(stream => {
if (window.webkitURL) {
video.src = window.webkitURL.createObjectURL(stream);
localMediaStream = stream;
} else if (video.mozSrcObject !== undefined) {
video.mozSrcObject = stream;
} else if (video.srcObject !== undefined) {
video.srcObject = stream;
} else {
video.src = stream;
}})
.catch(e => console.error(e));
hi Lightning2050 tôi có thể nhận được mã đầy đủ làm thế nào để chuyển đổi nguồn tôi đã thử nhưng nó không làm việc !! cảm ơn trước –
Hey! Kiểm tra điều này: [link] (https: // stackoverflow.com/a/45130523/7197142) –
@ Nahdiroviç Bạn có lẽ có bất kỳ html nào để đi với điều đó không? JS chỉ không làm việc cho tôi trên chrome di động. –
Thời gian qua tôi developped mã, soo đây là phiên bản mà tôi sử dụng: bạn gọi trực tiếp các chức năng whichCamera trong mã của bạn và bạn specefic mà camera "người dùng ", 'môi trường' hay 'máy tính'" nếu bạn đang runing trong một máy tính)
`//----------------------------------------------------------------------
// whichCamera(Type)
// For smartphone or tablet :
// Start the type={user,environment} camera.
// For computer it's simple :
// type = "computer".
//----------------------------------------------------------------------
var streamSrc, cameraType;
function whichCamera(type){
var cameraFacing;
cameraType = type;
if(type == "user")
cameraFacing = 0;
else if(type == "environment")
cameraFacing = 1;
else if(type == "computer"){
cameraFacing = 2;
}
console.log(type+" index : "+cameraFacing);
// Here we list all media devices, in order to choose between
// the front and the rear camera.
// videoDevices[0] : user Camera
// videoDevices[1] : environment Camera
// Then set the video resolution.
navigator.mediaDevices.enumerateDevices()
.then(devices => {
var videoDevices, videoDeviceIndex, constraints;
// Initialize the array wich will contain all video resources IDs.
// Most of devices have two video resources (Front & Rear Camera).
videoDevices = [0,0];
// Simple index to browse the videa resources array (videoDevices).
videoDeviceIndex = 0;
// devices.forEach(), this function will detect all media resources (Audio, Video) of the device
// where we run the application.
devices.forEach(function(device) {
console.log(device.kind + ": " + device.label +
" id = " + device.deviceId);
// If the kind of the media resource is video,
if (device.kind == "videoinput") {
// then we save it on the array videoDevices.
videoDevices[videoDeviceIndex++] = device.deviceId;
console.log(device.deviceId+" = "+videoDevices[videoDeviceIndex-1]);
}
});
console.log("Camera facing ="+cameraFacing+" ID = "+videoDevices[videoDeviceIndex-1]);
// Here we specified which camera we start,
// videoDevices[0] : Front Camera
// videoDevices[1] : Back Camera
if(cameraFacing != "computer"){
constraints = { deviceId: { exact: videoDevices[cameraFacing] }};
return navigator.mediaDevices.getUserMedia({ video:
constraints,
width: { min: 1280, ideal: 1600, max: 1920 },
height: { min: 720, ideal: 1200, max: 1080 }
}
);
}else
return navigator.mediaDevices.getUserMedia({ video: true });
})
// Then we retrieve the link to the video stream.
.then(stream => {
if (window.webkitURL) {
video.src = window.webkitURL.createObjectURL(stream);
localMediaStream = stream;
console.log(localMediaStream +" = "+ stream)
} else if (video.mozSrcObject !== undefined) {
video.mozSrcObject = stream;
console.log(video.mozSrcObject +" = "+ stream)
} else if (video.srcObject !== undefined) {
video.srcObject = stream;
console.log(video.srcObject +" = "+ stream)
} else {
video.src = stream;
console.log(video.src +" = "+ stream)
}
streamSrc = stream;
})
.catch(e => console.error(e));
}
trong Chrome trên Samsung S8 của tôi, tôi có thể sử dụng 'facingMode' = 'môi trường' để lấy video từ 'máy ảnh phía sau' . Giá trị mặc định có vẻ là "user" (các 'trước' máy ảnh)
trong nguyên cảo:
const video = document.getElementById("video");
const constraints = {
advanced: [{
facingMode: "environment"
}]
};
navigator.mediaDevices
.getUserMedia({
video: constraints
})
.then((stream) => {
video.src = window.URL.createObjectURL(stream);
video.play();
});
- 1. bên 2D di chuyển camera trong html5
- 2. Android 4 - Camera White Balancing dừng sau autoFocus
- 3. Sử dụng cả camera trước và sau đồng thời android
- 4. Bật PHP không hoạt động sau khi bật OSX Lion
- 5. Chỉ cho phép truy cập vào thiết bị camera trong HTML5
- 6. Làm cách nào để chọn đúng camera để bật đèn pin, trong Android 2.2?
- 7. Plupload Html5 xem trước sau Fileselect
- 8. Qt với HTML5 hoặc không có HTML5
- 9. HTML5 so với XFBML? Trên trang HTML5?
- 10. Triển khai camera IP
- 11. Hướng dẫn camera AVFoundation
- 12. Android: Unregister nút camera
- 13. UIImagePickerController với nguồn camera: video cắt tỉa không hoạt động
- 14. Video HTML5 với Brightcove
- 15. Android Fail để kết nối với dịch vụ camera
- 16. Cắt camera Android
- 17. Xamarin.Mobile iPad Camera Size
- 18. AVCaptureDevice Camera Zoom
- 19. ALAssetsLibrary được Camera Roll
- 20. pixel camera xoay
- 21. Android Camera Surface View
- 22. tạo lại đối tượng máy ảnh sau khi lỗi 100 (máy ảnh camera đã chết)
- 23. Three.js Di chuyển camera dọc theo đường dẫn sau sự kiện nhấp
- 24. Xoay camera xung quanh đối tượng với Three.js
- 25. Windows Mobile: sử dụng camera của điện thoại với C#
- 26. Truy cập điều khiển camera USB với AForge
- 27. iPhone 4 AVFoundation: Chụp từ camera trước và sau cùng một lúc
- 28. android: camera onPause/onResume issue
- 29. Phát sóng Camera Android Video
- 30. Cách phát hành camera sau khi hoạt động kết thúc trong Android?
Kinlan Tôi đã thử các phiên bản beta (google chrome) và nó hoạt động tuyệt vời , Tôi hy vọng phiên bản cuối cùng này sẽ sớm cập nhật trình duyệt của tôi nhờ mẹo. – raranibar
Lưu ý rằng getSources hiện không còn được dùng nữa. https://www.chromestatus.com/feature/4765305641369600 – Brad
Vâng, tôi cần cập nhật thông tin này. – Kinlan