当前位置:   article > 正文

uniapp-微信小程序-图片转base64_uni.getfilesystemmanager

uni.getfilesystemmanager

当前在做得小程序设计拍照识别,而服务器方需要前端提供图片的base64编码作为参数进行解析识别,一开始想着走原生JS的base64方法——借助canvas,当然我也试了,就在自己觉得没问题的时候,调试时终端报错了:ReferenceError: Image is not defined.

我着实懵了,也就是说Image对象实例化在小程序这边是无效的,查资料后真的很打脸,虽然人家微信不给Image用,但是人家给提供了转base64的方法。咱直接用就行了,唉,要是早一点对目标问题进行查询,早点看文档也许不用走一遭弯路喽。


我的需求:

1、图像来源:可以拍照,也可以从相册上传进行图像识别

2、后端限制:只对png、jpeg格式图片进行识别处理

  1. getPhoto(){
  2. uni.chooseMedia({
  3. count:1,// 限制选取图像数量
  4. mediaType:["image"],// 设置选取资源为图片类型
  5. sourceType:["album","camera"],// 设置图像来源:相册或相机
  6. camera:"back",// 设置相机为后置摄像头
  7. success:(res)=>{
  8. // 获取微信拿到的图片的临时地址并保存到本地
  9. this.photoPath=res.tempFiles[0].tempFilePath;
  10. // 获取当前图片信息
  11. uni.getImageInfo({
  12. src: res.tempFiles[0].tempFilePath,
  13. success: (image)=> {
  14. // 做png/jpeg的类型判断————对不同类型的图像添加不同的转换头信息
  15. if(image.type=='png'||image.type=='jpeg'){
  16. // 对符合类型的图片转换为base64类型
  17. uni.getFileSystemManager().readFile({// 【重点来啦】人家自提供的转码API
  18. filePath:image.path,// 所需转码图像路径
  19. encoding:"base64",// 转码类型
  20. success:(res)=>{
  21. // 生成base64
  22. let imageBase64='data:image/'+image.type+';base64,'+res.data;
  23. console.log('转base64后:',imageBase64);
  24. }
  25. })
  26. }else{// 友好一点,不是以上类型做出提醒
  27. uni.showToast({
  28. title:'当前只支持png/jpeg格式',
  29. duration:2500,
  30. icon:'none'
  31. })
  32. }
  33. }
  34. });
  35. }
  36. })
  37. },

如果看上去有些乱,可以看下面这个,也就是uni/wx提供的转码方法:

  1. // (由于uniapp开发所以uni打头)
  2. uni.getFileSystemManager().readFile({// 【重点来啦】人家自提供的转码API
  3. filePath:image.path,// 所需转码图像路径
  4. encoding:"base64",// 转码类型
  5. success:(res)=>{
  6. // 生成base64
  7. let imageBase64='data:image/'+image.type+';base64,'+res.data;
  8. console.log('转base64后:',imageBase64);
  9. }
  10. })

原生JS-canvas图像转base64的写法是这样的:(用到了jQuery的Deferred,记得引jQuery)

  1. let imgSrc = "";// 图片地址(可本地可网络)
  2. //width、height调用时传入具体像素值,控制大小 ,不传则默认图像大小
  3. function getBase64Image(img, width, height) {
  4. var canvas = document.createElement("canvas");
  5. canvas.width = width ? width : img.width;
  6. canvas.height = height ? height : img.height;
  7. var ctx = canvas.getContext("2d");
  8. ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
  9. var dataURL = canvas.toDataURL();
  10. return dataURL;
  11. }
  12. function getCanvasBase64(img) {
  13. var image = new Image();
  14. //至关重要-允许跨域
  15. image.crossOrigin = '';
  16. image.src = img;
  17. //至关重要——借用jQuery的Deferred方法
  18. var deferred = $.Deferred();
  19. if (img) {
  20. image.onload = function () {
  21. deferred.resolve(getBase64Image(image));//将base64传给done上传处理
  22. // document.getElementById("container2").appendChild(image);// 测试时用于页面显示
  23. }
  24. return deferred.promise();//要让onload完成后再return
  25. }
  26. }
  27. getCanvasBase64(imgSrc)
  28. .then(function (base64) {
  29. // 转成之后的就是base64了
  30. console.log("转base64:",base64);
  31. }, function (err) {
  32. console.log(err);
  33. });
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/689057
推荐阅读
相关标签
  

闽ICP备14008679号