-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencode_decode3.html
159 lines (135 loc) · 3.5 KB
/
encode_decode3.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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>图片</title>
<style>
img{ max-width: 100%; max-height: 100%;}
section{
display: flex;
width: 90%;
padding: 30px 0;
margin: 0 auto;
align-items: center;
align-content: space-between;
}
canvas{
max-height: 100%;
max-width: 100%;
}
section>div{
width: 50%;
}
</style>
</head>
<body>
<section>
<div>
<img src="" id="photo" alt="">
</div>
<div>
<img src="" id="photo2" alt="">
<!-- <canvas></canvas> -->
</div>
</section>
<input type="file" id="bbb">
<script>
const box = {}
async function encodeImage() {
// const cs = document.querySelector('canvas');
const cs = document.createElement('canvas');
const ctx = cs.getContext('2d');
const img = document.getElementById('photo');
cs.width = img.naturalWidth;
cs.height = img.naturalHeight;
let sw =0, sh=0, x, y;
console.log(cs.width, cs.height)
for(let i=4; i<Math.max(cs.width,cs.height); i++) {
if(cs.width%i == 0) {
sw = cs.width/i;
x = i;
if(sh){break}
}
if(cs.height%i == 0) {
sh = cs.height/i;
y = i;
if(sw){break}
}
};
ctx.drawImage(img,0,0);
let imgdataList = [], list=[];
for(let i=0; i<y; i++) {
for(let j=0; j<x; j++) {
imgdataList.push(ctx.getImageData(j*sw,i*sh,sw,sh));
list.push(j+i*x);
}
};
list = list.sort(() => 0.4 - Math.random())
ctx.fillRect(0,0,cs.width,cs.height);
for(let i=0, j=0; i<list.length; i++) {
if(i >= x && (i%x) == 0) {
j++;
}
ctx.putImageData(imgdataList[list[i]], sw*(i%x),sh*j);
}
// console.log(list.length, sw,sh, x,y);
const arr = ctx.getImageData(0,0,cs.width,cs.height).data;
const newBuffer = new ArrayBuffer(arr.length/4);
// const newBuffer = new ArrayBuffer(arr.length+list.length+1);
// const newarr = new Int8Array(newBuffer);
for(let i =0; i<arr.length/4; i++) {
// if(i>=arr.length){
// if(i>=arr.length+list.length) {
// newarr[i] = list.length;
// } else {
// newarr[i] = list[i-arr.length];
// }
// }else {
// old[i]= base64.charCodeAt(i);
// }
}
cs.toBlob(function(blob){
var urlCreator = window.URL || window.webkitURL;
var imageUrl = urlCreator.createObjectURL(blob);
var img = document.querySelector("#photo2");
img.src= imageUrl
},"image/png")
await showImage(arr, '#photo2')
}
function decodeImage() {
}
function showLocal(file,callback) {
var fileReader = new FileReader();
fileReader.onload = function(e) {
fileContent = e.target.result;
callback(fileContent);
}
fileReader.readAsArrayBuffer(file);
}
document.getElementById('bbb').addEventListener('change', function(e){
showLocal(e.target.files[0], async function(m){
await showImage(m, '#photo');
encodeImage()
})
}, false)
async function loadImage() {
const res = await fetch('./images/demo.jpg');
const ares = await res.arrayBuffer();
const oldit = new Int8Array(ares);
let ke = await showImage(oldit, '#photo');
encodeImage();
}
async function showImage(bf, id){
var blob = new Blob( [ bf ], { type: "image/png" } );
var urlCreator = window.URL || window.webkitURL;
var imageUrl = urlCreator.createObjectURL( blob );
var img = document.querySelector( id || "#photo" );
return new Promise(function(resolve){
img.onload = resolve;
img.src = imageUrl;
})
}
loadImage()
</script>
</body>
</html>