本地图片或拍照文件进行base64编码,百度进行识别的解决方案

当使用plus.gallery.pick选取文件的时候其实返回给我们的是一个本地文件URL路径。但是在使用的时候

有的时候是需要一个文件对象,比如当把一个本地图片转换为base字符串的时候就出现了这个问题:

如下:

核心代码如下:

/* 
     * 本地图片的绝对路径地址 转换成base64编码 如下代码: 
     */
    function getBase64Images(localImgUrl) {
        //这里对图片格式进行编码
        var imageBase;
        //*******************************************
        var picturerul;
        picturerul = plus.io.convertLocalFileSystemURL(localImgUrl);
        plus.io.resolveLocalFileSystemURL(picturerul, function(entry) {
            entry.file(function(file) {
                    reader = new plus.io.FileReader();
                    reader.onloadend = function(e) {
                        console.log("最新!未剪切的值的值为:" + JSON.stringify(e.target.result));
                        imageBase = e.target.result.replace("data:image/jpeg;base64,", "");
                        //e.target.result;//base64字符串 
                        console.log("最新!剪切的值的值为:" + imageBase);
                    };
                    reader.readAsDataURL(file);
                },
                function(e) {
                    alert(e.message);
                });
        }, function() {
            console.log(1);
        });

        return imageBase;
    }

 

下面的是操作选择本地文件后的调用方法:

mui.plusReady(function() {
                plus.gallery.pick(function(fileUrl) {
                    console.log("file文件为" + fileUrl);
                  
                    var imageBase;
                    //********************** 这里对图片格式进行编码*********************
                    var picturerul;
                    picturerul = plus.io.convertLocalFileSystemURL(fileUrl);
                    plus.io.resolveLocalFileSystemURL(picturerul, function(entry) {
                        entry.file(function(file) {
                                reader = new plus.io.FileReader();
                                reader.onloadend = function(e) {
                                    console.log("最新!未剪切的值的值为:" + JSON.stringify(e.target.result));//未剪切包含有头部
                                    imageBase = e.target.result.replace("data:image/jpeg;base64,", "");//变成真正可以使用的编码
                                    //e.target.result;//base64字符串 
                                    console.log("最新!剪切的值的值为:" + imageBase);
                                };
                                reader.readAsDataURL(file);
                            },
                            function(e) {
                                alert(e.message);
                            });
                    }, function() {
                        console.log(1);
                    });

                    //********************下面调用百度身份中识别接口***********************
                    mui.ajax({
                        url: "https://aip.baidubce.com/rest/2.0/ocr/v1/idcard",
                        header: {
                            "Content-Type": "application/x-www-form-urlencoded"
                        },
                        type: "post",
                        async: true,
                        data: {
                            access_token: access_token,
                            id_card_side: "back",
                            image: imageBase
                        },
                        dataType: "json",
                        timeout: 30000,
                        success: function(data) {
                            console.log("解析成功哈哈哈哈哈哈哈哈哈哈或或或或或或或或或或");
                            console.log(JSON.stringify(data));
                        },
                        error: function(xhr) {
                            console.log("请求解析失败");
                        }
                    }, function(e) {}, {
                        filename: "_doc/camera/",
                        filter: "image"
                    });
                });
            });
        }
    });

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值