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

开源 Arkts 鸿蒙应用 开发(八)多媒体--相册和相机

 文章的目的为了记录使用Arkts 进行Harmony app 开发学习的经历。本职为嵌入式软件开发,公司安排开发app,临时学习,完成app的开发。开发流程和要点有些记忆模糊,赶紧记录,防止忘记。

 相关链接:

开源 Arkts 鸿蒙应用 开发(一)工程文件分析-CSDN博客

开源 Arkts 鸿蒙应用 开发(二)封装库.har制作和应用-CSDN博客

开源 Arkts 鸿蒙应用 开发(三)Arkts的介绍-CSDN博客

开源 Arkts 鸿蒙应用 开发(四)布局和常用控件-CSDN博客

开源 Arkts 鸿蒙应用 开发(五)控件组成和复杂控件-CSDN博客

开源 Arkts 鸿蒙应用 开发(六)数据持久--文件和首选项存储-CSDN博客

开源 Arkts 鸿蒙应用 开发(七)数据持久--sqlite关系数据库-CSDN博客

 android 链接(十三章):

开源 java android app 开发(一)开发环境的搭建_csdn 开源 java android app-CSDN博客

.net mvc链接(八章):

开源C# .net mvc 开发(一)WEB搭建_c#部署web程序-CSDN博客

本章内容主要是鸿蒙的多媒体使用,主要功能包括从相册选择图片、拍摄照片以及将图片保存到应用沙箱目录。

1.使用流程

2.相册和相机的使用

3.保存到沙箱

4.所有代码

5.显示效果

一、使用流程图

二、相册和相机的使用

2.1  使用PhotoViewPicker选择图片

         Button("打开相册照片").width('100%').height(40).onClick(async () => {try {let PhotoSelectOptions = new photoAccessHelper.PhotoSelectOptions();PhotoSelectOptions.MIMEType = photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE;PhotoSelectOptions.maxSelectNumber = 1;let photoPicker = new photoAccessHelper.PhotoViewPicker();photoPicker.select(PhotoSelectOptions).then((PhotoSelectResult: photoAccessHelper.PhotoSelectResult) => {this.imageUri = PhotoSelectResult.photoUris[0] ? PhotoSelectResult.photoUris[0] : this.imageUri;hilog.info(0x0000, TAG, 'PhotoViewPicker.select succeed, uri: ' + JSON.stringify(PhotoSelectResult));}).catch((err: BusinessError) => {hilog.error(0x0000, TAG, `PhotoViewPicker.select failed, error: ${err.code}, ${err.message}`);});} catch (error) {let err: BusinessError = error as BusinessError;hilog.error(0x0000, TAG, `PhotoViewPicker failed, error: ${err.code}, ${err.message}`);}this.isShowGet = false;})

