1
|
cordova plugin add cordova-plugin-camera
|
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
|
<!DOCTYPE html>
<html>
<head>
<title>Capture Photo</title>
<meta http-equiv=
"Content-type"
content=
"text/html; charset=utf-8"
>
<script type=
"text/javascript"
charset=
"utf-8"
src=
"cordova.js"
></script>
<script type=
"text/javascript"
charset=
"utf-8"
>
var
destinationType;
document.addEventListener(
"deviceready"
,onDeviceReady,
false
);
//Cordova加載完成會觸發
function
onDeviceReady() {
destinationType=navigator.camera.DestinationType;
}
//拍照
function
capturePhoto() {
//拍照並獲取Base64編碼的圖像(quality : 存儲圖像的質量,範圍是[0,100])
navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 50,
destinationType: destinationType.DATA_URL }
);
}
//拍照成功
function
onPhotoDataSuccess(imageData) {
console.log(imageData);
var
smallImage = document.getElementById(
'smallImage'
);
smallImage.style.display =
'block'
;
smallImage.src =
"data:image/jpeg;base64,"
+ imageData;
}
//拍照失敗
function
onFail(message) {
alert(
'拍照失敗: '
+ message);
}
</script>
</head>
<body style=
"padding-top:50px"
>
<button style=
"font-size:23px;"
onclick=
"capturePhoto();"
>拍攝照片</button> <br>
<img style=
"display:none;width:240px;height:320px;"
id=
"smallImage"
src=
""
/>
</body>
</html>
|
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
|
<!DOCTYPE html>
<html>
<head>
<title>Capture Photo</title>
<meta http-equiv=
"Content-type"
content=
"text/html; charset=utf-8"
>
<script type=
"text/javascript"
charset=
"utf-8"
src=
"cordova.js"
></script>
<script type=
"text/javascript"
charset=
"utf-8"
>
var
destinationType;
document.addEventListener(
"deviceready"
,onDeviceReady,
false
);
//Cordova加載完成會觸發
function
onDeviceReady() {
destinationType=navigator.camera.DestinationType;
}
//拍照並編輯
function
capturePhotoEdit() {
//拍照並獲取Base64編碼的圖像(quality : 存儲圖像的質量,範圍是[0,100])
//allowEdit: true 拍照完畢後容許簡單編輯
navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 20,
allowEdit:
true
,
destinationType: destinationType.DATA_URL });
}
//拍照成功
function
onPhotoDataSuccess(imageData) {
console.log(imageData);
var
smallImage = document.getElementById(
'smallImage'
);
smallImage.style.display =
'block'
;
smallImage.src =
"data:image/jpeg;base64,"
+ imageData;
}
//拍照失敗
function
onFail(message) {
alert(
'拍照失敗: '
+ message);
}
</script>
</head>
<body style=
"padding-top:50px"
>
<button style=
"font-size:23px;"
onclick=
"capturePhotoEdit();"
>拍照並編輯</button> <br>
<img style=
"display:none;width:240px;height:240px;"
id=
"smallImage"
src=
""
/>
</body>
</html>
|