找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 113|回复: 0

Mac 系统, node 如何从剪切板中读取图片并转成 base64

[复制链接]

1088

主题

0

回帖

3292

积分

管理员

积分
3292
发表于 2023-12-8 09:55:41 | 显示全部楼层 |阅读模式
试了下 pbpaste命令,貌似只能打印出剪切板里的文本

用 Swift 写个命令行工具,然后 Node.js 调用吧。
如下是使用 js 和 vue 从剪切板获取图片对象,实测可用.转成 base64 需要自己处理getImage() { const clipboardObj = navigator.clipboard; if (clipboardObj !== undefined) { setTimeout(async () => { try { const clipboardItems = await navigator.clipboard.read(); for (const clipboardItem of clipboardItems) { // console.log(clipboardItem); for (const type of clipboardItem.types) { const blob = await clipboardItem.getType(type); console.log(blob); if(blob.type.includes("image")){ this.imageUrl = URL.createObjectURL(blob); return }else{ console.log("not image"); } } } this.$message({ message: '剪切板没有图片', type: 'warning', duration: 2000 }); } catch (err) { console.error(err.name, err.message); } }, 100); } },
google 关键字 pngpaste
pbpaste 已经接近可用的思路了,可以参照下面的方案写个函数存在 .*shrc 里:openssl base64 < path/to/file.png | tr -d '\n' | pbcopycat path/to/file.png | openssl base64 | tr -d '\n' | pbcopy
https://github.com/magicdawn/simple-mac-clipboard和 Electron clipboard 差不多的 api, 之前做 electron, 碰到读图片导致 electron crash. 写了个 addon, 可以在 node or electron 中使用const clip = require('simple-mac-clipboard')const buf = clip.readBuffer('public/png') // png bufferconst base64 = buf.toString('base64') // png buffer -> string (base64 encoding)
readBuffer 第一个参数可以从剪贴板数据中获取参见- https://superuser.com/questions/541335/clipboard-viewer-for-os-x-that-allows-extracting-the-data-in-any-available-forma- https://github.com/sindresorhus/Pasteboard-Viewer- https://langui.net/clipboard-viewer/
http://www.markdouma.com/developer/ClipboardViewer.zip
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|张迁碑

GMT+8, 2024-5-19 16:53 , Processed in 0.097206 second(s), 21 queries .

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表