1

HarmonyOS手机遥控Dayu开发板相机(一)-51CTO.COM

 1 year ago
source link: https://os.51cto.com/article/712599.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
264e55078ba3ca20fd9796f8855acd12c1fff0.png

​想了解更多关于开源的内容,请访问:​

​51CTO 开源基础软件社区​

​https://ost.51cto.com​

前几篇博文解决了HarmonyOS和OpenHarmony的通信问题,现在尝试用通信来做一些事情。比如用手机遥控开发板的摄像头,或者开发板遥控手机的摄像头,乃至照片回传。关于调用板子的摄像头的方式经研究,主要有两种,一种是camera组件,一种是调用API。先从简单的camera组件开始。

  • 调用接口成功

#夏日挑战赛#【FFH】HarmonyOS手机遥控Dayu开发板相机(一)-开源基础软件社区

#夏日挑战赛#【FFH】HarmonyOS手机遥控Dayu开发板相机(一)-开源基础软件社区

拍摄的图片实际没有保存到图库。

二、Camera组件

1、需要权限

相机权限,读写媒体文件的权限。

{
        "name": "ohos.permission.CAMERA"
      },
     {
        "name": "ohos.permission.WRITE_MEDIA"
      },
      {
        "name": "ohos.permission.READ_MEDIA"
      }

2、支持的事件

除了通用事件外,还支持error事件,在用户不允许使用摄像头的时候触发。

3、支持方法

仅支持,takePhoto(CameraTakePhotoOptions),拍照设置参数。

  • CameraTakePhotoOptions

参数列表:

quality

string

图片质量:high,normal,low

success

Function

接口调用成功的回调函数

Function

接口调用失败的回调函数

complete

Function

接口调用结束的回调函数

三、案例编写

关于手机侧,开发板侧的UDP通信请看我的前几篇博文,这里只分享Camera的组件调用。

HarmonyOS与OpenHarmony通信

1、index.css

这里全局就用一个摄像头画面即可。

<camera flash="on" deviceposition="back" @error="cameraError" style="height: 100%;width: 100%;position: absolute;top: 1%;" onclick="takepic"   id="c">
  </camera>
  • flash值控制闪光灯的开启。
  • @error就是前文提到的,如果用户不授权就会触发的回调函数。
  • deviceposition的值表示前后摄像头,front表示前置,back表示后置。

2、index.js

这里约定,接受到0的信息表示进行拍照的操作,手机侧沿用之前的功能编写即可。

  • takePhoto(options)
takepic(){
        var options= {
            "quality":"normal",
            "success":this.onSuccess(),
            "fail":this.onFailed(),
            "complete":this.onCompleted()
        }
        this.$element("c").takePhoto(options
        );
    },
    onFailed:async function(){
        prompt.showToast({
            message: "拍摄失败"
        });
    },
    onSuccess:async function(e){
        console.info(this.TAG+JSON.stringify(e))
        prompt.showToast({
            message: "拍摄成功"
        });
    },
    onCompleted:async function(){
        prompt.showToast({
            message: "拍摄完成"
        });
    },
     cameraError(){
        prompt.showToast({
            message: "授权失败!"
        });
    },

这里将takpic()方法绑定到UDPsocket的监听函数上即可,当判断信息为0的时候,执行拍照操作,可通过toast窗口判断拍摄情况。

  • 在config.json中添加读写媒体,相机的权限。
  • 获取用户授权。
grantPermission() {
        console.info('Calc[IndexPage] grantPermission')
        let context = featureAbility.getContext()
        context.requestPermissionsFromUser(['ohos.permission.CAMERA', "ohos.permission.WRITE_MEDIA","ohos.permission.READ_MEDIA"], 666, function (result) {
         
        })
    },

可见Camera组件能够获取相机的预览和拍照功能,各项回调函数能够进入,但是实际没有保存到图库,可能是该组件还未完善。下次我们继续尝试调用其他api来获取相机预览流和拍照功能。

​想了解更多关于开源的内容,请访问:​

​51CTO 开源基础软件社区​

​https://ost.51cto.com​​。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK