Commit 0400d748 by linjinhong

修改裁剪的方法

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