IT评测·应用市场-qidao123.com技术社区

标题: 【HarmonyOS NEXT】FAQ之媒体开发(图片处理) [打印本页]

作者: 嚴華    时间: 2024-10-6 20:58
标题: 【HarmonyOS NEXT】FAQ之媒体开发(图片处理)
1、通过PixelMap_CreatePixelMap创建的对象,内存在ArkTS侧和Native侧是否共享

A:通过PixelMap_CreatePixelMap创建的对象在ArkTS侧和Native侧会共享同一份内存。

2、怎样设置图片的高斯模糊效果

A:使用图像效果模块的blur接口,详情请参考链接:图像效果。

3、调用imageSource.createPixelMap()报错“Create PixelMap error”

A:该问题是sampleSize取值错误导致的,sampleSize表示缩略图采样巨细,当前只能取1。可通过DecodingOptions.desiredSize指定输出巨细。

4、图片压缩API的质量参数quality与图片原始巨细、压缩后巨细的关系

A:对于有损压缩图片格式,如jpeg格式,质量参数会影响压缩后的图片巨细,对于无损压缩图片格式,如png格式,质量参数不会影响压缩后的图片巨细
对于有损压缩图片格式,压缩后的图片巨细不仅取决于图片原始巨细、图片压缩质量,还与图片中内容有较大关系,因此当前系统不支持设置压缩后的图片巨细,如果应用想要指定压缩后图片巨细,可以根据压缩效果调整质量参数,或者将pixelmap scale到更小的尺寸后再压缩。

5、图片编解码支持的格式有哪些

A:办理如下

指将所支持格式的存档图片解码成统一的PixelMap,以便在应用或系统中举行图片显示或图片处理。当前支持的存档图片格式包括JPEG、PNG、GIF、RAW、WebP、BMP、SVG。

指将PixelMap编码成不同格式的存档图片(当前仅支持编码为JPEG、WebP 和 PNG 格式),用于后续处理,如保存、传输等。

6、怎样将相册选择的图片天生PixelMap

A:如下两种方法
方法一:
方法二:

7、怎样对相册图片举行编辑裁剪

A:可以通过图片处理模块的pixelMap方法对图片举行编辑裁剪。
其中包括但不限于:


8、怎样设置图片显示的分辨率

A:可以通过sourceSize属性设置图片分辨率,实例代码如下所示,原图尺寸为1280960,该示例将图片解码为4040。

9、怎样保存本舆图片到相册中

A:步调如下

10、怎样读取相册中的图片

A:使用photoAccessHelper.PhotoSelectOptions接口
  1. import { photoAccessHelper } from '@kit.MediaLibraryKit';
  2. import { image } from '@kit.ImageKit';
  3. import { fileIo as fs } from '@kit.CoreFileKit';
  4. @Entry
  5. @Component
  6. struct Index {
  7.  @State getAlbum: string = '显示相册中的图片';
  8.  @State pixel: image.PixelMap | undefined = undefined;
  9.  @State albumPath: string = '';
  10.  @State photoSize: number = 0;
  11.  async getPictureFromAlbum() {
  12.    // 拉起相册,选择图片
  13.    let PhotoSelectOptions = new photoAccessHelper.PhotoSelectOptions();
  14.    PhotoSelectOptions.MIMEType = photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE;
  15.    PhotoSelectOptions.maxSelectNumber = 1;
  16.    let photoPicker = new photoAccessHelper.PhotoViewPicker();
  17.    let photoSelectResult: photoAccessHelper.PhotoSelectResult = await photoPicker.select(PhotoSelectOptions);
  18.    this.albumPath = photoSelectResult.photoUris[0];
  19.    // 读取图片为buffer
  20.    const file = fs.openSync(this.albumPath, fs.OpenMode.READ_ONLY);
  21.    this.photoSize = fs.statSync(file.fd).size;
  22.    console.info('Photo Size: ' + this.photoSize);
  23.    let buffer = new ArrayBuffer(this.photoSize);
  24.    fs.readSync(file.fd, buffer);
  25.    fs.closeSync(file);
  26.    // 解码成PixelMap
  27.    const imageSource = image.createImageSource(buffer);
  28.    console.log('imageSource: ' + JSON.stringify(imageSource));
  29.    this.pixel = await imageSource.createPixelMap({});
  30.   }
  31.  build() {
  32.    Row() {
  33.      Column() {
  34.        Image(this.pixel)
  35.          .width('100%')
  36.          .aspectRatio(1)
  37.        Button('显示照片')
  38.          .onClick(() => {
  39.            this.getPictureFromAlbum();
  40.          })
  41.      }
  42.      .width('100%')
  43.    }
  44.    .height('100%')
  45.   }
  46. }
