• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

微信小程序base64图片在canvas上画图

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

上代码

wxml

<canvas canvas-id="myCanvas" style="width:400px;height:400px;"></canvas>

js

const ctx = wx.createCanvasContext('myCanvas'), //canvas
      fsm = wx.getFileSystemManager(),  //文件管理器
      FILE_BASE_NAME = 'tmp_base64src', //文件名
      format = 'png', //文件后缀
      base64Str = ('iVBORw0KGgoAAAANSUhEUgAAACgAAAAoCAYAAACM/rhtAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAA3ZpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuNi1jMDY3IDc5LjE1Nzc0NywgMjAxNS8wMy8zMC0yMzo0MDo0MiAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wTU09Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9tbS8iIHhtbG5zOnN0UmVmPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvc1R5cGUvUmVzb3VyY2VSZWYjIiB4bWxuczp4bXA9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC8iIHhtcE1NOk9yaWdpbmFsRG9jdW1lbnRJRD0ieG1wLmRpZDpiZDM3ZmYwZi0wZDgxLWRkNDMtYTBjOC05YzU1OWVmMmUyNmYiIHhtcE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uyboLXT1IXgr9FkNYyJ6tkptmuSy++eVqBb4DdOt11ERw+RSUEmEOwXTG1VptbdSkMJRXuwvpLYKkuglfPUD+cTmM0udLomntJxNztxx9N0m9BTP01N2oEegw/W4WqD92EK+WiTwx9KhjzynGmmzbFGnjlPvxugfUAY3dNCoLYTyNV46upZ9dcivYzbqz9qjnFeMhP8SjGwMQLGnbVWjlFLa7QfoZBbgBro8BzSTw47a+6a6dJFmVYEwFKhWTYPhrziG08WwyRC1WbSp8yviwk1aKa/8Dfj0HPDWvRv92De3sEVT8vZLwu2azdTAQpdCSsTaA2yOWR1zQ70RchfRb3ZsixgZxvP5SDghWH8Z27pL+EQfo3O9TTxATHu0Emgp6lnp1y/ypzhvaTR4opqlDU1ipr0mQQNebCkeZ8atAIlpQeQdF5woZy+sme4gQFhHTUqfosuzdUTPm8FBRwWDTrhq1fPyJJLftolCRi7ScHBb/D234y6kZYgQXF1WIHafZ9pdjBNb1RQ51Jz3HOKvsvxY+0pRXO6yn77SczQZC7BYf34XDReCEtY+0Z7AWi2Ln7FfgEY7lcJbIofB9MN23Smc/ixn+3GgQbBDfo9U+AAQAR3QEE8abLPQAAAABJRU5ErkJggg==') //base64字符串
    buffer = wx.base64ToArrayBuffer(base64Str), //base 转二进制
    filePath = `${wx.env.USER_DATA_PATH}/www.${format}`; //文件名

    fsm.writeFile({ //写文件
      filePath,
      data: buffer,
      encoding: 'binary',
      success(res) {
wx.getImageInfo({ //读取图片 src: filePath, success:
function(res) { console.log({ res }) ctx.drawImage(res.path, 0, 0, 150, 100) //画图 ctx.draw() }, error(res) { console.log({ res }) } }) } })

注意:base字符串没有头信息


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
小程序之百度智能小程序(第一章)发布时间:2022-07-18
下一篇:
小程序-页面跳转发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap