$img - 图片操作
- 更新时间:2026-02-08 19:43:41
图片操作
read(imgPath)
读取图片
支持相对路径写法
读取资源失败时会返回空null
- 参数 : imgPath {string} 图片路径
- 返回 : {Image} 读取的图片
open(imgPath)
构建img对象
和read效果一样的
- 参数 : imgPath {string} 图片路径
- 返回 : {Image} 图片对象
- 版本 : 1.2.4
open(bitmap)
构建img对象
- 参数 : bitmap {Bitmap} 安卓图片对象
- 返回 : {Image} 图片对象
- 版本 : 1.2.4
read(imgFile)
读取图片
读取资源失败时会返回空null
- 参数 : imgFile {File} 图片路径
- 返回 : {Image} 读取的图片
save(image, path, format, quality)
保存图片
支持相对路径写法
- 参数 : image {Image} 需要保存的图片
- 参数 : path {string} 保存路径
- 参数 : format {string} 格式
- 参数 : quality {int} 质量
save(image, path)
保存图片
默认保存png格式,质量为100,支持相对路径写法
- 参数 : image {Image} 需要保存的图片
- 参数 : path {string} 保存路径
toBase64(image, format, quality)
转换base64
- 参数 : image {Image} 需要转换为base64的图片
- 参数 : format {string} 格式
- 参数 : quality {int} 质量
- 返回 : 图片的base64字符串
toBase64(image)
转换base64
- 参数 : image {Image} 需要转换的图片
- 返回 : {string} base64字符串
toBase64(path)
转换base64
- 参数 : path {string} 需要转换的图片
- 返回 : {string} base64字符串
readBase64(base64)
加载base64
- 参数 : base64 {string} base64字符串
- 返回 : {Image} 图片对象
clip(image, x, y, w, h)
裁剪图片
- 参数 : image {img} 原本的图片
- 参数 : x {int} 开始坐标
- 参数 : y {int} 开始坐标
- 参数 : w {int} 宽度
- 参数 : h {int} 高度
- 返回 : {Image} 新的图片
resize(image, w, h, interpolation)
设置尺寸
插值方式(忽略大小写): NEAREST, LINEAR, CUBIC, AREA, LANCZOS4, EXACT
- 参数 : image {Image} 需要处理的图片
- 参数 : w {int} 宽度
- 参数 : h {int} 高度
- 参数 : interpolation {string} 插值方式
- 返回 : 图片
resize(image, w, h)
设置尺寸
使用默认插值方式:LINEAR
- 参数 : image {Image} 需要处理的图片
- 参数 : w {int} 宽度
- 参数 : h {int} 高度
- 返回 : {Image} 处理后的图片
scale(image, scaleX, scaleY, interpolation)
缩放图片
- 参数 : image {Image} 需要处理的图片
- 参数 : scaleX {double} 宽度缩放倍数
- 参数 : scaleY {double} 高度缩放倍数
- 参数 : interpolation {string} 插值方式
- 返回 : {Image} 处理后的图片
scale(image, scaleX, scaleY)
缩放图片
- 参数 : image {Image} 需要处理的图片
- 参数 : scaleX {double} 宽度缩放倍数
- 参数 : scaleY {double} 高度缩放倍数
- 返回 : {Image} 处理后的图片
rotate(image, degree, x, y)
旋转图片
- 参数 : image {Image} 需要处理的图片
- 参数 : degree {float} 旋转的角度
- 参数 : x {int} 旋转中心点x坐标
- 参数 : y {int} 旋转中心点y =坐标
- 返回 : {Image} 处理后的图片
rotate(image, degree)
旋转图片
- 参数 : image {Image} 需要处理的图片
- 参数 : degree {float} 旋转的角度
- 返回 : {Image} 处理后的图片
rotate(image)
旋转图片
默认旋转90度
- 参数 : image {Image} 需要处理的图片
- 返回 : {Image} 处理后的图片
concat(img1, img2, dir)
拼接图片
- 参数 : img1 {Image} 需要拼接的图片1
- 参数 : img2 {Image} 需要拼接的图片2
- 参数 : dir {string} 拼接方向(left,right,top,bottom)
- 返回 : {Image} 拼接好的图片
gray(image)
灰度化
- 参数 : image {Image} 需要灰度化的图片
- 返回 : {Image} 灰度化的图片
threshold(image, thre)
二值化图片
- 参数 : image {Image} 需要处理的图片
- 参数 : thre {double} 阈值
- 返回 : {Image} 处理后的图片
adaptiveThreshold(image, maxValue, adaptiveMethod, thresholdType, blockSize, C)
自适应二值化
- 参数 : image {Image} 图片
- 参数 : maxValue {number} 最大值
- 参数 : adaptiveMethod {string} 在一个邻域内计算阈值所采用的算法 默认值是:GAUSSIAN_C
- 参数 : thresholdType {string} 阈值化类型 默认值是:BINARY
- 参数 : blockSize {number} 邻域块大小
- 参数 : C {number} 偏移值调整量
- 返回 : {Image} 处理后的图片
cvtColor(img, code, dstCn)
颜色空间转换
- 参数 : img {Image} 图片
- 参数 : code {string} 颜色空间转换的类型,可选的值有一共有205个 默认值:COLOR_BGR2GRAY
- 参数 : dstCn {int} 目标图像的颜色通道数量,如果不填写则根据其他参数自动决定。
- 返回 : {Image} 颜色转换后的图片
cvtColor(img, code)
颜色空间转换
- 参数 : img {Image} 图片
- 参数 : code {string} 颜色空间转换的类型,可选的值有一共有205个 默认值:COLOR_BGR2GRAY
- 返回 : {Image} 颜色转换后的图片
inRange(img, lowerBound, upperBound)
图片二值化
- 参数 : img {Image} 图片
- 参数 : lowerBound {string|number} 颜色下界
- 参数 : upperBound {string|number} 颜色上界
- 返回 : {Image} 处理后的图片
blur(img, size, point, type)
模糊处理
模糊类型(不区分大小写): CONSTANT, REPLICATE, REFLECT, WRAP, REFLECT_101, TRANSPARENT, REFLECT101, DEFAULT, ISOLATED
- 参数 : img {Image} 图片
- 参数 : size {double[]} 定义滤波器的大小,如[3, 3]
- 参数 : point {double[]} 指定锚点位置(被平滑点),默认为图像中心
- 参数 : type {string} 推断边缘像素类型,默认为"DEFAULT"
- 返回 : {Image} 处理后的图片
medianBlur(img, size)
中值滤波
- 参数 : img {Image} 图片
- 参数 : size {int} 定义滤波器的大小
- 返回 : {Image} 处理后的图片
gaussianBlur(img, size, sigmaX, sigmaY, type)
高斯模糊
模糊类型(不区分大小写): CONSTANT, REPLICATE, REFLECT, WRAP, REFLECT_101, TRANSPARENT, REFLECT101, DEFAULT, ISOLATED
- 参数 : img {Image} 图片
- 参数 : size {double[]} 定义滤波器的大小,如[3, 3]
- 参数 : sigmaX {double} x方向的标准方差,不填写则自动计算
- 参数 : sigmaY {double} y方向的标准方差,不填写则自动计算
- 参数 : type {string} 推断边缘像素类型,默认为"DEFAULT"
- 返回 : {Image} 处理后的图片
flip(image)
左右翻转
- 参数 : image {Image} 需要处理的图片
- 返回 : {Image} 处理后的图片
upside(image)
上下翻转
- 参数 : image {Image} 需要处理的图片
- 返回 : {Image} 处理后的图片
flip(image, sx, sy)
水平翻转
- 参数 : image {Image} 需要处理的图片
- 参数 : sx {float} 横向翻转的方向
- 参数 : sy {float} 纵向翻转的方向
- 返回 : {Image} 处理后的图片
putTop(bigImg, smallImg, x, y)
在大图片上面放置一个小图片
- 参数 : bigImg {Image} 大图片
- 参数 : smallImg {Image} 小图片
- 参数 : x {int} 小图片左上角x
- 参数 : y {int} 小图片左上角y
- 返回 : {Image} 处理完成的图片
findColor(image, color, threshold, x, y, w, h)
查找颜色
- 参数 : image {Image} 需要处理的图片
- 参数 : color {string} 需要查找的颜色
- 参数 : threshold {int} 阈值
- 参数 : x {int} 范围x起点坐标
- 参数 : y {int} 范围y起点坐标
- 参数 : w {int} 范围宽度
- 参数 : h {int} 范围高度
- 返回 : {Image} 处理后的图片
findColor(image, color, threshold, region)
查找颜色
- 参数 : image {Image} 需要处理的图片
- 参数 : color {string} 需要查找的颜色
- 参数 : threshold {int} 阈值
- 参数 : region {int[]} 找色范围
- 返回 : {Point} 找到的结果
findColor(image, color, threshold)
查找颜色
- 参数 : image {Image} 需要处理的图片
- 参数 : color {string} 需要查找的颜色
- 参数 : threshold {int} 阈值
- 返回 : {Image} 处理后的图片
findColor(image, color)
查找颜色
- 参数 : image {Image} 需要处理的图片
- 参数 : color {string} 需要查找的颜色
- 返回 : {Image} 处理后的图片
findMultiColors(image, region, color, threshold, colors)
多点找色
- 参数 : image {Image} 图片
- 参数 : region {int[]} 范围
- 参数 : color {string} 起点颜色
- 参数 : threshold {int} 起点色阈值
- 参数 : colors {int[]} 点色数据
- 返回 : {point} 位置
findImg(bigImg, minImg, options)
找图
- 参数 : bigImg {Image} 大图片
- 参数 : minImg {Image} 小图片
- 参数 : options {object} 参数
- 返回 : {Point} 找到的位置(中心点)
findImg(bigImg, minImg)
找图
- 参数 : bigImg 大图片
- 参数 : minImg 小图片
- 返回 : 找到的位置(中心点)
findImgAll(bigImg, minImg, options)
找到所有图片位置
- 参数 : bigImg {Image} 大图片
- 参数 : minImg {Image} 需要查找的小图片
- 参数 : options {object} 查找参数
- 返回 : {point[]} 所有的位置集合
findImgAll(bigImg, minImg)
找到所有图片位置
- 参数 : bigImg {Image} 大图片
- 参数 : minImg {Image} 需要查找的小图片
- 返回 : {point[]} 所有的位置集合
show(image)
显示图片
- 参数 : image 需要显示的图片
show(path)
显示图片
- 参数 : path {string} 图片路径