复制代码

11、怎样把ImageReceiver收到的视频帧数据保存到本地

A:如实例代码所示,在示例代码中保存接收到的前三帧数据,也可以通过业务需要调整。
  1. let size: image.Size = {
  2.  width: 640,
  3.  height: 480
  4. }
  5. let receiver: image.ImageReceiver = image.createImageReceiver(size, image.ImageFormat.JPEG, 8);
  6. receiver.on('imageArrival', () => {
  7.  console.info("imageArrival callback");
  8.  receiver.readNextImage((err: BusinessError, nextImage: image.Image) => {
  9.    if (err || nextImage === undefined) {
  10.      console.error("receiveImage -error:" + err + " nextImage:" + nextImage);
  11.      return;
  12.    }
  13.    nextImage.getComponent(image.ComponentType.JPEG, (err: BusinessError, imgComponent: image.Component) => {
  14.      if (err || imgComponent === undefined) {
  15.        console.error("receiveImage--getComponent -error:" + err + " imgComponent:" + imgComponent);
  16.        return;
  17.      }
  18.      if (imgComponent.byteBuffer as ArrayBuffer) {
  19.        let sourceOptions: image.SourceOptions = {
  20.          sourceDensity: 120,
  21.          sourcePixelFormat: 8,
  22.          sourceSize: {
  23.            height: 1080,
  24.            width: 1920
  25.          },
  26.        }
  27.        let imageResource = image.createImageSource(imgComponent.byteBuffer, sourceOptions);
  28.        let imagePackerApi = image.createImagePacker();
  29.        let packOpts: image.PackingOption = { format: "image/jpeg", quality: 90 };
  30.        const filePath: string = getContext().cacheDir + "/image.jpg";
  31.        let file = fs.openSync(filePath, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE);
  32.        imagePackerApi.packToFile(imageResource, file.fd, packOpts).then(() => {
  33.          console.error('pack success: ' + filePath);
  34.        }).catch((error: BusinessError) => {
  35.          console.error('Failed to pack the image. And the error is: ' + error);
  36.        })
  37.        imageResource.createPixelMap({}).then((res) => {
  38.          this.imgUrl = res;
  39.        });
  40.      } else {
  41.        return;
  42.      }
  43.      nextImage.release();
  44.    });
  45.   });
  46. });
复制代码

12、为什么获取到的yuv数据量比宽 * 高 * 1.5多

A:这是正通例格,多出来的为YUV尾部数据,不影响YUV数据的解析。

13、怎样保存网络图片到相册

A:步调如下
14、通过OH_Pixelmap_CreatePixelMap创建的对象,内存是怎样分配的

A:通过OH_Pixelmap_CreatePixelMap创建的对象在ArkTS侧和Native侧会共享同一份内存。

15、怎样实现PixelMap和base64的相互转换

A:步调如下


16、怎样将PixelMap压缩到指定巨细以下

A:目前没有直接的接口支持将PixelMap压缩到指定巨细以下,但可以通过循环压缩的方式实现,具体可参考如下方案实现压缩:

17、错误码62980096怎么处理

A:pixelFormat罗列目前是给ImageSource用的,所以NV21或者NV12格式的图片如果要创建PixelMap需要通过以下方式:

18、怎样将C++侧接收的PixelMap转换成cv::mat格式

A:将ArkTS侧传到Native侧的PixelMap转换成cv::mat有两种方法:

上述两种方法都必须保证PixelMap的格式与opencv中mat的格式同等,否则会出现色彩的偏差。

19、image.createPixelMap中pixelFormat不见效

A:目前image.createPixelMap默认只能使用BGRA_8888格式处理数据,通过Promise返回效果。后续会提供新接口,可以支持指定输入流格式。

20、怎样将PixelMap保存到相册

A:PixelMap使用imagePacker.packToFile()的方法将ImageSource图片源编码后直接打包进文件。

更多详情及参考代码查察如下:文档中心

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 IT评测·应用市场-qidao123.com技术社区 (https://dis.qidao123.com/) Powered by Discuz! X3.4