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

2021-07-14 14:58:48 +08:00
 ciming

试了下 pbpaste命令,貌似只能打印出剪切板里的文本

2054 次点击
所在节点    Node.js
7 条回复
ysc3839
2021-07-14 15:03:45 +08:00
用 Swift 写个命令行工具,然后 Node.js 调用吧。
james2013
2021-07-14 15:32:28 +08:00
如下是使用 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);
}
},
ddsfeng
2021-07-14 15:41:19 +08:00
google 关键字 pngpaste
JasonEWNL
2021-07-14 16:14:33 +08:00
pbpaste 已经接近可用的思路了,可以参照下面的方案写个函数存在 .*shrc 里:

openssl base64 < path/to/file.png | tr -d '\n' | pbcopy

cat path/to/file.png | openssl base64 | tr -d '\n' | pbcopy
magicdawn
2021-07-27 18:44:40 +08:00
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 buffer
const base64 = buf.toString('base64') // png buffer -> string (base64 encoding)
magicdawn
2021-07-27 18:56:05 +08:00
magicdawn
2021-07-27 18:57:36 +08:00

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/789483

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX