HTML5技术支持WebApp在手机上拍照,显示在页面上并上传到服务器。这是手机微博应用中常见的功能,当然你也可以在其它类型应用中适当使用此技术。
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
|
var
flag =
true
;
window.addEventListener(
"DOMContentLoaded"
,
function
() {
var
video = document.getElementById(
"video"
), canvas, context;
try
{
canvas = document.createElement(
"canvas"
);
canvas.width = 600;
canvas.height = 600;
context = canvas.getContext(
"2d"
);
}
catch
(e) { alert(
"not support canvas!"
);
return
; }
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;
if
(navigator.getUserMedia)
navigator.getUserMedia(
{
"video"
:
true
},
function
(stream) {
if
(video.mozSrcObject !== undefined)video.mozSrcObject = stream;
else
video.src = ((window.URL || window.webkitURL || window.mozURL || window.msURL) && window.URL.createObjectURL(stream)) || stream;
video.play();
},
function
(error) {
alert(
"请检查是否开启摄像头"
);
flag =
false
;
}
);
else
alert(
"Native device media streaming (getUserMedia) not supported in this browser"
);
setInterval(
function
() {
if
(!flag){
return
;
}
context.drawImage(video, 0, 0, canvas.width = video.videoWidth, canvas.height = video.videoHeight);
var
image = canvas.toDataURL(
"image/png"
).replace(
"data:image/png;base64,"
,
""
);
$.ajax({
url :
'qRCodeAction_decoderQRCode.action'
,
async :
false
,
type :
'post'
,
data : {
'time'
: (
new
Date()).toString(),
'img'
: image
},
success :
function
(result) {
},
});
}, 5000);
},
false
);
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
/**
* 解析二维码
*/
public
String decoderQRCode(){
try
{
String realpath = ServletActionContext.getServletContext().getRealPath(
"/file"
);
SimpleDateFormat sdf =
new
SimpleDateFormat(
"yyyyMMddHHmmss"
);
String imgName = sdf.format(
new
Date()) +
".png"
;
String filePath = realpath+Constants.SF_FILE_SEPARATOR+imgName;
OutputStream out =
new
FileOutputStream(filePath);
QRCode.GenerateImage(img,out);
//生成图片
message = QRCode.decoderQRCode(filePath);
}
catch
(Exception e) {
e.printStackTrace();
}
return
Action.SUCCESS;
}
|