• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2023 Hunan OpenValley Digital Industry Development Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *  http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15import image from '@ohos.multimedia.image'
16import fs from '@ohos.file.fs';
17
18export async function readImage(uri: string, decodingOptions?: image.DecodingOptions): Promise<PixelMap | null> {
19  const prefix: string = 'file:/';
20  if (uri.startsWith('file://media')) {
21  } else if (prefix) {
22    uri = uri.substr(prefix.length);
23  }
24  // path参数为文件的应用沙箱路径或文件URI,所以使用沙箱路径时,需要去掉前缀
25  const file: fs.File = await fs.open(uri, fs.OpenMode.READ_ONLY);
26  let imageSourceApi: image.ImageSource = image.createImageSource(file.fd);
27  if (!imageSourceApi) {
28    return null;
29  }
30  let pixmap: image.PixelMap;
31  if (decodingOptions) {
32    pixmap = await imageSourceApi.createPixelMap(decodingOptions);
33  } else {
34    pixmap = await imageSourceApi.createPixelMap();
35  }
36  if (!pixmap) {
37    return null;
38  }
39  return pixmap;
40}