photo1.html 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Android相机调用演示</title>
  7. <style>
  8. body {
  9. font-family: 'Arial', sans-serif;
  10. display: flex;
  11. flex-direction: column;
  12. align-items: center;
  13. padding: 20px;
  14. /* background-color: #00f5f500; */
  15. }
  16. .container {
  17. max-width: 500px;
  18. width: 100%;
  19. background: white;
  20. padding: 20px;
  21. border-radius: 10px;
  22. box-shadow: 0 4px 6px rgba(0,0,0,0.1);
  23. }
  24. .btn {
  25. background: #4285f4;
  26. color: white;
  27. border: none;
  28. padding: 12px 20px;
  29. border-radius: 5px;
  30. font-size: 16px;
  31. cursor: pointer;
  32. width: 100%;
  33. margin: 10px 0;
  34. transition: background 0.3s;
  35. }
  36. .btn:hover {
  37. background: #3367d6;
  38. }
  39. .preview {
  40. width: 100%;
  41. max-height: 400px;
  42. object-fit: contain;
  43. margin: 20px 0;
  44. border: 1px dashed #ccc;
  45. display: none;
  46. }
  47. .status {
  48. margin: 10px 0;
  49. padding: 10px;
  50. border-radius: 5px;
  51. text-align: center;
  52. }
  53. .error {
  54. background: #ffebee;
  55. color: #c62828;
  56. }
  57. .success {
  58. background: #e8f5e9;
  59. color: #2e7d32;
  60. }
  61. </style>
  62. </head>
  63. <body>
  64. <div class="container">
  65. <h1>相机调用演示</h1>
  66. <p>点击下方按钮调用Android相机拍照</p>
  67. <button class="btn" id="openCameraBtn">打开相机</button>
  68. <div id="statusMsg" class="status" style="display: none;"></div>
  69. <img id="preview" class="preview" alt="拍摄预览">
  70. <input type="file" id="cameraInput" accept="image/*" capture="camera" style="display: none;">
  71. </div>
  72. <script>
  73. document.addEventListener('DOMContentLoaded', function() {
  74. const openCameraBtn = document.getElementById('openCameraBtn');
  75. const cameraInput = document.getElementById('cameraInput');
  76. const preview = document.getElementById('preview');
  77. const statusMsg = document.getElementById('statusMsg');
  78. openCameraBtn.addEventListener('click', function() {
  79. cameraInput.click();
  80. });
  81. cameraInput.addEventListener('change', function(e) {
  82. if (e.target.files && e.target.files[0]) {
  83. const file = e.target.files[0];
  84. if (!file.type.match('image.*')) {
  85. showStatus('请选择图片文件', 'error');
  86. return;
  87. }
  88. const reader = new FileReader();
  89. reader.onload = function(event) {
  90. preview.src = event.target.result;
  91. preview.style.display = 'block';
  92. showStatus('图片加载成功', 'success');
  93. };
  94. reader.onerror = function() {
  95. showStatus('图片读取失败', 'error');
  96. };
  97. reader.readAsDataURL(file);
  98. }
  99. });
  100. function showStatus(message, type) {
  101. statusMsg.textContent = message;
  102. statusMsg.className = 'status ' + type;
  103. statusMsg.style.display = 'block';
  104. setTimeout(function() {
  105. statusMsg.style.display = 'none';
  106. }, 3000);
  107. }
  108. });
  109. </script>
  110. </body>
  111. </html>