跳到正文

剪贴板

剪贴板 j20ntg9l

原文
剪贴板信息

剪贴板信息

收录
更新
HTML
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>图片转盲文 最终终极版</title>
    <style>
        body {
            padding: 25px;
            background: #fff;
            font-family: Arial, sans-serif;
        }
        .panel {
            margin-bottom: 15px;
            display: flex;
            gap: 15px;
            align-items: center;
            flex-wrap: wrap;
        }
        .slider-group {
            display: flex;
            flex-direction: column;
            gap: 5px;
            min-width: 1800px;
        }
        input[type="range"] {
            width: 1800px;
        }
        button {
            padding: 8px 14px;
            cursor: pointer;
            background: #007bff;
            color: white;
            border: none;
            border-radius: 4px;
        }
        #result {
            font-family: monospace;
            white-space: pre;
            font-size: 6px;
            line-height: 1;
            background: #f9f9f9;
            border: 1px solid #ddd;
            padding: 10px;
            margin-top: 15px;
        }
    </style>
</head>
<body>

<h3>🖼️ 图片转盲文字符画 🖼️</h3>

<div class="panel">
    <input type="file" id="imgFile" accept="image/*">
    <button onclick="copyAll()">一键复制</button>
    <button onclick="saveTxt()">保存txt</button>
</div>

<div class="panel">
    <div class="slider-group">
        <label>宽度: <span id="wValue">400</span></label>
        <input type="range" id="width" min="100" max="8000" value="400">
    </div>
    <div class="slider-group">
        <label>高度: <span id="hValue">600</span></label>
        <input type="range" id="height" min="200" max="8000" value="600">
    </div>
    <div class="slider-group">
        <label>二值化阈值: <span id="tValue">190</span></label>
        <input type="range" id="threshold" min="10" max="255" value="190">
    </div>
</div>

<div id="result">上传图片后,拖动滑块即可实时预览</div>

<script>
let brailleText = "";
let currentFile = null;

function pixel_to_braille(block) {
    let b = 0;
    if (block[0][0] < 128) b |= 0b00000001;
    if (block[1][0] < 128) b |= 0b00000010;
    if (block[2][0] < 128) b |= 0b00000100;
    if (block[0][1] < 128) b |= 0b00001000;
    if (block[1][1] < 128) b |= 0b00010000;
    if (block[2][1] < 128) b |= 0b00100000;
    if (block[3][0] < 128) b |= 0b01000000;
    if (block[3][1] < 128) b |= 0b10000000;
    return String.fromCharCode(0x2800 + b);
}

document.getElementById('width').addEventListener('input', (e) => {
    document.getElementById('wValue').innerText = e.target.value;
    if(currentFile) startConvert();
});
document.getElementById('height').addEventListener('input', (e) => {
    document.getElementById('hValue').innerText = e.target.value;
    if(currentFile) startConvert();
});
document.getElementById('threshold').addEventListener('input', (e) => {
    document.getElementById('tValue').innerText = e.target.value;
    if(currentFile) startConvert();
});

document.getElementById('imgFile').addEventListener('change', async (e) => {
    currentFile = e.target.files[0];
    if (!currentFile) return;

    const img = new Image();
    img.src = URL.createObjectURL(currentFile);
    await new Promise(r => img.onload = r);

    const baseW = 400;
    const scale = baseW / img.width;
    const autoH = Math.round(img.height * scale);

    document.getElementById('width').value = baseW;
    document.getElementById('height').value = autoH;
    document.getElementById('wValue').innerText = baseW;
    document.getElementById('hValue').innerText = autoH;

    startConvert();
});

function get(p, w, x, y) {
    return p[y * w + x] ?? 255;
}

async function startConvert() {
    if (!currentFile) return;

    const W = parseInt(document.getElementById('width').value);
    const H = parseInt(document.getElementById('height').value);
    const T = parseInt(document.getElementById('threshold').value);
    const out = document.getElementById('result');
    out.innerText = "生成中...";

    const img = new Image();
    img.src = URL.createObjectURL(currentFile);
    await new Promise(r => img.onload = r);

    const canvas = document.createElement('canvas');
    const ctx = canvas.getContext('2d');
    canvas.width = W;
    canvas.height = H;
    ctx.drawImage(img, 0, 0, W, H);

    const data = ctx.getImageData(0, 0, W, H).data;
    const pixels = [];
    for (let i = 0; i < data.length; i += 4) {
        const gray = data[i];
        pixels.push(gray < T ? 0 : 255);
    }

    let txt = "";
    for (let y = 0; y < H; y += 4) {
        for (let x = 0; x < W; x += 2) {
            const block = [
                [get(pixels, W, x, y), get(pixels, W, x+1, y)],
                [get(pixels, W, x, y+1), get(pixels, W, x+1, y+1)],
                [get(pixels, W, x, y+2), get(pixels, W, x+1, y+2)],
                [get(pixels, W, x, y+3), get(pixels, W, x+1, y+3)],
            ];
            txt += pixel_to_braille(block);
        }
        txt += "\n";
    }

    brailleText = txt;
    out.innerText = txt;
}

function copyAll() {
    navigator.clipboard.writeText(brailleText).then(() => alert("复制成功!"));
}

function saveTxt() {
    const blob = new Blob([brailleText], { type: "text/plain; charset=utf-8" });
    const a = document.createElement('a');
    a.href = URL.createObjectURL(blob);
    a.download = "tupian.txt";
    a.click();
}
</script>
</body>
</html>