当前位置: 首页 > news >正文

鸿蒙实现相机拍照及相册选择照片

前言:

1.如果你的应用不是存储类型或者相机拍照类型,你就需要用 @kit.CameraKit Api 实现相机拍照和相册选择照片功能,如果你不用这个的话,你使用 picker.PhotoViewPicker ,你就需要申请权限,那你提交应用审核的时候就会被拒,说你使用了敏感权限。

2.以下是自己封装的一个工具类,可直接导入使用

import { camera, cameraPicker } from '@kit.CameraKit';

import { photoAccessHelper } from '@kit.MediaLibraryKit';

import fs from '@ohos.file.fs';

import { BusinessError } from '@kit.BasicServicesKit';

class PhotoPicker {

private imgSrc:string = ''

//拍照

async takePhoto():Promise<string> {

const pickerResult = await cameraPicker.pick(getContext(),

[cameraPicker.PickerMediaType.PHOTO], {

cameraPosition: camera.CameraPosition.CAMERA_POSITION_BACK

});

this.saveImage(pickerResult.resultUri);

return this.imgSrc;

}

//从相册中选 photoAccessHelper版

async pickPhoto():Promise<string | void> {

const PhotoSelectOptions = new photoAccessHelper.PhotoSelectOptions();

PhotoSelectOptions.MIMEType = photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE;

//选择图片数量

PhotoSelectOptions.maxSelectNumber = 1;

let photoPicker = new photoAccessHelper.PhotoViewPicker();

try {

const res = await photoPicker.select(PhotoSelectOptions)

if (res.photoUris.length <= 0) {

return

}

this.saveImage(res.photoUris[0])

return this.imgSrc;

} catch (e) {

console.log('img----err--',JSON.stringify(e))

}

}

//存到应用缓存

private saveImage(src:string){

try {

const destPath = getContext(this).cacheDir

const fileType = src.split('.')[1]

const fileName = Date.now()

const path = `${destPath}/${fileName}.${fileType}`

const imgFile = fs.openSync(src,fs.OpenMode.READ_ONLY)

const tempFile = fs.openSync(path,fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE)

// 拷贝

fs.copyFileSync(imgFile.fd,tempFile.fd)

fs.close(imgFile)

fs.close(tempFile)

// 从沙箱中获取图片路径

// this.imgSrc = fileUri.getUriFromPath(path)

this.imgSrc = path

} catch (e) {

let err: BusinessError = e as BusinessError;

console.info("img--[picker] Photo Save error = " + JSON.stringify(err));

}

}

}

export const photoPicker = new PhotoPicker()

http://www.lryc.cn/news/471898.html

相关文章:

  • 「C/C++」C++17 之 std::filesystem::recursive_directory_iterator 目录及子目录迭代器
  • 智能EDA小白从0开始 —— DAY30 冉谱微RFIC-GPT
  • Android -- 调用系统相册之图片裁剪保存
  • 读《道德经》让人感到心胸气闷?董仲舒篡改
  • D52【python 接口自动化学习】- python基础之模块与标准库
  • 基于yolov8的布匹缺陷检测系统,支持图像、视频和摄像实时检测【pytorch框架、python源码】
  • SQL Server 中,将单行数据转换为多行数据
  • 解决数组两数之和问题与逻辑推理找出谋杀案凶手
  • uniapp的IOS证书申请(测试和正式环境)及UDID配置流程
  • windows 安装apex_Nvidia Apex安装
  • Laravel5 抓取第三方网站图片,存储到本地
  • DevOps和CI/CD以及在微服务架构中的作用
  • Rust 力扣 - 5. 最长回文子串
  • DDOS防护介绍
  • 深入了解 kotlinx-datetime:配置与使用指南
  • Qt编程技巧小知识点(6)根据 *IDN? 对程控仪器连接状态进行确认
  • 【Android】Kotlin教程(4)
  • 机票电子行程单如何批量查验?Java机票电子行程单查验接口示例-发票查验接口
  • 记录element-ui改造select显示为table,并支持多查询条件
  • Spearman、Pearson、Euclidean、Cosine、Jaccard,用来衡量不同数据之间的相似性或差异性
  • Suno 歌曲生成 API 对接说明
  • 详细且系统的Spring Boot应用开发
  • 线程支持库(C++11)
  • 【计网】深入理解NAT机制,内网穿透与内网打洞,代理服务
  • C# 创建型设计模式----工厂模式
  • java中Scanner的nextLine和next方法
  • 2024年全国山洪径流模拟与洪水危险性评价技术及典型地区洪水淹没及损失分析
  • CDC 同步数据需要的MySQL数据权限
  • Ubuntu20.04 更新Nvidia驱动 + 安装CUDA12.1 + cudnn8.9.7
  • 算法自学 Lesson3 - 逻辑回归(LR)