目录

imageSuperResolution (Image Super-Resolution)

Image super-resolution supports super-resolution reconstruction for input low-resolution images to make the images clearer. It is applicable to scenarios such as image quality improvement and old photo restoration.

Since: 26.0.0

Modules to Import

import { imageSuperResolution } from '@kit.CoreVisionKit';

ISPResponse

Response class for image super-resolution processing.

It inherits from visionBase.Response.

System capability: SystemCapability.AI.Vision.VisionBase

Model restriction: This API can be used only in the stage model.

Since: 26.0.0

Name Type Read-Only Optional Description
pixelMap image.PixelMap No No After super-resolution processing, the image quality is improved while the total pixel count is quadrupled.

ImageSRAnalyzer

Image super-resolution analyzer class.

It inherits from visionBase.Analyzer.

System capability: SystemCapability.AI.Vision.VisionBase

Model restriction: This API can be used only in the stage model.

Since: 26.0.0

[h2]ImageSRAnalyzer.create

create(): Promise<ImageSRAnalyzer>

Creates an image super-resolution analyzer instance. This API uses a promise to return the result.

System capability: SystemCapability.AI.Vision.VisionBase

Model restriction: This API can be used only in the stage model.

Since: 26.0.0

Returns

Type Description
Promise<ImageSRAnalyzer> Promise used to return the image super-resolution analyzer instance.

Error codes

For details about the error codes, please refer to Core Vision Kit Error Codes.

ID Error Message
1018700001 Service exception.

Example

import { imageSuperResolution } from '@kit.CoreVisionKit';
import { hilog } from '@kit.PerformanceAnalysisKit';

async function createImageSRAnalyzer() {
  try {
    const analyzer = await imageSuperResolution.ImageSRAnalyzer.create();
    hilog.info(0x0000, 'ImageSR', 'success to created ImageSRAnalyzer');
    return analyzer;
  } catch (error) {
    const err: BusinessError = error as BusinessError;
    hilog.error(0x0000, 'ImageSR', `Failed to create ImageSRAnalyzer code: ${err.code}, message: ${err.message}`);
    return null;
  }
}

[h2]ImageSRAnalyzer.process

process(request: visionBase.Request): Promise<ISPResponse>

Performs super-resolution processing on images. This API uses a promise to return the result.

System capability: SystemCapability.AI.Vision.VisionBase

Model restriction: This API can be used only in the stage model.

Since: 26.0.0

Parameters

Parameter Type Mandatory Description
request visionBase.Request Yes Image instance. The image super-resolution API supports only one image. For details, see Constraints.

Returns

Type Description
Promise<ISPResponse> Promise used to return the image super-resolution processing result.

Error codes

For details about the error codes, please refer to Core Vision Kit Error Codes.

ID Error Message
1018700001 Service exception.

Example

import { imageSuperResolution, visionBase } from '@kit.CoreVisionKit';
import { image } from '@kit.ImageKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { fileIo } from '@kit.CoreFileKit';
import { photoAccessHelper } from '@kit.MediaLibraryKit';

async function processImageSuperResolution() {
  let imageSource: image.ImageSource | undefined = undefined;
  let inputImage: PixelMap | undefined = undefined;

  // Select an image from the gallery.
  let photoSelectOptions = new photoAccessHelper.PhotoSelectOptions();
  photoSelectOptions.MIMEType = photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE;
  photoSelectOptions.maxSelectNumber = 1;
  let photoPicker: photoAccessHelper.PhotoViewPicker = new photoAccessHelper.PhotoViewPicker();
  let photoSelectResult = await photoPicker.select(photoSelectOptions);
  let uri = photoSelectResult.photoUris[0];
  if (uri === undefined) {
    hilog.info(0x0000, 'ImageSR', 'uri is undefined');
    return;
  }

  // Convert the image into a pixel map.
  let file = await fileIo.open(uri, fileIo.OpenMode.READ_ONLY);
  imageSource = image.createImageSource(file.fd);
  inputImage = await imageSource.createPixelMap();
  if (!inputImage) {
    return;
  }

  // Create an image super-resolution analyzer.
  const analyzer = await imageSuperResolution.ImageSRAnalyzer.create();
  if (!analyzer) {
    hilog.error(0x0000, 'ImageSR', 'Failed to create analyzer');
    return;
  }

  // Call the image super-resolution API.
  try {
    let imageData: visionBase.ImageData = {
      pixelMap: inputImage
    }
    const request: visionBase.Request = {
      inputData: imageData
    };
    request.inputData = imageData
    const response = await analyzer.process(request);
    hilog.info(0x0000, 'ImageSR',
      `Super resolution completed size:${JSON.stringify(response.pixelMap.getImageInfoSync().size)}`);
  } catch (error) {
    const err: BusinessError = error as BusinessError;
    hilog.error(0x0000, 'ImageSR', `Failed to process super resolution code: ${err.code}, message: ${err.message}`);
  }

  // Free resources.
  if (inputImage && imageSource) {
    void inputImage.release();
    void imageSource.release();
  }
  if (file) {
    await fileIo.close(file);
  }
  await analyzer.destroy();
}

@Entry
@Component
struct Page {
  build() {
    Column() {
      Button('processImageSuperResolution').onClick(() => {
        // Call the function.
        void processImageSuperResolution();
      })
    }
  }
}

[h2]ImageSRAnalyzer.destroy

destroy(): Promise<void>

Releases the image super-resolution analyzer service. This API uses a promise to return the result.

System capability: SystemCapability.AI.Vision.VisionBase

Model restriction: This API can be used only in the stage model.

Since: 26.0.0

Returns

Type Description
Promise<void> Promise that returns no value.

Error codes

For details about the error codes, please refer to Core Vision Kit Error Codes.

ID Error Message
1018700001 Service exception.

Example

import { imageSuperResolution } from '@kit.CoreVisionKit';
import { hilog } from '@kit.PerformanceAnalysisKit';

async function destroyImageSRAnalyzer() {
  const analyzer = await imageSuperResolution.ImageSRAnalyzer.create();
  if (analyzer) {
    hilog.info(0x0000, 'ImageSR', 'success to created ImageSRAnalyzer');

    // You can add the code for image super-resolution processing here.

    // Release the image super-resolution analyzer service after the usage is complete.
    await analyzer.destroy();
    hilog.info(0x0000, 'ImageSR', 'success to released ImageSRAnalyzer');
  }
}

@Entry
@Component
struct Page {
  build() {
    Column() {
      Button('destroyImageSRAnalyzer').onClick(() => {
        // Call the function.
        void destroyImageSRAnalyzer();
      })
    }
  }
}