thedesk/app/main/img.js

88 lines
2.8 KiB
JavaScript
Raw Permalink Normal View History

2019-05-19 17:39:30 +10:00
function img(mainWindow, dir) {
2019-12-14 03:54:40 +11:00
const electron = require('electron')
const dialog = electron.dialog
const fs = require('fs')
var Jimp = require('jimp')
var ipc = electron.ipcMain
const BrowserWindow = electron.BrowserWindow
ipc.on('file-select', (e, args) => {
2020-05-11 19:42:43 +10:00
let fileNames = dialog.showOpenDialogSync(mainWindow, {
properties: ['openFile', 'multiSelections'],
title: '添付ファイルを選択',
defaultPath: '.',
filters: [
{
name: 'メディアファイル',
extensions: ['jpg', 'png', 'gif', 'bmp', 'jpeg', 'mp4', 'webm'],
},
{ name: '画像', extensions: ['jpg', 'png', 'gif', 'bmp', 'jpeg'] },
{ name: '動画', extensions: ['mp4', 'webm'] },
{ name: '全てのファイル', extensions: ['*'] },
],
})
if (!fileNames) {
return false
}
for (var i = 0; i < fileNames.length; i++) {
var path = fileNames[i]
var bin = fs.readFileSync(path, 'base64')
e.sender.send('resizeJudgement', [bin, 'new', fileNames[i]])
2020-05-11 19:42:43 +10:00
}
2019-12-14 03:54:40 +11:00
})
ipc.on('bmp-image', (e, args) => {
2020-05-11 19:42:43 +10:00
Jimp.read(args[0], function (err, lenna) {
2019-12-14 03:54:40 +11:00
if (err) throw err
2020-05-11 19:42:43 +10:00
lenna.getBase64(Jimp.MIME_PNG, function (err, src) {
e.sender.send('bmp-img-comp', [src, args[1]])
2019-12-14 03:54:40 +11:00
})
})
})
ipc.on('resize-image', (e, args) => {
var ext = args[0].toString().slice(args[0].indexOf('/') + 1, args[0].indexOf(';'))
if (ext == 'jpeg') {
var use = 'MIME_JPEG'
} else {
var use = 'MIME_PNG'
}
var b64 = args[0].replace(/^data:\w+\/\w+;base64,/, '')
var decodedFile = new Buffer(b64, 'base64')
2020-05-11 19:42:43 +10:00
Jimp.read(decodedFile, function (err, lenna) {
2019-12-14 03:54:40 +11:00
if (err) throw err
2020-05-11 19:42:43 +10:00
lenna.scaleToFit(args[1], args[1]).getBase64(Jimp[use], function (err, src) {
e.sender.send('bmp-img-comp', [src, args[1]])
2019-12-14 03:54:40 +11:00
})
})
})
ipc.on('stamp-image', (e, args) => {
var text = args[1]
var b64 = args[0].replace(/^data:\w+\/\w+;base64,/, '')
var decodedFile = new Buffer(b64, 'base64')
console.log(text)
2020-05-11 19:42:43 +10:00
Jimp.read(decodedFile, function (err, image) {
2019-12-14 03:54:40 +11:00
if (err) throw err
2020-05-11 19:42:43 +10:00
Jimp.loadFont(Jimp.FONT_SANS_16_BLACK).then((font) => {
2019-12-14 03:54:40 +11:00
var evWidth = Jimp.measureText(font, text)
var width = image.bitmap.width
var height = image.bitmap.height
var left = width - evWidth - 10
var top = height - 30
2020-05-11 19:42:43 +10:00
var color = Jimp.intToRGBA(image.getPixelColor(left, top))
console.log(left, top, color)
2019-12-14 03:54:40 +11:00
var ave = (color.r + color.g + color.b) / 3
if (ave > 128) {
2020-05-11 19:42:43 +10:00
image.print(font, left, top, args[1]).getBase64(Jimp.MIME_PNG, function (err, src) {
e.sender.send('bmp-img-comp', [src, args[1], true])
2019-12-14 03:54:40 +11:00
})
} else {
2020-05-11 19:42:43 +10:00
Jimp.loadFont(Jimp.FONT_SANS_16_WHITE).then((font) => {
image.print(font, left, top, args[1]).getBase64(Jimp.MIME_PNG, function (err, src) {
e.sender.send('bmp-img-comp', [src, args[1], true])
2019-12-14 03:54:40 +11:00
})
})
}
})
})
})
2019-04-03 14:59:29 +11:00
}
2019-12-14 03:54:40 +11:00
exports.img = img