目录

Using ImageSource to Obtain RAW Data

Image RAW data is the raw data directly obtained from the image sensor, which fully retains all sensor information without any compression loss. By using RAW data, you can obtain the highest quality image information and customize image processing algorithms based on specific requirements, enabling more flexible image processing and optimization.

Starting from API version 24, you can decode image files in supported formats into ImageRawData to obtain RAW data in your app or system. RAW data includes the image buffer and bits per pixel.

The currently supported image file format is DNG.

How to Develop

For details about the APIs for obtaining image RAW data, see createImageRawData.

  1. Import the Image module globally, and import the corresponding Kit modules as needed.

    // Import the required modules.
    import { image } from '@kit.ImageKit';
    import { BusinessError } from '@kit.BasicServicesKit';
    import { common } from '@kit.AbilityKit';
    import { fileIo } from '@kit.CoreFileKit';
    import { resourceManager } from '@kit.LocalizationKit';
  2. Obtain the image.

    • Method 1: Obtain the image directly through the sandbox path. This method is only applicable to images in the app sandbox. For details about how to obtain the path, see Obtaining Application File Paths.

      function getFilePath(context: Context, fileName: string): string {
        const filePath: string = context.cacheDir + '/' + fileName;
        return filePath;
      }
    • Method 2: Obtain the file descriptor of the image through the sandbox path. For details, see @ohos.file.fs (File Management). This method requires importing the @kit.CoreFileKit module.

      function getFileFd(context: Context, fileName: string): number | undefined {
        try {
          const filePath: string = context.cacheDir + '/' + fileName;
          const file: fileIo.File = fileIo.openSync(filePath, fileIo.OpenMode.READ_ONLY);
          const fd: number = file?.fd;
          return fd;
        } catch (err) {
          console.error(`Failed to get the fileFd with error: ${err}.`);
          return undefined;
        }
      }
    • Method 3: Obtain the ArrayBuffer of a resource file through the resource manager. For details, see getRawFileContent. This method requires importing the @kit.LocalizationKit module.

      async function getFileBuffer(context: Context, fileName: string): Promise<ArrayBuffer | undefined> {
        try {
          const resourceMgr: resourceManager.ResourceManager = context.resourceManager;
          // Obtain the resource file content and return a Uint8Array.
          const fileData: Uint8Array = await resourceMgr.getRawFileContent(fileName);
          console.info('Successfully get the RawFileContent.');
          // Convert to ArrayBuffer and return.
          const buffer: ArrayBuffer = fileData.buffer.slice(0);
          return buffer;
        } catch (error) {
          console.error(`Failed to get the RawFileContent with error: ${error}.`);
          return undefined;
        }
      }
    • Method 4: Obtain the RawFileDescriptor of a resource file through the resource manager. For details, see getRawFd. This method requires importing the @kit.LocalizationKit module.

      async function getRawFd(context: Context, fileName: string): Promise<resourceManager.RawFileDescriptor | undefined> {
        try {
          const resourceMgr: resourceManager.ResourceManager = context.resourceManager;
          const rawFileDescriptor: resourceManager.RawFileDescriptor = await resourceMgr.getRawFd(fileName);
          console.info('Successfully get the RawFileDescriptor.');
          return rawFileDescriptor;
        } catch (error) {
          console.error(`Failed to get the RawFileDescriptor with error: ${error}.`);
          return undefined;
        }
      }
  3. Create an ImageSource instance.

    • Method 1: Create an ImageSource using a sandbox path. The sandbox path can be obtained through method 1 in step 2.

      // path is the obtained sandbox path.
      const imageSource : image.ImageSource = image.createImageSource(filePath);
    • Method 2: Create an ImageSource using a file descriptor (fd). The fd can be obtained through method 2 in step 2.

      // fd is the obtained file descriptor.
      const imageSource: image.ImageSource = image.createImageSource(fd);
    • Method 3: Create an ImageSource using a buffer array. The buffer array can be obtained through method 3 in step 2.

      const imageSource: image.ImageSource = image.createImageSource(buffer);
    • Method 4: Create an ImageSource using the RawFileDescriptor of a resource file. The RawFileDescriptor can be obtained through method 4 in step 2.

      const imageSource: image.ImageSource = image.createImageSource(rawFileDescriptor);
  4. Obtain the ImageRawData object and print the pixel values.

    async createImageRawData(imageSource: image.ImageSource | undefined) : Promise<image.ImageRawData | undefined> {
      if (!imageSource) {
        console.error('imageSource is undefined.');
        return undefined;
      }
      try {
        const data = await imageSource.createImageRawData();
        if (data.bitsPerPixel == 16 && data.buffer) {
          let array: Uint16Array = new Uint16Array(data.buffer);
          let length = array.byteLength.valueOf();
          console.info(`uint16Array length: ${length}`);
          let value: string = '';
          for (let i = 0; i < array.length && i < 10; i++) {
            value += array[i] + ', ';
          }
          console.info(`get dng rawdata is:${value}.`);
        }
        return data;
      } catch (err) {
        console.error(`get dng rawdata failed.err: ${JSON.stringify(err)}`);
        return undefined;
      }
    }
  5. Release the imageSource.

    After confirming that the asynchronous method of imageSource has completed execution and the variable is no longer in use, you can manually call the following method to release it as needed.

    async release_imageSource() {
      try {
        await this.imageSource?.release();
      } catch (error) {
        console.error(`Failed to release ImageSource: ${error}.`);
      } finally {
        this.imageSource = undefined;
      }
    }