6***@qq.com
6***@qq.com
  • 发布:2020-04-30 15:55
  • 更新:2020-04-30 15:55
  • 阅读:777

本地图片转base64问题

分类:HBuilderX

拍照获取的地址:file:///var/mobile/Containers/Data/Application/2A17B3A4-F473-4595-9290-F9F0AAE575D9/Documents/Pandora/apps/HBuilder/doc/photo_023.jpg?version=1588232764173

相册获取的地址:/var/mobile/Containers/Data/Application/2A17B3A4-F473-4595-9290-F9F0AAE575D9/Documents/Pandora/apps/HBuilder/doc/head.png?version=1588232755549

            //将图片压缩转成base64    
            function getBase64Image(img) {  
                var canvas = document.createElement("canvas");  
                var width = img.width;  
                var height = img.height;  
                // calculate the width and height, constraining the proportions    
                if (width > height) {  
                    if (width > 100) {  
                        height = Math.round(height *= 100 / width);  
                        width = 100;  
                    }  
                } else {  
                    if (height > 100) {  
                        width = Math.round(width *= 100 / height);  
                        height = 100;  
                    }  
                }  
                canvas.width = width; /*设置新的图片的宽度*/  
                canvas.height = height; /*设置新的图片的长度*/  
                var ctx = canvas.getContext("2d");  
                ctx.drawImage(img, 0, 0, width, height); /*绘图*/  
                var dataURL = canvas.toDataURL("image/png", 0.8);  
                return dataURL.replace("data:image/png;base64,", "");  
            }

用这个方式转base64为 data:,

2020-04-30 15:55 负责人:无 分享
已邀请:

该问题目前已经被锁定, 无法添加新回复