Tuyệt Chiêu Thần Thánh: Chuyển đổi Hình ảnh Sang Text Bằng ...
Có thể bạn quan tâm

Hôm nay mình sẽ giới thiệu với mọi người cách chuyển đổi hình ảnh sang text bằng JavaScript. Và demo sẽ như sau:
Chuyển ảnh sang text:

Dưới đây là source code mình tìm được của bạn Ruanyu Jian:
Phần HTML lần lượt của việc chuyển từ ảnh sang text và chuyển từ video sang text:
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>Char Picture</title>
<style>
#show{
font-family: Courier New;
font-size: 10px;
line-height: 8px;
}
</style>
<script src="2.js"></script>
</head>
<body>
<input type="file" id="file"><button type="button" onclick="showImage()">Tạo</button><br>
<img src="" style="width: 100px"/>
<pre id="show"></pre>
<script>
var map=getCharsMap(),show=document.getElementById("show"),
img=document.getElementsByTagName("img")[0],
canvas = document.createElement("canvas");
function showImage(){
var file = document.getElementById('file').files[0],
ctx = canvas.getContext('2d'),
url = URL.createObjectURL(file);
if(!file){
alert("Vui lòng chọn tệp tin");
}
img.src = url;
img.onload=function(){
canvas.width=img.naturalWidth;
canvas.height=img.naturalHeight;
ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
show.innerText=toChars(ctx,canvas.width,canvas.height,100);
}
}
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"/>
<title>Char Video</title>
<style>
html, body {
width: 100%;
}
video {
margin: auto;
position: relative;
top: 0;
left: 0;
width: 20%;
height: 20%;
}
#stage {
margin: auto;
position: absolute;
top: 0;
left: 20%;
right: 0;
width: 80%;
font-family: Courier New;
font-size: 16px;
line-height: 10px;
}
#stage img {
width: 100%;
height: 100%;
}
</style>
<script src="2.js"></script>
</head>
<body>
<input type="file" id="file">
<button id="play" type="button" onclick="play()">Tạo</button>
<br>
<video controls="controls">
</video>
<!--<div id="stage"></div>-->
<pre id="stage"></pre>
<script type="text/javascript">
var interval, video = document.getElementsByTagName("video")[0],
stage = document.getElementById("stage"),
canvas = document.createElement("canvas"),
captureImage = function () {
var ctx;
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
if (canvas.width) {
ctx = canvas.getContext('2d');
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
stage.innerText = toChars(ctx, canvas.width, canvas.height, 100);
}
},
beginCapture = function () {
interval = setInterval(function () {
captureImage(1)
}, 100);
},
endCapture = function () {
if (interval) {
clearInterval(interval);
}
},
play = function () {
var file = document.getElementById('file').files[0];
var url = URL.createObjectURL(file);
if (!file) {
alert("Chọn tệp tin");
}
console.log(url);
video.src = url;
video.play();
};
video.addEventListener("play", beginCapture);
video.addEventListener("pause", endCapture);
video.addEventListener("ended", endCapture);
video.addEventListener("playing", function () {
endCapture();
beginCapture();
});
</script>
</body>
</html>
Và đây là phần chính của thuật toán, JavaScript:
/** * Created by Ruanyu Jian */ var map=getCharsMap(); /* * Chuyển hình ảnh thành text */ function toChars(context, width, height, rowChars) { var pixels = [], output = "", imageData = context.getImageData(0, 0, width, height), rowChars = width < rowChars ? width : rowChars, char_h = width / rowChars, char_w = char_h, rows = height / char_h, cols = rowChars, getBlockGray = function (x, y, w, h) { var sumGray = 0, pixels; for (var row = 0; row < w; row++) { for (var col = 0; col < h; col++) { var cx = x + col, cy = y + row, index = (cy * imageData.width + cx) * 4, data = imageData.data, R = data[index], G = data[index + 1], B = data[index + 2], gray = ~~(R * 0.3 + G * 0.59 + B * 0.11); sumGray += gray; } } pixels = w * h; return ~~(sumGray / pixels); }; for (var r = 0; r < rows; r++) { for (var c = 0; c < cols; c++) { var pos_x = ~~(c * char_h), pos_y = ~~(r * char_h), avg = getBlockGray(pos_x, pos_y, ~~char_w, ~~char_h), ch = map[avg]; output += ch; } output += '\r\n'; } ; return output; } function getCharsMap() { var chars = ['@', 'w', '#', '$', 'k', 'd', 't', 'j', 'i', '.', ' ']; var step = 25, map = {}; for (var i = 0; i < 256; i++) { var index = ~~(i / 25) map[i] = chars[index]; } ; return map; }Chỉ với vài đoạn code bạn đã có thể gây ấn tượng bằng cách chuyển hình avatar của mình sang text. Hy vọng anh em sẽ thích bài này.
Nguồn: Devmaster Acasemy via TechTalk
Từ khóa » đổi ảnh Js
-
Chuyển đổi Hình ảnh Bằng Javascript - Nongdanit
-
Lập Trình JavaScript - Thay đổi ảnh Tự động Với JS - Slide ảnh
-
Tạo Image Slider - Dom - JavaScript
-
Code Javascript Chuyển Ảnh Với Onclick (), Tạo Hiệu Ứng Chuyển ...
-
Thay đổi Nguồn Hình ảnh Bằng JavaScript - HelpEx
-
Bài Tập: Đổi ảnh Bằng Javascript - WebVN
-
Cropit - Chỉnh Sửa, Thay đổi Kích Thước ảnh Bằng JS - Viblo
-
Đoạn Mã JavaScript Tự động Thay đổi ảnh Nền Background Dựa Vào ...
-
Hướng Dẫn Tạo Javascript Tự động Thay đổi Hình ảnh, Link
-
Đoạn Script Làm Chuyển đổi Hình ảnh | Flamingo
-
JavaScript Thay đổi Hình ảnh Sự Kiện Onclick
-
Lập Trình JavaScript - Thay đổi ảnh Tự động Với JS - Slide ảnh
-
Ví Dụ Về Chuyển đổi Hình Tự động Với Javascript - Express Magazine
-
Hướng Dẫn Tạo Slideshow Với HTML, CSS Và Javascript