|
|
@@ -0,0 +1,112 @@
|
|
|
+import { http } from '@kit.NetworkKit';
|
|
|
+import { image } from '@kit.ImageKit';
|
|
|
+import { IBestToast } from '@ibestservices/ibest-ui';
|
|
|
+import { BusinessError } from '@kit.BasicServicesKit';
|
|
|
+import { Context } from '@ohos.abilityAccessCtrl';
|
|
|
+import { util } from '@kit.ArkTS';
|
|
|
+import { fileIo as fs } from '@kit.CoreFileKit';
|
|
|
+import { ContextHelper } from './ContextHelper';
|
|
|
+
|
|
|
+
|
|
|
+export class ImageUtil {
|
|
|
+ // 根据url下载图片,获取的是ArrayBuffer对象
|
|
|
+
|
|
|
+ static async downLoadPic(url: string): Promise<ArrayBuffer | null> {
|
|
|
+
|
|
|
+ let data = await http.createHttp().request(url);
|
|
|
+
|
|
|
+ if (data.result instanceof ArrayBuffer) {
|
|
|
+
|
|
|
+ let imageData: ArrayBuffer = data.result
|
|
|
+
|
|
|
+ return imageData;
|
|
|
+
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ static async arrayBufToPixelMap(buf: ArrayBuffer): Promise<PixelMap> {
|
|
|
+
|
|
|
+ let imgSource: image.ImageSource = image.createImageSource(buf);
|
|
|
+
|
|
|
+ let result: PixelMap = await imgSource.createPixelMap();
|
|
|
+
|
|
|
+ return result;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取图片url
|
|
|
+ */
|
|
|
+ static async saveImageToBox(pixmap: PixelMap, context: Context) {
|
|
|
+
|
|
|
+ // 处理PixelMap数据
|
|
|
+ // 2. PixelMap数据 => 二进制
|
|
|
+ const packer = image.createImagePacker() // 创建一个图片打包转换器
|
|
|
+ // 图片打包转换器 接收PixelMap数据 传入打包配置(jpeg格式 质量100)
|
|
|
+ const arrayBuffer = await packer.packToData(pixmap, { format: 'image/jpeg', quality: 100 }) // 转换为二进制
|
|
|
+
|
|
|
+ // 3.创建图片一个文件 写入数据(二进制)
|
|
|
+ const imagePath = context.cacheDir + '/' + util.generateRandomUUID() + '.jpeg'
|
|
|
+ const file = fs.openSync(imagePath, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE)
|
|
|
+ fs.writeSync(file.fd, arrayBuffer)
|
|
|
+ return imagePath
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取图片尺寸
|
|
|
+ */
|
|
|
+ static async getImageSize(context: UIContext, path: string, callback: (width: number, height: number) => void) {
|
|
|
+ const imageSource = image.createImageSource(path);
|
|
|
+ imageSource.getImageInfo((error: BusinessError, imageInfo: image.ImageInfo) => {
|
|
|
+ if (error) {
|
|
|
+ IBestToast.show('获取图片信息失败')
|
|
|
+ } else {
|
|
|
+ callback(context.px2vp(imageInfo.size.width), context.px2vp(imageInfo.size.height))
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 手动压缩图片
|
|
|
+ */
|
|
|
+ static async manualCompression(filePath: string, quality: number, context: Context) {
|
|
|
+ const imageSourceApi: image.ImageSource = image.createImageSource(filePath);
|
|
|
+ let packOpts: image.PackingOption = { format: "image/jpeg", quality: quality };
|
|
|
+ const imagePackerApi = image.createImagePacker()
|
|
|
+ const buffer = await imagePackerApi.packing(imageSourceApi, packOpts)
|
|
|
+ const imagePath = context.cacheDir + '/' + util.generateRandomUUID() + '.jpeg'
|
|
|
+ const file = fs.openSync(imagePath, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE)
|
|
|
+ fs.writeSync(file.fd, buffer)
|
|
|
+ fs.closeSync(file)
|
|
|
+ fs.unlinkSync(filePath)
|
|
|
+ return imagePath
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ static async downLoadToBox(url: string) {
|
|
|
+ const buff = await ImageUtil.downLoadPic(url)
|
|
|
+ if (buff) {
|
|
|
+ const pixelMap = await ImageUtil.arrayBufToPixelMap(buff)
|
|
|
+ const original = await ImageUtil.saveImageToBox(pixelMap, ContextHelper.context)
|
|
|
+ //压缩图片
|
|
|
+ const localUrl = await ImageUtil.manualCompression(original, 10, ContextHelper.context)
|
|
|
+ return localUrl
|
|
|
+ }
|
|
|
+ return null
|
|
|
+ }
|
|
|
+
|
|
|
+ static async downLoadToBoxWithQuality(url: string, quality: number) {
|
|
|
+ const buff = await ImageUtil.downLoadPic(url)
|
|
|
+ if (buff) {
|
|
|
+ const imageSourceApi: image.ImageSource = image.createImageSource(buff);
|
|
|
+ let packOpts: image.PackingOption = { format: "image/jpeg", quality: quality };
|
|
|
+ const imagePackerApi = image.createImagePacker()
|
|
|
+ const buffer = await imagePackerApi.packing(imageSourceApi, packOpts)
|
|
|
+ return buffer
|
|
|
+ }
|
|
|
+ return null
|
|
|
+ }
|
|
|
+}
|