基于ImageKit对图片进行处理

基于ImageKit对图片进行处理

HarmonyOS
2024-06-13 10:41:56
1.2w浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
e_leaner

步骤一:获取图片。

方法一:通过沙箱路径获取:

const context : Context = getContext(this); 
const filePath : string = context.cacheDir + '/test.jpg';
  • 1.
  • 2.

方法二:通过沙箱路径获取图片的文件描述符:

const context = getContext(this); 
const filePath = context.cacheDir + '/test.jpg'; 
const file : fs.File = fs.openSync(filePath, fs.OpenMode.READ_WRITE); 
const fd : number = file?.fd;
  • 1.
  • 2.
  • 3.
  • 4.

方法三:通过资源管理器获取资源文件的ArrayBuffer:

const context : Context = getContext(this); 
// 获取resourceManager资源管理器 
const resourceMgr : resourceManager.ResourceManager = context.resourceManager; 
resourceMgr.getRawFileContent('test.jpg').then((fileData : Uint8Array) => { 
  console.log("Succeeded in getting RawFileContent") 
  // 获取图片的ArrayBuffer 
  const buffer = fileData.buffer.slice(0); 
}).catch((err : BusinessError) => { 
  console.error("Failed to get RawFileContent") 
});
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

方法四:通过资源管理器获取资源文件的RawFileDescriptor

const context : Context = getContext(this); 
// 获取resourceManager资源管理器 
const resourceMgr : resourceManager.ResourceManager = context.resourceManager; 
resourceMgr.getRawFd('test.jpg').then((rawFileDescriptor : resourceManager.RawFileDescriptor) => { 
  console.log("Succeeded in getting resourceManager") 
}).catch((err : BusinessError) => { 
  console.error("Failed to get resourceManager") 
});
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

步骤二:创建imageSource。

方法一:通过沙箱路径创建ImageSource。沙箱路径可以通过步骤2的方法一获取。

// path为已获得的沙箱路径 
const imageSource : image.ImageSource = image.createImageSource(filePath);
  • 1.
  • 2.

方法二:通过文件描述符fd创建ImageSource。文件描述符可以通过步骤2的方法二获取。

// fd为已获得的文件描述符 
const imageSource : image.ImageSource = image.createImageSource(fd);
  • 1.
  • 2.

方法三:通过缓冲区数组创建ImageSource。缓冲区数组可以通过步骤2的方法三获取。

const imageSource : image.ImageSource = image.createImageSource(buffer);
  • 1.

方法四:通过资源文件的RawFileDescriptor创建ImageSource。RawFileDescriptor可以通过步骤2的方案四获取。

const imageSource : image.ImageSource = image.createImageSource(rawFileDescriptor);
  • 1.

步骤三:设置解码参数DecodingOptions,解码获取PixelMap图片对象。

let decodingOptions : image.DecodingOptions = { 
  editable: true, 
  desiredPixelFormat: 3, 
} 
// 创建pixelMap并进行简单的旋转和缩放 
imageSource.createPixelMap(decodingOptions).then((pixelMap : image.PixelMap) => { 
  // 顺时针旋转90° 
  pixelMap.rotate(90); 
 
  // 宽为原来的0.5 
  // 高为原来的0.5 
  pixelMap.scale(0.5, 0.5); 
  console.log("Succeeded in creating PixelMap") 
}).catch((err : BusinessError) => { 
  console.error("Failed to create PixelMap") 
});
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.

步骤四:图形处理。

场景一:在Ts侧:

创建图像编码ImagePacker对象。

const imagePackerApi = image.createImagePacker();
  • 1.

设置编码输出流和编码参数。

format为图像的编码格式;quality为图像质量,范围从0-100,100为最佳质量。

let packOpts : image.PackingOption = { format:"image/jpeg", quality:98 };
  • 1.

进行图片编码,并保存编码后的图片:

方法一: 通过pixelMap编码。

import {BusinessError} from '@ohos.base' 
import fs from '@ohos.file.fs' 
const context : Context = getContext(this); 
const path : string = context.cacheDir + "/pixel_map.jpg"; 
let file = fs.openSync(path, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE); 
imagePackerApi.packToFile(pixelMap, file.fd, packOpts).then(() => { 
  // 直接打包进文件 
}).catch((error : BusinessError) => { 
  console.error('Failed to pack the image. And the error is: ' + error); 
})
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

方法二:通过imageSource编码。

import {BusinessError} from '@ohos.base' 
import fs from '@ohos.file.fs' 
const context : Context = getContext(this); 
const filePath : string = context.cacheDir + "/image_source.jpg"; 
let file = fs.openSync(filePath, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE); 
imagePackerApi.packToFile(imageSource, file.fd, packOpts).then(() => { 
  // 直接打包进文件 
}).catch((error : BusinessError) => { 
  console.error('Failed to pack the image. And the error is: ' + error); 
})
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

场景二:在Native侧:

在ts侧传入pixelmap和文件fd到native侧。

const path = getContext(this).filesDir + "imagenative.jpeg"; 
let file = fs.openSync(path, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 
let fd =file.fd 
testNapi.add1(this.pixelMap,fd)
  • 1.
  • 2.
  • 3.
  • 4.

创建编码器实例对象。

napi_value packer; 
int32_t result = OH_ImagePacker_Create(env, &packer); 
ImagePacker_Native* nativePacker = OH_ImagePacker_InitNative(env, packer);
  • 1.
  • 2.
  • 3.

设置编码参数。

struct ImagePacker_Opts_ opts; 
// 配置编码格式(必须) 
opts.format = "image/jpeg"; 
// 配置编码质量(必须) 
opts.quality = 98;
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

进行编码。

int32_t result = OH_ImagePacker_PackToFile(nativePacker, args[0], &opts, fd);
  • 1.
分享
微博
QQ
微信
回复
2024-06-13 20:54:25


相关问题
基于PhotoViewPicker图片进行操作
1367浏览 • 1回复 待解决
如何图片进行高斯模糊处理
2670浏览 • 1回复 待解决
基于CameraKit相机进行操作
1245浏览 • 1回复 待解决
如何相册图片进行编辑裁剪
2411浏览 • 1回复 待解决
HarmonyOS 怎么图片进行压缩上传
556浏览 • 1回复 待解决
指定url的图片进行下载保存
1711浏览 • 1回复 待解决
如何网络图片处理,有人知道吗?
646浏览 • 1回复 待解决
如何字符串进行MD5哈希处理
3171浏览 • 1回复 待解决
基于滚动组件的手势处理
868浏览 • 1回复 待解决
如何Serviceabbility进行调试?
3516浏览 • 1回复 待解决
基于libuv异步库进行线程通信
2306浏览 • 0回复 待解决
如何网页进行预加载?
1256浏览 • 1回复 待解决
如何网页进行预连接?
724浏览 • 1回复 待解决
打包怎么代码进行混淆?
7009浏览 • 1回复 待解决
如何编译产物进行反编译
1325浏览 • 1回复 待解决
如何鸿蒙设备进行ui测试?
3909浏览 • 1回复 待解决