HarmonyOS 相册/拍照 压缩 上传demo

HarmonyOS
1天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
aquaa

拍照上传头像demo:

import { camera, cameraPicker } from '@kit.CameraKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { photoAccessHelper } from '@kit.MediaLibraryKit';
import { picker } from '@kit.CoreFileKit';

@Entry
@Component
struct Index {
  @State message: string = 'Hello World';
  @State uri: string = '';
  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
  ];
  async open(){
    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 video URI
      this.uri = pickerResult.resultUri;
      hilog.info(0x0000, ' ', "the pick pickerResult is:" + JSON.stringify(pickerResult));
    } catch (error) {
      let err = error as BusinessError;
      hilog.error(0x0000, '', `the pick call failed. error code: ${err.code}`);
    }
  }
  async choose(){
    try {
      // 设置图片选择器选项
      const photoSelectOptions = new picker.PhotoSelectOptions();
      // 限制只能选择一张图片
      photoSelectOptions.MIMEType = picker.PhotoViewMIMETypes.IMAGE_TYPE;
      photoSelectOptions.maxSelectNumber = 10;
      // 创建并实例化图片选择器
      const photoViewPicker = new picker.PhotoViewPicker();
      // 选择图片并获取图片URI
      let uris: picker.PhotoSelectResult = await photoViewPicker.select(photoSelectOptions);
      if (!uris || uris.photoUris.length === 0) return;
      console.info('123123uris', JSON.stringify(uris))
      // 获取选中图片的第一张URI
      this.uri = uris.photoUris[0];
    } catch (error) {
      let err: BusinessError = error as BusinessError;
      console.error('PhotoViewPicker failed with err: ' + JSON.stringify(err));
    }
  }
  build() {
    Column() {
      Image(this.uri)
        .width(200)
        .height(200)
        .borderRadius(100)
        .backgroundColor(Color.Gray)
      Button('打开相机').onClick(()=>{
        this.open()
      })
      Button('选择图片').onClick(()=>{
        this.choose()
      })
    }
    .justifyContent(FlexAlign.Start)
    .alignItems(HorizontalAlign.Center)
    .height('100%')
    .width('100%')
  }
}

关于图片压缩可以使用ImagePacker,参考官方文档:

https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-image-V5#imagepacker

分享
微博
QQ
微信
回复
1天前
相关问题
HarmonyOS 相册,相机拍照并裁剪
92浏览 • 1回复 待解决
HarmonyOS 怎么对图片进行压缩上传
82浏览 • 1回复 待解决
HarmonyOS 上传文件相关Demo
555浏览 • 1回复 待解决
HarmonyOS 上传文件的demo
20浏览 • 1回复 待解决
HarmonyOS 如何压缩图片之后再上传
50浏览 • 1回复 待解决
需要视频录制、压缩demo
244浏览 • 1回复 待解决
HarmonyOS 关于从相册中选择图片上传
37浏览 • 1回复 待解决
进入相册拍照选择图片做头像
13629浏览 • 2回复 已解决
HarmonyOS如何上传图片到模拟器相册
980浏览 • 1回复 待解决
鸿蒙请求相册和相机的demo
5552浏览 • 1回复 待解决