官网中有描述camera组件功能界面属性介绍,但是官网没有具体的demo让我们感受拍照的功能,今天写一篇demo来完善一下拍照的功能

demo 功能如下

第一步首先进行拍照功能

第二步 进行js页面跳转功能

第三步 使用image 显示拍照的照片

第一步首先进行拍照功能

参考链接如下

https://developer.harmonyos.com/cn/docs/documentation/doc-references/js-components-media-camera-0000001051414121

1.1 html 代码


<div class="container"> <camera id='camera-id' flash="on" deviceposition="back" @error="cameraError"> </camera> <text class="textTakePhoto" on:click="takePhoto" > 拍照</text> </div>

1.2 css 代码

···

.container {

display: flex;

justify-content: center;

flex-direction: column;

align-items: center;

}

camera {

width: 100%;

height: 80%;

}

.textTakePhoto{

padding: 10px;

border-width: 2vp;

border-color: black;

border-bottom-style: solid;

border-radius: 90vp;

margin-top: 40px;

text-color: black;

font-family: sans-serif;

font-size: 25px;

}



1.3      js 代码

···

export default {

    data: {

        title: 'World',

        src:""

    },

    cameraError(){

    },

    takePhoto(){

        var _this=this;

        console.log('------------------------------>takePhoto')

        var camera = this.$element('camera-id')

        camera.takePhoto({

            quality:'high',

            success(result){

            _this. src=result.uri

            console.log("拍照成功路径==>>"+_this.src);

//            

            },

            fail(result){

                console.info('-------------fail------'+result)

            },

            complete(result){

//                console.info('-------------complete------'+result)

            }

        })

    },

}

1.4 运行效果

第二步 进行js页面跳转功能

我们可以在拍照中完成(complete)的时候进行跳转另外一个界面,然后对图片路径做出相应的处理

参考资料如下

https://developer.harmonyos.com/cn/docs/documentation/doc-references/js-apis-basic-features-routes-0000000000611824

2.1代码如下


complete(result){ console.info('-------------complete------'+result) var appsrc="file:///data/data/com.harmony.alliance.jsdemo/files/" var path="internal://app/"+_this.src.substring(appsrc.length,_this.src.length) console.info('-------------success------'+path ) router.push({ uri: "pages/myImage/myImage", params: { src: path }, }) }

第三步 使用image 显示拍照的照片

3.1 参考资料如下

https://developer.harmonyos.com/cn/docs/documentation/doc-references/js-components-basic-image-0000000000611597

3.2怎么显示本地图片

Image组件存在一个src假设为internal://app/1.png

其真实路径是file:///data/data/包名 /files/1.png

3.3 hml代码


<div class="container">
<text>{{src}}</text>
<image src="{{src}}" style="width: 300px; height: 300px;">
</image>
</div>

CSS代码

.container {

    display: flex;

    justify-content: center;

    align-items: center;

    flex-direction: column;

    left: 0px;

    top: 0px;

    width: 454px;

    height: 454px;

}

.title {

    font-size: 30px;

    text-align: center;

    width: 200px;

    height: 100px;

}

Js 代码


export default { data: { title: 'World', src:"" } }

3.4 运行效果

==

更多精彩内容,请见华为开发者官方论坛→https://developer.huawei.com/consumer/cn/forum/home?ha_source=sanfang

【HarmonyOS】【JS】鸿蒙Js camera怎么拍照并使用image显示出来的更多相关文章

  1. 从微信小程序到鸿蒙js开发【04】——list组件

    目录: 1.可滚动区域 2.list + list-item 3.list + list-item-group + list-item 1.可滚动区域 在许多场景中,页面会有一块区域是可滚动的,比如这 ...

  2. 从微信小程序到鸿蒙js开发【06】——swiper&animator&marquee

    目录: 1.swiper轮播图 2.image-animator幻灯片 3.marquee跑马灯 4.nginx动静分离 1.swiper轮播图 微信小程序的swiper组件中只能放置swiper-i ...

  3. 从微信小程序到鸿蒙js开发【08】——表单组件&注册登录模块

    目录: 1.登录模块 2.注册模块 3.系列文章导读 牛年将至,祝大家行行无bug,页页so easy- 在微信小程序中,提供了form组件,可以将input.picker.slider.button ...

  4. 从微信小程序到鸿蒙js开发【11】——页面路由

    目录: 1.router.push()&wx.navigateTo() 2.router.replace()&wx.redirectTo() 3.router.back()&w ...

  5. 从微信小程序到鸿蒙js开发【12】——storage缓存&自动登录

    鸿蒙入门指南,小白速来!从萌新到高手,怎样快速掌握鸿蒙开发?[课程入口] 正文: 在应用开发时,我们常需要将一些数据缓存到本地,以提升用户体验.比如在一个电商的app中,如果希望用户登录成功后,下次打 ...

  6. 从微信小程序到鸿蒙js开发【13】——list加载更多&回到顶部

    鸿蒙入门指南,小白速来!从萌新到高手,怎样快速掌握鸿蒙开发?[课程入口] 目录: 1.list加载更多 2.list回到顶部 3.<从微信小程序到鸿蒙js开发>系列文章合集 1.list加 ...

  7. 鸿蒙js开发7 鸿蒙分组列表和弹出menu菜单

    鸿蒙入门指南,小白速来!从萌新到高手,怎样快速掌握鸿蒙开发?[课程入口]目录:1.鸿蒙视图效果2.js业务数据和事件3.页面视图代码4.跳转页面后的视图层5.js业务逻辑部分6.<鸿蒙js开发& ...

  8. 从微信小程序到鸿蒙js开发【15】——JS调用Java

    鸿蒙入门指南,小白速来!0基础学习路线分享,高效学习方法,重点答疑解惑--->[课程入口] 目录:1.新建一个Service Ability2.完善代码逻辑3.JS端远程调用4.<从微信小 ...

  9. 鸿蒙JS 开发整理

    目录 一.前言: 二.鸿蒙 JS UI框架 2.1 JS UI特性 2.2 架构 2.3 新的UI框架结构 三.API 四.最后 一.前言: 5月25日,华为对外宣布计划在6月2日正式举办鸿蒙手机发布 ...

随机推荐

  1. 使用react搭建组件库:react+typescript+storybook

    前期准备 1. 初始化项目 npx create-react-app react-components --template typescript 2. 安装依赖 使用哪种打包方案:webpack/r ...

  2. MySQL 报错:[Err] 1071 - Specified key was too long; max key length is 767 bytes

    [Err] 1071 - Specified key was too long; max key length is 767 bytes 这个会出现在MySQ5.7以下版本 因为没有启用innodb_ ...

  3. Linux下c++之常见错误代码errno(退而结网法)

    1.关于 还在到处找 errno对应的含义? 自己动手,很方便可找到其明确的含义 2. 动手 2.1 创建 c++源文件,输入下面的代码: #pragma once #include <iost ...

  4. 【LeetCode】代码模板,刷题必会

    目录 二分查找 排序的写法 BFS的写法 DFS的写法 回溯法 树 递归 迭代 前序遍历 中序遍历 后序遍历 构建完全二叉树 并查集 前缀树 图遍历 Dijkstra算法 Floyd-Warshall ...

  5. 【LeetCode】235. Lowest Common Ancestor of a Binary Search Tree 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 [LeetCode] https://leet ...

  6. 【LeetCode】72. Edit Distance 编辑距离(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 记忆化搜索 动态规划 日期 题目地址:http ...

  7. 【LeetCode】384. Shuffle an Array 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 库函数 Fisher–Yates 洗牌 水塘抽样 日 ...

  8. Notepad++汉化教程

    Notepad++汉化方法总结 Notepad++系统只带了中文语言包,不需要像其他软件一样破解 打开Notepad++(通过文本文件右键选择以Notepad++打开或者找到Notepad++的快捷方 ...

  9. jsoncpp转换字符串

    Json::Value root; ...//root中写入数据 //方法一:转为格式化字符串,里面加了很多空格及换行符 string strJson1 = root.toStyledString() ...

  10. 安装uiautomator2 + python 自动化环境

    搭建环境: 1.Python版本 37 2.已经搭建到adb (之前试过在python版本几,一直都装不上UIautomator2,报这个错) 安装步骤: 1.到python的安装路径 > 一直 ...