获取图片裁剪到边缘的坐标
支持获取图片旋转,水平翻转,垂直翻转后的边缘坐标
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | export function clearImageEdgeBlank(image: HTMLCanvasElement, { padding = 0, rotate = 0, flipV = false , flipH = false , }): Promise<{ contentWidth: number; contentHeight: number; top: number; left: number; frameSize: number; }> { return new Promise((resolve, reject) => { // create canvas const canvas = document.createElement( 'canvas' ); const ctx = canvas.getContext( '2d' ); let scale = 1; const maxLength = Math.max(image.width, image.height); if (maxLength > 1000) { scale = maxLength / 1000; } const imageWidth = image.width / scale; const imageHeight = image.height / scale; const frameSize = translateXYToLine({ x: 0, y: 0 }, { x: imageWidth, y: imageHeight }); canvas.width = frameSize; canvas.height = frameSize; const x = canvas.width / 2; const y = canvas.height / 2; const canvasImage = document.createElement( 'canvas' ); const ctxImage = canvasImage.getContext( '2d' ); canvasImage.width = imageWidth; canvasImage.height = imageHeight; if (flipV || flipH) { ctxImage?.translate(flipH ? imageWidth : 0, flipV ? imageHeight : 0); ctxImage?.scale(flipH ? -1 : 1, flipV ? -1 : 1); } ctxImage?.drawImage(image, 0, 0, imageWidth, imageHeight); ctx?.translate(x, y); if (rotate) { ctx?.rotate(rotate * Math.PI / 180); } ctx?.translate(-x, -y); ctx?.drawImage(canvasImage, x - imageWidth / 2, y - imageHeight / 2); const imageData = ctx?.getImageData(0, 0, canvas.width, canvas.height); const { data = [], width = 0, height = 0 } = imageData || {}; // 左偏移,从左开始一列一列的查找 const getOffsetLeft = () => { for ( let i = 0; i < width; i += 1) { for ( let j = 0; j < height; j += 1) { const pxStartIndex = (j * width + i) * 4; if (data[pxStartIndex + 3] > 0) { return Promise.resolve(i); } } } return Promise.resolve(0); }; // 上偏移,从左开始,一行一行的查找 const getOffsetTop = () => { for ( let i = 0; i < height; i += 1) { for ( let j = 0; j < width; j += 1) { const pxStartIndex = (i * width + j) * 4; if (data[pxStartIndex + 3] > 0) { return Promise.resolve(i); } } } return Promise.resolve(0); }; // 右偏移,从右开始,一列一列的查找 const getOffsetRight = () => { for ( let i = width - 1; i >= 0; i -= 1) { for ( let j = 0; j < height; j += 1) { const pxStartIndex = (j * width + i) * 4; if (data[pxStartIndex + 3] > 0) { return Promise.resolve(i); } } } return Promise.resolve(0); }; // 下偏移,从右开始,一行一行的查找 const getOffsetBottom = () => { for ( let i = height - 1; i >= 0; i -= 1) { for ( let j = 0; j < width; j += 1) { const pxStartIndex = (i * width + j) * 4; if (data[pxStartIndex + 3] > 0) { return Promise.resolve(i); } } } return Promise.resolve(0); }; const queue = [ getOffsetLeft(), getOffsetTop(), getOffsetRight(), getOffsetBottom(), ]; Promise.all(queue).then((res) => { let [startX, startY, endX, endY] = res; // 加上padding startX -= padding; startY -= padding; endX += padding; endY += padding; const contentWidth = endX - startX; const contentHeight = endY - startY; const top = startY; const left = startX; // rosolve裁剪后的图像字符串 resolve({ contentWidth: contentWidth * scale, contentHeight: contentHeight * scale, top: top * scale, left: left * scale, frameSize: frameSize * scale, }); }); }); } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· What?废柴, 还在本地部署DeepSeek吗?Are you kidding?
· 程序员转型AI:行业分析
· 为DeepSeek添加本地知识库
· 深入集成:使用 DeepSeek SDK for .NET 实现自然语言处理功能
· .NET程序员AI开发基座:Microsoft.Extensions.AI
2019-08-30 ToPrimitive将值转换为对应的基本类型对象