内容说明
提供了完整的图片处理功能,包括:
- 图片加载:从文件路径或 File 对象加载 Bitmap
- 格式转换:Bitmap 与字节数组之间的转换(支持 PNG、JPEG、WEBP)
- 图片处理:缩放、旋转、圆形/圆角处理
- 图片保存:支持保存为 JPEG、PNG、WEBP 格式
功能列表
读取功能
getBitmap(path: String?): 从文件路径加载 BitmapgetBitmap(file: File?): 从 File 对象加载 Bitmap
格式转换
bitmap2Bytes(bitmap: Bitmap?): Bitmap 转 PNG 字节数组bitmap2JpegBytes(bitmap: Bitmap?, quality: Int): Bitmap 转 JPEG 字节数组bitmap2PngBytes(bitmap: Bitmap?, quality: Int): Bitmap 转 PNG 字节数组bitmap2WebpBytes(bitmap: Bitmap?, quality: Int): Bitmap 转 WEBP 字节数组bytes2Bitmap(bytes: ByteArray?): 字节数组转 Bitmap
基本处理
scale(bitmap: Bitmap?, newWidth: Int, newHeight: Int): 按指定宽高缩放scaleBy(bitmap: Bitmap?, scaleW: Float, scaleH: Float): 按比例缩放rotate(bitmap: Bitmap?, degrees: Int): 以中心为轴旋转getRotateDegree(path: String?): 获取 EXIF 旋转角度
圆形/圆角
toRound(bitmap: Bitmap?): 转换为圆形图片toRoundCorner(bitmap: Bitmap?, radius: Float): 转换为圆角图片
保存功能
saveAsJpeg(bitmap: Bitmap?, filePath: String?, quality: Int, recycle: Boolean): 保存为 JPEGsaveAsPng(bitmap: Bitmap?, filePath: String?, recycle: Boolean): 保存为 PNGsaveAsWebp(bitmap: Bitmap?, filePath: String?, quality: Int, recycle: Boolean): 保存为 WEBPsaveAsJpegFile(bitmap: Bitmap?, file: File?, quality: Int, recycle: Boolean): 保存为 JPEG(使用 File 对象)
OOP 调用例子
-- ============================================
-- 图片处理示例
-- 演示如何使用 XfxPlugin.lua 调用 ImageOps 进行图片处理
-- ============================================
print('========== 图片处理示例 ==========')
-- 加载 XfxPlugin.lua 类
local xfxModule = require('lib/XfxPlugin')
-- 创建 XFX 对象实例
local xfx = xfxModule:new({
apkName = 'xfxPlugin-release.apk', -- APK 文件名
})
-- 获取 ImageOps 对象
local ImageOps = xfx:getOps('imageOps')
if not ImageOps then
print('无法获取 ImageOps 对象')
return
end
-- 示例图片路径(请根据实际情况修改)
local sourceImagePath = '/sdcard/video_first_frame_async.jpg'
local outputDir = '/sdcard/image_ops_output/'
-- 确保输出目录存在
local FileOps = xfx:getOps('fileOps')
if FileOps then
FileOps.createOrExistsDir(outputDir)
end
print('开始图片处理示例...')
-- ============================================
-- 1. 加载图片
-- ============================================
print('\n--- 1. 加载图片 ---')
local bitmap = ImageOps.getBitmap(sourceImagePath)
if bitmap then
print('图片加载成功')
else
print('图片加载失败,请检查文件路径:' .. sourceImagePath)
return
end
-- ============================================
-- 2. 图片缩放
-- ============================================
print('\n--- 2. 图片缩放 ---')
-- 按指定宽高缩放(缩放到 800x600)
local scaledBitmap1 = ImageOps.scale(bitmap, 800, 600)
if scaledBitmap1 then
local saved1 = ImageOps.saveAsJpeg(scaledBitmap1, outputDir .. 'scaled_800x600.jpg', 90, true)
if saved1 then
print('缩放图片已保存:scaled_800x600.jpg')
end
end
-- 按比例缩放(缩小到 50%)
local scaledBitmap2 = ImageOps.scaleBy(bitmap, 0.5, 0.5)
if scaledBitmap2 then
local saved2 = ImageOps.saveAsJpeg(scaledBitmap2, outputDir .. 'scaled_50percent.jpg', 90, true)
if saved2 then
print('按比例缩放图片已保存:scaled_50percent.jpg')
end
end
-- ============================================
-- 3. 图片旋转
-- ============================================
print('\n--- 3. 图片旋转 ---')
-- 旋转 90 度(顺时针)
local rotatedBitmap1 = ImageOps.rotate(bitmap, 90)
if rotatedBitmap1 then
local saved3 = ImageOps.saveAsJpeg(rotatedBitmap1, outputDir .. 'rotated_90.jpg', 90, true)
if saved3 then
print('旋转 90 度图片已保存:rotated_90.jpg')
end
end
-- 旋转 180 度
local rotatedBitmap2 = ImageOps.rotate(bitmap, 180)
if rotatedBitmap2 then
local saved4 = ImageOps.saveAsJpeg(rotatedBitmap2, outputDir .. 'rotated_180.jpg', 90, true)
if saved4 then
print('旋转 180 度图片已保存:rotated_180.jpg')
end
end
-- 获取 EXIF 旋转角度
local exifDegree = ImageOps.getRotateDegree(sourceImagePath)
print('EXIF 旋转角度:' .. exifDegree .. ' 度')
-- ============================================
-- 4. 圆形/圆角处理
-- ============================================
print('\n--- 4. 圆形/圆角处理 ---')
-- 转换为圆形
local roundBitmap = ImageOps.toRound(bitmap)
if roundBitmap then
local saved5 = ImageOps.saveAsPng(roundBitmap, outputDir .. 'round.png', true)
if saved5 then
print('圆形图片已保存:round.png')
end
end
-- 转换为圆角(圆角半径 50 像素)
local roundCornerBitmap = ImageOps.toRoundCorner(bitmap, 50.0)
if roundCornerBitmap then
local saved6 = ImageOps.saveAsPng(roundCornerBitmap, outputDir .. 'round_corner_50.png', true)
if saved6 then
print('圆角图片已保存:round_corner_50.png')
end
end
-- ============================================
-- 5. 格式转换
-- ============================================
print('\n--- 5. 格式转换 ---')
-- Bitmap 转 JPEG 字节数组
local jpegBytes = ImageOps.bitmap2JpegBytes(bitmap, 85)
print(jpegBytes)
-- Bitmap 转 PNG 字节数组
local pngBytes = ImageOps.bitmap2PngBytes(bitmap, 100)
print(pngBytes)
-- 字节数组转回 Bitmap
if jpegBytes then
local restoredBitmap = ImageOps.bytes2Bitmap(jpegBytes)
if restoredBitmap then
print('从字节数组恢复 Bitmap 成功')
-- 保存恢复的图片
ImageOps.saveAsJpeg(restoredBitmap, outputDir .. 'restored_from_bytes.jpg', 90, true)
end
end
-- ============================================
-- 6. 图片保存(不同格式)
-- ============================================
print('\n--- 6. 图片保存(不同格式) ---')
-- 保存为 JPEG(质量 90)
local savedJpeg = ImageOps.saveAsJpeg(bitmap, outputDir .. 'saved_as_jpeg.jpg', 90, false)
if savedJpeg then
print('保存为 JPEG 成功')
end
-- 保存为 PNG(无损)
local savedPng = ImageOps.saveAsPng(bitmap, outputDir .. 'saved_as_png.png', false)
if savedPng then
print('保存为 PNG 成功')
end
-- 保存为 WEBP(质量 90)
local savedWebp = ImageOps.saveAsWebp(bitmap, outputDir .. 'saved_as_webp.webp', 90, false)
if savedWebp then
print('保存为 WEBP 成功')
end
-- ============================================
-- 7. 综合示例:处理并保存图片
-- ============================================
print('\n--- 7. 综合示例:处理并保存图片 ---')
-- 重新加载原始图片
local originalBitmap = ImageOps.getBitmap(sourceImagePath)
if originalBitmap then
-- 先缩放
local processed = ImageOps.scale(originalBitmap, 400, 400)
if processed then
-- 再旋转
processed = ImageOps.rotate(processed, 45)
if processed then
-- 最后添加圆角
processed = ImageOps.toRoundCorner(processed, 30.0)
if processed then
-- 保存最终结果
local finalSaved = ImageOps.saveAsJpeg(processed, outputDir .. 'final_processed.jpg', 95, true)
if finalSaved then
print('综合处理完成,已保存:final_processed.jpg')
end
end
end
end
end
print('\n========== 图片处理示例完成 ==========')
print('所有处理后的图片已保存到:' .. outputDir)
直接调用例子
import('com.nx.assist.lua.LuaEngine')
-- ============================================
-- 方式二:直接调用方式(不推荐,但可以了解底层实现)
-- ============================================
print('========== 方式二:直接调用方式 ==========')
local loader = LuaEngine.loadApk('xfxPlugin-release.apk')
if not loader then
print('Failed to load APK')
return
end
local UtilCodeMain = loader.loadClass('com.xfx.plugin.Main')
if not UtilCodeMain then
print('Failed to load Class')
return
end
-- 初始化插件(需要 Context)
local context = LuaEngine.getContext()
UtilCodeMain.init(context)
-- 获取 ImageOps 对象
local ImageOps = UtilCodeMain.imageOps()
-- 加载图片
local bitmap = ImageOps.getBitmap('/sdcard/test_image.jpg')
-- 缩放图片
local scaled = ImageOps.scale(bitmap, 800, 600)
-- 保存图片
ImageOps.saveAsJpeg(scaled, '/sdcard/output.jpg', 90, true)
实际应用示例
示例 1:批量压缩图片
local xfxModule = require('lib/XfxPlugin')
local xfx = xfxModule:new({apkName = 'xfxPlugin-release.apk'})
local ImageOps = xfx:getOps('imageOps')
local FileOps = xfx:getOps('fileOps')
if not ImageOps or not FileOps then
print('无法获取必要的 Ops 对象')
return
end
-- 源目录和目标目录
local sourceDir = '/sdcard/photos/'
local targetDir = '/sdcard/photos_compressed/'
-- 创建目标目录
FileOps.createOrExistsDir(targetDir)
-- 获取源目录下的所有图片文件
local filesJson = FileOps.listFilesAsJson(sourceDir, 'jpg')
local files = jsonLib.decode(filesJson)
for i = 1, #files do
local file = files[i]
local sourcePath = file.filePath
local fileName = file.fileName
-- 加载图片
local bitmap = ImageOps.getBitmap(sourcePath)
if bitmap then
-- 缩放(保持宽高比,最大边 1200 像素)
local width = bitmap.width
local height = bitmap.height
local maxSize = 1200
local scale = 1.0
if width > height then
if width > maxSize then
scale = maxSize / width
end
else
if height > maxSize then
scale = maxSize / height
end
end
local scaled = ImageOps.scaleBy(bitmap, scale, scale)
if scaled then
-- 保存为 JPEG(质量 80)
local targetPath = targetDir .. fileName
local saved = ImageOps.saveAsJpeg(scaled, targetPath, 80, true)
if saved then
print('已压缩:' .. fileName)
end
end
end
end
print('批量压缩完成')
示例 2:生成圆形头像
local xfxModule = require('lib/XfxPlugin')
local xfx = xfxModule:new({apkName = 'xfxPlugin-release.apk'})
local ImageOps = xfx:getOps('imageOps')
if not ImageOps then
print('无法获取 ImageOps 对象')
return
end
-- 加载原始头像
local originalPath = '/sdcard/avatar.jpg'
local bitmap = ImageOps.getBitmap(originalPath)
if bitmap then
-- 先缩放为正方形(200x200)
local square = ImageOps.scale(bitmap, 200, 200)
if square then
-- 转换为圆形
local round = ImageOps.toRound(square)
if round then
-- 保存为 PNG(保持透明背景)
local saved = ImageOps.saveAsPng(round, '/sdcard/avatar_round.png', true)
if saved then
print('圆形头像已生成:/sdcard/avatar_round.png')
end
end
end
end
示例 3:图片格式批量转换
local xfxModule = require('lib/XfxPlugin')
local xfx = xfxModule:new({apkName = 'xfxPlugin-release.apk'})
local ImageOps = xfx:getOps('imageOps')
local FileOps = xfx:getOps('fileOps')
if not ImageOps or not FileOps then
print('无法获取必要的 Ops 对象')
return
end
-- 源目录和目标目录
local sourceDir = '/sdcard/images/'
local targetDir = '/sdcard/images_webp/'
FileOps.createOrExistsDir(targetDir)
-- 获取所有 PNG 文件
local filesJson = FileOps.listFilesAsJson(sourceDir, 'png')
local files = jsonLib.decode(filesJson)
for i = 1, #files do
local file = files[i]
local sourcePath = file.filePath
local fileName = FileOps.getFileNameNoExtension(file.fileName)
-- 加载 PNG 图片
local bitmap = ImageOps.getBitmap(sourcePath)
if bitmap then
-- 转换为 WEBP 格式
local targetPath = targetDir .. fileName .. '.webp'
local saved = ImageOps.saveAsWebp(bitmap, targetPath, 90, true)
if saved then
print('已转换:' .. fileName .. '.webp')
end
end
end
print('格式转换完成')
注意事项
- 内存管理:处理大量图片时,建议在保存后设置
recycle = true来回收 Bitmap 内存 - 文件路径:确保文件路径正确,不存在的文件会导致加载失败
- 图片格式:支持的格式包括 JPEG、PNG、WEBP、BMP 等常见格式
- 质量参数:
- JPEG:0-100,建议使用 80-95
- PNG:无损格式,quality 参数可忽略
- WEBP:0-100,建议使用 80-95
- 旋转角度:正数为顺时针,负数为逆时针
- 缩放比例:
scaleBy方法的比例参数,1.0 表示原始大小,0.5 表示缩小到 50%
接口说明
读取接口
getBitmap(path: String?): Bitmap?– 从文件路径加载 BitmapgetBitmap(file: File?): Bitmap?– 从 File 对象加载 Bitmap
格式转换接口
bitmap2Bytes(bitmap: Bitmap?): ByteArray?– Bitmap 转 PNG 字节数组bitmap2JpegBytes(bitmap: Bitmap?, quality: Int): ByteArray?– Bitmap 转 JPEG 字节数组bitmap2PngBytes(bitmap: Bitmap?, quality: Int): ByteArray?– Bitmap 转 PNG 字节数组bitmap2WebpBytes(bitmap: Bitmap?, quality: Int): ByteArray?– Bitmap 转 WEBP 字节数组bytes2Bitmap(bytes: ByteArray?): Bitmap?– 字节数组转 Bitmap
基本处理接口
scale(bitmap: Bitmap?, newWidth: Int, newHeight: Int): Bitmap?– 按指定宽高缩放scaleBy(bitmap: Bitmap?, scaleW: Float, scaleH: Float): Bitmap?– 按比例缩放rotate(bitmap: Bitmap?, degrees: Int): Bitmap?– 以中心为轴旋转getRotateDegree(path: String?): Int– 获取 EXIF 旋转角度
圆形/圆角接口
toRound(bitmap: Bitmap?): Bitmap?– 转换为圆形图片toRoundCorner(bitmap: Bitmap?, radius: Float): Bitmap?– 转换为圆角图片
保存接口
saveAsJpeg(bitmap: Bitmap?, filePath: String?, quality: Int, recycle: Boolean): Boolean– 保存为 JPEGsaveAsPng(bitmap: Bitmap?, filePath: String?, recycle: Boolean): Boolean– 保存为 PNGsaveAsWebp(bitmap: Bitmap?, filePath: String?, quality: Int, recycle: Boolean): Boolean– 保存为 WEBPsaveAsJpegFile(bitmap: Bitmap?, file: File?, quality: Int, recycle: Boolean): Boolean– 保存为 JPEG(使用 File 对象)
1. 官方交流QQ群,添加多个不批。建议使用安卓手机或电脑申请。
飞云脚本圈: 586333520
Auto.js学习交流③群:286635606
Auto.js学习交流②群:712194666(满员)
IOS免越狱自动化测试群:691997586
2. 盗版,破解有损他人权益和违法作为,请各位会员支持正版。
3. 本站部分资源来源于用户上传和网络搜集,如有侵权请提供版权证明并联系站长删除。
4.如未特别申明,本站的技术性文章均为原创,未经授权,禁止转载/搬运等侵权行为。
5.全站所有付费服务均为虚拟商品,购买后自动发货。售出后概不接受任何理由的退、换。注册即为接受此条款。
6.如果站内内容侵犯了您的权益,请联系站长删除。
飞云脚本 » 【懒人精灵】小飞侠插件专题——图片加载、转换、缩放、保存操作
飞云脚本圈: 586333520

Auto.js学习交流③群:286635606
Auto.js学习交流②群:712194666(满员)
IOS免越狱自动化测试群:691997586
2. 盗版,破解有损他人权益和违法作为,请各位会员支持正版。
3. 本站部分资源来源于用户上传和网络搜集,如有侵权请提供版权证明并联系站长删除。
4.如未特别申明,本站的技术性文章均为原创,未经授权,禁止转载/搬运等侵权行为。
5.全站所有付费服务均为虚拟商品,购买后自动发货。售出后概不接受任何理由的退、换。注册即为接受此条款。
6.如果站内内容侵犯了您的权益,请联系站长删除。
飞云脚本 » 【懒人精灵】小飞侠插件专题——图片加载、转换、缩放、保存操作