harmony 鸿蒙Image Encoding

  • 2023-06-24
  • 浏览 (678)

Image Encoding

Image encoding refers to the process of encoding a pixel map into an archived image in different formats (only in JPEG, WebP, and PNG currently) for subsequent processing, such as storage and transmission.

How to Develop

Read Image API Reference for APIs related to image encoding.

  1. Create an ImagePacker object.
   // Import the required module.
   import image from '@ohos.multimedia.image';
   
   const imagePackerApi = image.createImagePacker();
  1. Set the encoding output stream and encoding parameters.

format indicates the image encoding format, and quality indicates the image quality. The value ranges from 0 to 100, and the value 100 indicates the optimal quality.

   let packOpts : image.PackingOption = { format:"image/jpeg", quality:98 };
  1. Create a PixelMap object or an ImageSource object.

  2. Encode the image and save the encoded image.

Method 1: Use the PixelMap object for encoding.

   import {BusinessError} from '@ohos.base'
   imagePackerApi.packing(pixelMap, packOpts).then( (data : ArrayBuffer) => {
     // data is the file stream obtained after packing. You can write the file and save it to obtain an image.
   }).catch((error : BusinessError) => { 
     console.error('Failed to pack the image. And the error is: ' + error); 
   })

Method 2: Use the ImageSource object for encoding.

   import {BusinessError} from '@ohos.base'
   imagePackerApi.packing(imageSource, packOpts).then( (data : ArrayBuffer) => {
       // data is the file stream obtained after packing. You can write the file and save it to obtain an image.
   }).catch((error : BusinessError) => { 
     console.error('Failed to pack the image. And the error is: ' + error); 
   })

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Media

harmony 鸿蒙Developing Audio Call

harmony 鸿蒙Audio Call Development

harmony 鸿蒙Audio Decoding

harmony 鸿蒙Audio Effect Management

harmony 鸿蒙Audio Encoding

harmony 鸿蒙Audio Input Device Management

harmony 鸿蒙Audio Output Device Management

harmony 鸿蒙Audio Playback Concurrency Policy

harmony 鸿蒙Audio Playback Development

0  赞