photo1.html 4.0 KB

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