-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
30 lines (26 loc) · 939 Bytes
/
test.js
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
var key = "";
var json = '{"requests":[{"image":{"content":'+ getBase64Image(img) +'},"features":[{"type":"LABEL_DETECTION","maxResults":5}]}]}'
$.ajax({
url: "https://vision.googleapis.com/v1/images:annotate?key=" + key,
type: "GET",
dataType: 'json',
contentType: 'application/json',
data: {data: json},
success: function (data) {
console.log(data);
$.cookie('imageTags', JSON.stringify(data));
result = JSON.stringify(data);
},
error: function(data){
console.log("AJAX error: " + data);
}
});
function getBase64Image(imgElem) {
var canvas = document.createElement("canvas");
canvas.width = imgElem.clientWidth;
canvas.height = imgElem.clientHeight;
var ctx = canvas.getContext("2d");
ctx.drawImage(imgElem, 0, 0);
var dataURL = canvas.toDataURL("image/png");
return dataURL.replace(/^data:image\/(png|jpg);base64,/, "");
}