2.2  用cameraPicker调用设备相机

          Button("相机拍摄照片").width('100%').height(40).onClick(async () => {// [Start pick_file]try {let pickerProfile: cameraPicker.PickerProfile ={ cameraPosition: camera.CameraPosition.CAMERA_POSITION_BACK };//Select the action of pulling up the camera to take pictures.let pickerResult: cameraPicker.PickerResult = await cameraPicker.pick(this.getUIContext().getHostContext(),[cameraPicker.PickerMediaType.PHOTO], pickerProfile);//Return the photo uri to the application.this.imageUri = pickerResult.resultUri ? pickerResult.resultUri : this.imageUri;hilog.info(0x0000, TAG, 'cameraPicker.pick succeed, uri: ' + JSON.stringify(pickerResult));} catch (error) {let err = error as BusinessError;hilog.error(0x0000, TAG, `cameraPicker.pick failed, error: ${err.code}, ${err.message}`);}// [End pick_file]this.isShowGet = false;})

三、保存到沙箱,特别的是大量使用async/await处理异步操作

 async saveImageToSandbox() {// 1. 检查图片URIif (!this.imageUri) {this.getUIContext().getPromptAction().showToast({message: $r('app.string.no_image_alert'),duration: 2000});return;}try {// 2. 等待图片复制到沙箱await copyImg2Sandbox(this.imageUri, this.path);// 3. 创建ImageSourcethis.imageSource = image.createImageSource(this.path);// 4. 并行获取图片信息await Promise.all([new Promise<void>((resolve, reject) => {this.imageSource!.getImageInfo((error, info) => {error ? reject(error) : resolve();});}),this.imageSource.getImageProperties([image.PropertyKey.IMAGE_WIDTH,image.PropertyKey.IMAGE_LENGTH,image.PropertyKey.F_NUMBER])]);// 5. 创建PixelMap(确保这一步成功)this.pixelMap = await this.imageSource.createPixelMap();if (!this.pixelMap) {throw new Error('Failed to create PixelMap');}// 6. 保存到文件await pixelMap2File(this.pixelMap, this.path);this.getUIContext().getPromptAction().showToast({message: $r('app.string.save_in_sandbox_success'),duration: 2000});this.isShowSave = false;} catch (error) {this.handleSaveError(error as BusinessError);}}

四、所有代码,新建工程后,只修改Index.ets就可以

Index.ets代码

import { image } from '@kit.ImageKit';
import { PhotoPickerComponent, PickerController, photoAccessHelper, ReminderMode } from '@kit.MediaLibraryKit';
import { cameraPicker, camera } from '@kit.CameraKit';
import { picker } from '@kit.CoreFileKit';
import { promptAction } from '@kit.ArkUI';
import { BusinessError } from '@kit.BasicServicesKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { copyImg2Sandbox, pixelMap2File } from '../common/utils/Utils';const TAG = 'IMAGE_APP';@Entry
@Component
struct Index {@State path: string = this.getUIContext().getHostContext()!.filesDir + '/image.jpg';@State pixelMapPath: string = this.getUIContext().getHostContext()!.filesDir + '/pixelMap.jpg';@State imageUri: string | undefined = undefined;@State imageSource: image.ImageSource | undefined = undefined;@State pixelMap: image.PixelMap | undefined = undefined;@State isShowGet: boolean = false;@State isShowPicker: boolean = false;@State isShowSave: boolean = false;@State pickerController: PickerController = new PickerController();async saveImageToSandbox() {// 1. 检查图片URIif (!this.imageUri) {this.getUIContext().getPromptAction().showToast({message: $r('app.string.no_image_alert'),duration: 2000});return;}try {// 2. 等待图片复制到沙箱await copyImg2Sandbox(this.imageUri, this.path);// 3. 创建ImageSourcethis.imageSource = image.createImageSource(this.path);// 4. 并行获取图片信息await Promise.all([new Promise<void>((resolve, reject) => {this.imageSource!.getImageInfo((error, info) => {error ? reject(error) : resolve();});}),this.imageSource.getImageProperties([image.PropertyKey.IMAGE_WIDTH,image.PropertyKey.IMAGE_LENGTH,image.PropertyKey.F_NUMBER])]);// 5. 创建PixelMap(确保这一步成功)this.pixelMap = await this.imageSource.createPixelMap();if (!this.pixelMap) {throw new Error('Failed to create PixelMap');}// 6. 保存到文件await pixelMap2File(this.pixelMap, this.path);this.getUIContext().getPromptAction().showToast({message: $r('app.string.save_in_sandbox_success'),duration: 2000});this.isShowSave = false;} catch (error) {this.handleSaveError(error as BusinessError);}}// 错误处理方法private handleSaveError(error: BusinessError) {hilog.error(0x0000, TAG, `保存失败: ${error.code}, ${error.message}`);let message = "save_failed";if (error.message.includes('createPixelMap')) {message = "pixelmap_creation_failed";} else if (error.message.includes('copyImg2Sandbox')) {message = "copy_failed";}this.getUIContext().getPromptAction().showToast({message,duration: 2000});}build() {Navigation() {Column() {Image(this.imageUri).height(400).margin({ top: 16 })Column({ space: 12 }) {Button("打开相册照片").width('100%').height(40).onClick(async () => {try {let PhotoSelectOptions = new photoAccessHelper.PhotoSelectOptions();PhotoSelectOptions.MIMEType = photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE;PhotoSelectOptions.maxSelectNumber = 1;let photoPicker = new photoAccessHelper.PhotoViewPicker();photoPicker.select(PhotoSelectOptions).then((PhotoSelectResult: photoAccessHelper.PhotoSelectResult) => {this.imageUri = PhotoSelectResult.photoUris[0] ? PhotoSelectResult.photoUris[0] : this.imageUri;hilog.info(0x0000, TAG, 'PhotoViewPicker.select succeed, uri: ' + JSON.stringify(PhotoSelectResult));}).catch((err: BusinessError) => {hilog.error(0x0000, TAG, `PhotoViewPicker.select failed, error: ${err.code}, ${err.message}`);});} catch (error) {let err: BusinessError = error as BusinessError;hilog.error(0x0000, TAG, `PhotoViewPicker failed, error: ${err.code}, ${err.message}`);}this.isShowGet = false;})Button("相机拍摄照片").width('100%').height(40).onClick(async () => {// [Start pick_file]try {let pickerProfile: cameraPicker.PickerProfile ={ cameraPosition: camera.CameraPosition.CAMERA_POSITION_BACK };//Select the action of pulling up the camera to take pictures.let pickerResult: cameraPicker.PickerResult = await cameraPicker.pick(this.getUIContext().getHostContext(),[cameraPicker.PickerMediaType.PHOTO], pickerProfile);//Return the photo uri to the application.this.imageUri = pickerResult.resultUri ? pickerResult.resultUri : this.imageUri;hilog.info(0x0000, TAG, 'cameraPicker.pick succeed, uri: ' + JSON.stringify(pickerResult));} catch (error) {let err = error as BusinessError;hilog.error(0x0000, TAG, `cameraPicker.pick failed, error: ${err.code}, ${err.message}`);}// [End pick_file]this.isShowGet = false;})Button("保存照片到APP目录").width('100%').height(40).onClick(async () => {this.saveImageToSandbox();})}.width('100%').height(150).padding({ left: 16, right: 16 }).margin({ bottom: 16 }).justifyContent(FlexAlign.SpaceBetween)}.width('100%').height('100%').justifyContent(FlexAlign.SpaceBetween)}.width('100%').height('100%').title($r('app.string.title'))}
}

五、显示效果

5.1  app图片和打开相册后图片

5.2  保存到APP目录后图片所存位置图,点击DevEco的右下角的 Device File Browser

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

相关文章:

  • 一个适合MCU的分级菜单框架
  • 格式工厂5.21.0简介
  • 设计模式六:工厂模式(Factory Pattern)
  • 从安装到上手:Ubuntu 22.04 玩转 Containerd 2.1.3 容器运行时
  • 在 Windows上用WSL和VSCode进行Linux开发环境配置
  • 《使用 IDEA 部署 Docker 应用指南》
  • 在Anolis8.6上源码编译安装部署OpenVAS(GVM)未完待续
  • git bash命令不够完善,想整合msys2该怎么办?
  • Dynamics 365 Contact Center是什么
  • Java 解析前端上传 ZIP 压缩包内 Excel 文件的完整实现方案
  • 前端开发者快速理解Spring Boot项目指南
  • 在 Angular 应用程序中使用 Genkit 的完整指南
  • docker 容器学习
  • Three.js 全景图(Equirectangular Texture)教程:从加载到球面映射
  • AR技术:应急响应的加速利器
  • AR技术:石化行业培训的“游戏规则改变者”
  • Web开发:ABP框架12——中间件Middleware的创建和使用
  • AR巡检和传统巡检的区别
  • CCLink IE转ModbusTCP网关与三菱PLC通讯无纸记录器
  • uni-app开发小程序,根据图片提取主题色值
  • 网络编程基础:从 OSI 模型到 TCP/IP 协议族的全面解析
  • Android 中 SystemServiceManager 和 ServiceManager 的应用场景、区别与联系
  • 漏洞扫描 + 渗透测试:双轮驱动筑牢网络安全防线
  • Ubuntu 22.04 使用 Docker 安装 Redis 5 (安装包形式)
  • 内网与外网是通过什么进行传输的?内外网文件传输的安全方法
  • C#最佳实践:为何应尽量减少静态类的使用
  • 迅为八核高算力RK3576开发板摄像头实时推理测试 RetinaFace人脸检测
  • Curtain e-locker 易锁防泄密:无需网络隔离,实现安全与效率并存
  • 大腾智能国产3D CAD软件正式上架华为云云商店
  • 进程资源分配的安全性判断与安全序列