拍照后图片保存到本地后能打开并显示

拍照后图片保存到本地指定路径能打开并显示,用Device file Browser 导出保存的图片,可以打开。存到本地后的图片, 可以加载显示。

HarmonyOS
2024-12-27 17:09:09
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
shlp

示例代码如下:

import { cameraPicker } from '@kit.CameraKit';
import { camera } from '@kit.CameraKit';
import { BusinessError } from '@ohos.base';
import { hilog } from '@kit.PerformanceAnalysisKit'
import { CommonConstants as Const } from '../common/constants/CommonConstants';
import { image } from '@kit.ImageKit';

import fs from '@ohos.file.fs';
import { util } from '@kit.ArkTS';
import { fileUri } from '@kit.CoreFileKit';
@Entry
@Component
struct ImagePickerPage {
  @State uri: Resource | string | undefined = undefined;
  private cameraPosition: Array<camera.CameraPosition> = [
    camera.CameraPosition.CAMERA_POSITION_UNSPECIFIED, camera.CameraPosition.CAMERA_POSITION_BACK,
    camera.CameraPosition.CAMERA_POSITION_FRONT, camera.CameraPosition.CAMERA_POSITION_FOLD_INNER
  ];
  private mediaType: Array<cameraPicker.PickerMediaType> = [
    cameraPicker.PickerMediaType.PHOTO, cameraPicker.PickerMediaType.VIDEO
  ];
  baseDirStr: string = "/soon"
  imageDirStr: string = "/images"
  getBaseFileDir() {
    let path = getContext().filesDir + this.baseDirStr
    if (fs.accessSync(path) == false) {
      fs.mkdirSync(path)
    }
    return path
  }
  getImageFileDir() {
    let path = this.getBaseFileDir() + this.imageDirStr
    if (fs.accessSync(path) == false) {
      fs.mkdirSync(path)
    }
    return path
  }
  saveImage(buffer: ArrayBuffer,type:string , name?: string): string | undefined {
    if (!name) {
      name = util.generateRandomUUID(false)
    }
    let path = this.getImageFileDir() + "/" + name
    return this.saveFile(buffer , type , path)
  }
  saveFile(buffer: ArrayBuffer | string, type:string ,uri: string): string | undefined {
    let file: fs.File | undefined = undefined
    try {
      let path = uri + "." + type
      file = fs.openSync(path, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE)
      fs.writeSync(file.fd, buffer)
      fs.closeSync(file)
      return path
    } catch (e) {
      try {
        if (file) fs.close(file)
        console.log("whiteImage error" + e)
      } catch (e) {
      }
      return undefined
    }
  }
  build() {
    Row() {
      Column() {
        Image(this.uri)
          .height($r('app.float.image_height'))
          .alt($r('app.media.startIcon'))
        Button($r('app.string.capture'))
          .width($r('app.float.button_width'))
          .margin({ top: $r('app.float.margin') })
          .onClick(async () =&gt; {
          try {
            // Configure to launch the rear camera
            let pickerProfile: cameraPicker.PickerProfile = { cameraPosition: this.cameraPosition[1] };
            // Configure to photo mode
            let pickerResult: cameraPicker.PickerResult = await cameraPicker.pick(getContext(this),
              [this.mediaType[0]], pickerProfile);
            // Get URI
            let uri = pickerResult.resultUri
            let type = uri.split(&#34;.&#34;).pop()
            let fd = fs.openSync(uri , fs.OpenMode.READ_ONLY).fd
            const imagePackerApi: image.ImagePacker = image.createImagePacker();
            const imageSourceApi: image.ImageSource = image.createImageSource(fd);
            let packOpts: image.PackingOption = { format: &#34;image/jpeg&#34;, quality: 98 };
            imagePackerApi.packing(imageSourceApi, packOpts, (err: BusinessError, data: ArrayBuffer) =&gt; {
              if (err) {
                console.error('packing failed.');
              } else {
                let res = this.saveImage(data,type??&#34;jpg&#34;)
                this.uri = fileUri.getUriFromPath(res);
              }
            })
            hilog.info(0x0000, ' ', &#34;the pick pickerResult is:&#34; + JSON.stringify(pickerResult));
          } catch (error) {
            let err = error as BusinessError;
            hilog.error(0x0000, '', `the pick call failed. error code: ${err.code}`);
          }
        })
      }
      .width(Const.FULL_SIZE)
    }
    .height(Const.FULL_SIZE)

  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
分享
微博
QQ
微信
回复
2024-12-27 19:13:12


相关问题
获取网络图片保存到相册
2851浏览 • 1回复 待解决
HarmonyOS拍照后图片添加水印
680浏览 • 1回复 待解决
HarmonyOS 图片保存到相册
836浏览 • 1回复 待解决
HarmonyOS 图片保存到相册报错
1038浏览 • 1回复 待解决
如何将Pixmap保存到本地文件?
1223浏览 • 1回复 待解决
怎么下载网络上PDF保存到本地?
4728浏览 • 1回复 待解决
应用内组件截图保存到用户文件
2386浏览 • 1回复 待解决
HarmonyOS 下载文件保存到指定目录
1190浏览 • 1回复 待解决