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

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

HarmonyOS
9天前
浏览
收藏 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)

  }
}
分享
微博
QQ
微信
回复
8天前
相关问题
获取网络图片保存到相册
1681浏览 • 1回复 待解决
怎么下载网络上PDF保存到本地?
3898浏览 • 1回复 待解决
如何将Pixmap保存到本地文件?
573浏览 • 1回复 待解决
HarmonyOS 图片保存到相册报错
258浏览 • 1回复 待解决
HarmonyOS 下载文件保存到指定目录
218浏览 • 1回复 待解决
应用内组件截图保存到用户文件
1318浏览 • 1回复 待解决
如何调用系统拍照获取图片
857浏览 • 1回复 待解决