-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmask.js
123 lines (99 loc) · 4.1 KB
/
mask.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
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
console.log("mask.js")
const {ipcRenderer, clipboard, nativeImage} = require('electron');
const {getCurrentWindow} = require('@electron/remote');
var Tesseract = require('tesseract.js');
function blob_to_buffer(blob, callback) {
const file_reader = new FileReader()
file_reader.addEventListener("loadend", event => {
if (file_reader.error) {
callback(file_reader.error)
} else {
callback(null, Buffer.from(file_reader.result))
}
}, false)
file_reader.readAsArrayBuffer(blob)
return file_reader
}
var window = getCurrentWindow();
function mask(imageURL, type){
var img = document.getElementById( "target" );
img.src = imageURL;
console.log("doing")
Jcrop.load('target').then(img => {
const stage = Jcrop.attach('target',{
shade:true
});
stage.addClass('jcrop-ux-no-outline');
console.log("done")
console.log(stage)
stage.listen('crop.change',function(widget,e){
const pos = widget.pos;
console.log(pos.x,pos.y,pos.w,pos.h);
var x1 = document.getElementById("snackbarLoad");
x1.className = "show";
var cc = pos
var image = document.getElementById( "target" );
var heightScale = (image.naturalHeight/image.height)
var widthScale = (image.naturalWidth/image.width)
cc.x = cc.x * widthScale
cc.y = cc.y * heightScale;
cc.w = cc.w * widthScale;
cc.h = cc.h * heightScale;
var canvasElement = document.createElement("canvas");
canvasElement.width = Math.floor(cc.w);
canvasElement.height = Math.floor(cc.h);
var ctx = canvasElement.getContext("2d");
// console.log(image.naturalWidth, image.naturalHeight, image.width, image.height);
ctx.drawImage(image, cc.x, cc.y, cc.w, cc.h, 0, 0, canvasElement.width, canvasElement.height);
stage.destroy()
var imgUrl = canvasElement.toDataURL();
if(type==1){
canvasElement.toBlob(function(blob){
blob_to_buffer(blob, async(err, buffer) => {
if(err)console.log("err")
var native_image = nativeImage.createFromBuffer(buffer);
clipboard.writeImage(native_image)
var x = document.getElementById("snackbarText");
x.innerHTML = "Cropped image copied to clipboard..!!❤️"
x1.className = x1.className.replace("show", "");
x.className = "show";
setTimeout(function(){
x.className = x.className.replace("show", "");
window.close();
}, 1000);
})
})
}
else if(type==2){
var imgUrl = canvasElement.toDataURL();
Tesseract.recognize(
imgUrl,
'eng',
{ logger: m => console.log(m) }
).then(({ data: { text } }) => {
console.log(text);
clipboard.writeText(text)
var x = document.getElementById("snackbarText");
x.innerHTML = "Text copied to clipboard..!!❤️";
x1.className = x1.className.replace("show", "");
x.className = "show";
setTimeout(function(){
x.className = x.className.replace("show", "");
window.close();
}, 1000);
})
}
})
})
}
ipcRenderer.on('request-object', function (event, requestObject){
var imageUrl = requestObject.imageURL;
var type = requestObject.type;
console.log(requestObject)
if(type==1){
mask(imageUrl,1);
}
else if(type==2){
mask(imageUrl,2);
}
});