Commit 0400d748 by linjinhong

修改裁剪的方法

parent 56dcbfaa
"use strict";
import { ipcMain } from "electron";
import { app, protocol, BrowserWindow, screen, globalShortcut } from "electron";
import { app, protocol, BrowserWindow, globalShortcut } from "electron";
import { createProtocol } from "vue-cli-plugin-electron-builder/lib";
import { createServer } from "@/server/index.js";
import { autoUpdater } from "electron-updater";
......@@ -187,6 +187,8 @@ app.on("activate", async () => {
app.on("ready", async () => {
await createWindow();
checkMemorySetting();
// 获取当前窗口的尺寸
const { width, height } = win.getBounds();
win.webContents.send("window-size", { width, height });
......@@ -203,6 +205,23 @@ app.on("will-quit", () => {
globalShortcut.unregister("CommandOrControl+R");
});
function checkMemorySetting() {
try {
const v8 = require("v8");
const heapStats = v8.getHeapStatistics();
const currentLimitMB = Math.floor(
heapStats.heap_size_limit / (1024 * 1024)
);
console.log(`当前堆内存限制: ${currentLimitMB}MB`);
if (currentLimitMB < 4096) {
console.warn("内存设置不足,建议重启应用");
}
} catch (error) {
console.error("无法检查内存设置", error);
}
}
if (isDevelopment) {
if (process.platform === "win32") {
process.on("message", (data) => {
......
......@@ -44,41 +44,38 @@ function readEnv() {
}
readEnv();
export default {
saveImgByUrl:async (req, res) => {
saveImgByUrl: async (req, res) => {
try {
const response = await axios({
url:req.body.url,
method: 'GET',
responseType: 'stream',
url: req.body.url,
method: "GET",
responseType: "stream",
});
let fileName = uuid.v4() + ".png";
const filePath = path.join(
process.cwd(),
"./print/Input/" +fileName
);
const filePath = path.join(process.cwd(), "./print/Input/" + fileName);
const writer = fs.createWriteStream(filePath);
response.data.pipe(writer);
writer.on('finish', () => {
writer.on("finish", () => {
res.json({
code: 200,
data:filePath
})
data: filePath,
});
});
writer.on('error', (err) => {
writer.on("error", (err) => {
res.json({
code: 500,
data:err.message
})
data: err.message,
});
});
} catch (error) {
console.error('Error downloading image:', error);
console.error("Error downloading image:", error);
res.json({
code: 500,
data:error.message
})
data: error.message,
});
}
},
writePrintLog: async (req, res) => {
......@@ -333,14 +330,13 @@ export default {
});
},
checkUpdate: async (req, res) => {
console.log("version", req.query.version);
const company = "https://admin.jomalls.com";
try {
let q = `${company}/api/manage/rest/app/checkUpdate?version=${req.query.version}&businessType=production_assistant`;
let { data } = await axios.get(q);
console.log("q", q);
console.log("data", data);
// console.log("q", q);
// console.log("data", data);
if (data.data) {
q = `${company}/api/manage/rest/app/getLatest?businessType=production_assistant`;
......@@ -447,7 +443,7 @@ export default {
let url = "https://factory.jomalls.com/api/logisticsAddress/getAllCountry";
try {
let { data } = await axios.get(url, { headers: { "jwt-token": token } });
console.log(data);
res.send(data);
} catch (error) {
console.log(error);
......
......@@ -12,13 +12,13 @@ const sharp = require("sharp");
*/
async function cropImageTransparentEdges(inputPath, outputPath, options = {}) {
const threshold = options.threshold || 10;
const blockSize = options.blockSize || 500; // 可调的块大小,根据内存调整
// 确保输出目录存在
const outputDir = path.dirname(outputPath);
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
}
console.log(21, outputDir);
try {
// 读取图片信息
......@@ -31,6 +31,8 @@ async function cropImageTransparentEdges(inputPath, outputPath, options = {}) {
.toBuffer({ resolveWithObject: true });
const { width, height, channels } = info;
console.log("width", width);
console.log("height", height);
// 找到图片的边界
let topmost = height;
......@@ -38,17 +40,44 @@ async function cropImageTransparentEdges(inputPath, outputPath, options = {}) {
let leftmost = width;
let rightmost = 0;
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
const alphaIdx = (y * width + x) * channels + channels - 1;
const alpha = data[alphaIdx];
// 如果像素不是透明的(根据阈值)
if (alpha > threshold) {
topmost = Math.min(topmost, y);
bottommost = Math.max(bottommost, y);
leftmost = Math.min(leftmost, x);
rightmost = Math.max(rightmost, x);
// for (let y = 0; y < height; y++) {
// for (let x = 0; x < width; x++) {
// const alphaIdx = (y * width + x) * channels + channels - 1;
// const alpha = data[alphaIdx];
// // 如果像素不是透明的(根据阈值)
// if (alpha > threshold) {
// topmost = Math.min(topmost, y);
// bottommost = Math.max(bottommost, y);
// leftmost = Math.min(leftmost, x);
// rightmost = Math.max(rightmost, x);
// }
// }
// }
const alphaIndexOffset = channels - 1; // alpha channel
// 分块扫描
for (let yStart = 0; yStart < height; yStart += blockSize) {
for (let xStart = 0; xStart < width; xStart += blockSize) {
const blockHeight = Math.min(blockSize, height - yStart);
const blockWidth = Math.min(blockSize, width - xStart);
for (let y = 0; y < blockHeight; y++) {
for (let x = 0; x < blockWidth; x++) {
const alphaIdx =
((yStart + y) * width + (xStart + x)) * channels +
alphaIndexOffset;
const alpha = data[alphaIdx];
// 判断透明度是否大于阈值
if (alpha > threshold) {
// 更新边界
topmost = Math.min(topmost, yStart + y);
bottommost = Math.max(bottommost, yStart + y);
leftmost = Math.min(leftmost, xStart + x);
rightmost = Math.max(rightmost, xStart + x);
}
}
}
}
}
......@@ -67,7 +96,8 @@ async function cropImageTransparentEdges(inputPath, outputPath, options = {}) {
// 计算裁切区域
const cropWidth = rightmost - leftmost + 1;
const cropHeight = bottommost - topmost + 1;
console.log("metadatawidth", metadata.width);
console.log("metadataheight", metadata.height);
// 裁切并保存图片
await sharp(inputPath)
.extract({
......@@ -188,6 +218,8 @@ async function processImages(inputPath, outputPath, options = {}) {
}
}
// 分区边界检测实现
module.exports = {
cropTransparentEdges,
cropImageTransparentEdges,
......
......@@ -17,10 +17,10 @@ export default {
components: { PrintDialog, UpdateDialog },
props: {
user: {
default: {
default: () => ({
avatar: "",
factory: {},
},
}),
type: Object,
},
factoryType: { type: String, default: "CN" },
......@@ -298,7 +298,7 @@ export default {
return new Promise(async (resolve, reject) => {
try {
let res = await this.$api.post("/saveImgByUrl", { url });
console.log(res);
resolve(res.data);
} catch (e) {
reject(e.message);
......@@ -409,11 +409,11 @@ export default {
type: "warning",
});
try {
this.cleanDirectorySync();
} catch (error) {
console.error("清理目录时发生错误:", error.message);
}
// try {
// this.cleanDirectorySync();
// } catch (error) {
// console.error("清理目录时发生错误:", error.message);
// }
try {
//查找生产单号信息传给第二个显示器
......
......@@ -246,6 +246,7 @@ export default {
if (typeof this.detail.imageAry == "string") {
this.detail.imageAry = JSON.parse(this.detail.imageAry);
}
console.log(249, this.detail.imageAry);
},
changeCheckFn(value) {
console.log("check", value);
......@@ -1019,7 +1020,6 @@ export default {
try {
let { data } = await this.$api.post("/getAllCountry");
this.$store.commit("setCountry", data);
console.log(991, this.countryList);
} catch (error) {
console.log(error);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment