1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
|
---
title: Introduction to the Camera API
slug: Archive/B2G_OS/API/Camera_API/Introduction
translation_of: Archive/B2G_OS/API/Camera_API/Introduction
---
<p></p><section class="Quick_links" id="Quick_Links"><ol><li><strong><a href="/ko/docs/Web/API/Camera_API">Camera API</a></strong></li><li class="toggle"><details open><summary>Interfaces</summary><ol><li><a href="/ko/docs/Web/API/CameraCapabilities"><code>CameraCapabilities</code></a></li><li><a href="/ko/docs/Web/API/CameraControl"><code>CameraControl</code></a></li><li><a href="/ko/docs/Web/API/CameraManager"><code>CameraManager</code></a></li></ol></details></li><li class="toggle"><details open><summary>Methods</summary><ol><li><a href="/ko/docs/Web/API/Navigator/mozCameras"><code>Navigator.mozCameras</code></a></li></ol></details></li></ol></section><p></p>
<p><span class="seoSummary"><a href="/en-US/docs/Web/API/Camera_API">Camera API</a> 를 사용하면 디바이스의 카메라를 이용해 사진을 찍어 현재 웹페이지에 업로드하는것이 가능해집니다.</span> This is achieved through an <code>input</code> element with <code>type="file"</code> and an <code>accept</code> attribute to declare that it accepts images. The HTML looks like this:</p>
<pre class="brush: html"><input type="file" id="take-picture" accept="image/*">
</pre>
<p>When users choose to activate this HTML element, they are presented with an option to choose a file, where the device's camera is one of the options. If they select the camera, it goes into picture taking mode. After the picture has been taken, the user is presented with a choice to accept or discard it. If accepted, it gets sent to the <code><input type="file"></code> element and its <code>onchange</code> event is triggered.</p>
<h2 id="Get_a_reference_to_the_taken_picture">Get a reference to the taken picture</h2>
<p>With the help of the <a href="/en-US/docs/Using_files_from_web_applications">File API</a> you can then access the taken picture or chosen file:</p>
<pre class="brush: js">var takePicture = document.querySelector("#take-picture");
takePicture.onchange = function (event) {
// Get a reference to the taken picture or chosen file
var files = event.target.files,
file;
if (files && files.length > 0) {
file = files[0];
}
};
</pre>
<h2 id="Presenting_the_picture_in_the_web_page">Presenting the picture in the web page</h2>
<p>Once you have a reference to the taken picture (i.e., file), you can then use <a href="/ko/docs/Web/API/Window/URL/createObjectURL" title="The documentation about this has not yet been written; please consider contributing!"><code>window.URL.createObjectURL()</code></a> to create a URL referencing the picture and setting it as the <code>src</code> of an image:</p>
<pre class="brush: js">// Image reference
var showPicture = document.querySelector("#show-picture");
// Create ObjectURL
var imgURL = window.URL.createObjectURL(file);
// Set img src to ObjectURL
showPicture.src = imgURL;
// For performance reasons, revoke used ObjectURLs
URL.revokeObjectURL(imgURL);
</pre>
<p>If <code>createObjectURL()</code> isn't supported, an alternative is to fallback to <a href="/ko/docs/Web/API/FileReader" title="FileReader 객체는 웹 애플리케이션이 비동기적으로 데이터를 읽기 위하여 읽을 파일을 가리키는File 혹은 Blob 객체를 이용해 파일의 내용을(혹은 raw data버퍼로) 읽고 사용자의 컴퓨터에 저장하는 것을 가능하게 해줍니다."><code>FileReader</code></a>:</p>
<pre class="brush: js">// Fallback if createObjectURL is not supported
var fileReader = new FileReader();
fileReader.onload = function (event) {
showPicture.src = event.target.result;
};
fileReader.readAsDataURL(file);
</pre>
<h2 id="Complete_example">Complete example</h2>
<p>If you want to see it in action, take a look at the <a class="external" href="http://robnyman.github.com/camera-api/">complete working Camera API example</a>.</p>
<p>Here is the code used for that demo:</p>
<h3 id="HTML_page">HTML page</h3>
<pre class="brush: html"><!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Camera API</title>
<link rel="stylesheet" href="css/base.css" type="text/css" media="screen">
</head>
<body>
<div class="container">
<h1>Camera API</h1>
<section class="main-content">
<p>A demo of the Camera API, currently implemented in Firefox and Google Chrome on Android. Choose to take a picture with your device's camera and a preview will be shown through createObjectURL or a FileReader object (choosing local files supported too).</p>
<p>
<input type="file" id="take-picture" accept="image/*">
</p>
<h2>Preview:</h2>
<p>
<img src="about:blank" alt="" id="show-picture">
</p>
<p id="error"></p>
</section>
<p class="footer">All the code is available in the <a href="https://github.com/robnyman/robnyman.github.com/tree/master/camera-api">Camera API repository on GitHub</a>.</p>
</div>
<script src="js/base.js"></script>
</body>
</html>
</pre>
<h3 id="JavaScript_file">JavaScript file</h3>
<pre class="brush: js">(function () {
var takePicture = document.querySelector("#take-picture"),
showPicture = document.querySelector("#show-picture");
if (takePicture && showPicture) {
// Set events
takePicture.onchange = function (event) {
// Get a reference to the taken picture or chosen file
var files = event.target.files,
file;
if (files && files.length > 0) {
file = files[0];
try {
// Create ObjectURL
var imgURL = window.URL.createObjectURL(file);
// Set img src to ObjectURL
showPicture.src = imgURL;
// Revoke ObjectURL
URL.revokeObjectURL(imgURL);
}
catch (e) {
try {
// Fallback if createObjectURL is not supported
var fileReader = new FileReader();
fileReader.onload = function (event) {
showPicture.src = event.target.result;
};
fileReader.readAsDataURL(file);
}
catch (e) {
//
var error = document.querySelector("#error");
if (error) {
error.innerHTML = "Neither createObjectURL or FileReader are supported";
}
}
}
}
};
}
})();
</pre>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p></p><p class="warning"><strong><a href="https://github.com/mdn/browser-compat-data">We're converting our compatibility data into a machine-readable JSON format</a></strong>.
This compatibility table still uses the old format,
because we haven't yet converted the data it contains.
<strong><a href="/ko/docs/MDN/Contribute/Structures/Compatibility_tables">Find out how you can help!</a></strong></p>
<div class="htab">
<a id="AutoCompatibilityTable" name="AutoCompatibilityTable"></a>
<ul>
<li class="selected"><a>Desktop</a></li>
<li><a>Mobile</a></li>
</ul>
</div><p></p>
<div id="compat-desktop">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Chrome</th>
<th>Firefox (Gecko)</th>
<th>Internet Explorer</th>
<th>Opera</th>
<th>Safari</th>
</tr>
<tr>
<td>Camera API</td>
<td><span style="color: #f00;">No support</span></td>
<td><span style="color: #f00;">No support</span></td>
<td><span style="color: #f00;">No support</span></td>
<td><span style="color: #f00;">No support</span></td>
<td><span style="color: #f00;">No support</span></td>
</tr>
<tr>
<td><code><a href="/en-US/docs/Web/API/URL/createObjectURL">createObjectURL()</a></code></td>
<td>16</td>
<td><a href="/en-US/Firefox/Releases/8" title="Released on 2011-11-08.">8.0</a> (8.0)</td>
<td>10</td>
<td><span style="color: #f00;">No support</span></td>
<td><span style="color: #f00;">No support</span></td>
</tr>
<tr>
<td><a href="/ko/docs/Web/API/FileReader" title="FileReader 객체는 웹 애플리케이션이 비동기적으로 데이터를 읽기 위하여 읽을 파일을 가리키는File 혹은 Blob 객체를 이용해 파일의 내용을(혹은 raw data버퍼로) 읽고 사용자의 컴퓨터에 저장하는 것을 가능하게 해줍니다."><code>FileReader</code></a></td>
<td>16</td>
<td><a href="/en-US/Firefox/Releases/3.6" title="Released on 2010-01-21.">3.6</a> (1.9.2)</td>
<td>10</td>
<td>11.6</td>
<td><span style="color: #f00;">No support</span></td>
</tr>
</tbody>
</table>
</div>
<div id="compat-mobile">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Android</th>
<th>Chrome for Android</th>
<th>Firefox Mobile (Gecko)</th>
<th>IE Mobile</th>
<th>Opera Mobile</th>
<th>Safari Mobile</th>
</tr>
<tr>
<td>Camera API</td>
<td>3.0</td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
<td>10.0 (10.0)</td>
<td><span style="color: #f00;">No support</span></td>
<td><span style="color: #f00;">No support</span></td>
<td><span style="color: #f00;">No support</span></td>
</tr>
<tr>
<td><code><a href="/en-US/docs/Web/API/URL/createObjectURL">createObjectURL()</a></code></td>
<td>4</td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
<td>10.0 (10.0)</td>
<td><span style="color: #f00;">No support</span></td>
<td><span style="color: #f00;">No support</span></td>
<td><span style="color: #f00;">No support</span></td>
</tr>
<tr>
<td><a href="/ko/docs/Web/API/FileReader" title="FileReader 객체는 웹 애플리케이션이 비동기적으로 데이터를 읽기 위하여 읽을 파일을 가리키는File 혹은 Blob 객체를 이용해 파일의 내용을(혹은 raw data버퍼로) 읽고 사용자의 컴퓨터에 저장하는 것을 가능하게 해줍니다."><code>FileReader</code></a></td>
<td>3</td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
<td>10.0 (10.0)</td>
<td><span style="color: #f00;">No support</span></td>
<td>11.10</td>
<td><span style="color: #f00;">No support</span></td>
</tr>
</tbody>
</table>
</div>
|