深井菜鸟
深井菜鸟
  • 发布:2016-09-26 18:39
  • 更新:2016-09-26 21:09
  • 阅读:2286

DCloud 如何把base64保存为图片文件

分类:HTML5+

我这样写可以导出文件,但图片是无法显示的,因为不是一个有效的图片文件

writer.onwrite = function(result){
alert('success');
};

var base64 = canvas.toDataURL('image/jpg')
,img = new Image();
img.src = base64;
//这里是处理图片
ctx.clearRect(0,0,canvas.width,canvas.height);
ctx.drawImage(image,0,0,canvas.width,canvas.height);
ctx.drawImage(img,0,0,canvas.width,canvas.height);
//导出base64与取出base64头
base64 = canvas.toDataURL('image/jpg');
base64 = base64.replace(/^data:image\/\w+;base64,/, '');
//写入文件
var dataBuffer = new Buffer(base64,'base64');
writer.write(bufferArr);

2016-09-26 18:39 负责人:无 分享
已邀请:
赵梦欢

赵梦欢 - 专注前端,乐于分享!

可以使用plus.nativeObj.Bitmap

<!DOCTYPE html>  
<html>  
    <head>  
        <meta charset="UTF-8">  
        <title></title>  
    </head>  
    <body>  
        <script type="text/javascript">  
            var base64Str = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAYAAADDPmHLAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAABuwAAAbsBOuzj4gAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAAAOCSURBVHic7d0xSxxRFIbh15DOwl47u0MglVtEEQSL5B/YxtrKX2Jlra3/QAtBCGqxVoFwunRub2G9KeYuMbOzRhJnZtfve5pBZlcvnHfvjgujS+PxGNP1ru8FWL8cgDgHIO79Sx6UmZ+AXWAAbACrbS7K/tkIuAOGwGVE3P7tCUvPXQRm5gpwBOy/1gqtUyfAYUQ8zHrAzAAycws4A9aAR+AYuAGGETF6/bXa/8rMVapdehM4AJaBe2AvIq6bntMYQHnl/6Aa/jfga0T8bGnd1oLMXAdOgW2qCD407QSzLgKP+D38HQ9/8ZSZ7VDNcI1qplOmdoBywXdDte1/9PAXW9kJvlO9HWzWLwybdoDdcjz28BdfmeFx+XK3fr4pgEE53rS1KOvcZJaD+ommADbKcdjacqxrk1lu1E80BbAK4F/13o4ns5z6AM8fBYtzAOIcgDgHIM4BiHMA4hyAOAcgzgGIcwDiHIA4ByDOAYhzAOIcgDgHIM4BiHMA4hyAOAcgzgGIkw0gM88z87zvdfTtRX8f4I363PcC5oHsDmAVByDOAYhzAOIcgDgHIM4BiHMA4hyAOAcgzgGIcwDiHIA4ByDOAYhzAOIcgDgHIM4BiHMA4hyAOAcgzgGIcwDiHIA4ByDOAYibi3sDy02avdyrl5mz/3duOy4i4kvHP3Mm7wDi5mIH6OMVMXnlR8RS1z97nngHEOcAxDkAcQ5AnAMQ5wDEOQBxDkCcAxDnAMQ5AHEOQJwDEOcAxDkAcQ5AnAMQ5wDEOQBxDkCcAxDnAMQ5AHEOQJwDEOcAxDkAcXNxb2BPLvpewDyQDWCebtHuk98CxDkAcQ5AnAMQ5wDEOQBxDkCcAxDnAMQ5AHEOQJwDEOcAxDkAcQ5AnAMQ5wDEOQBxDkBcUwAjgMxc7Xgt1pInsxzVzzUFcFeOg9ZWZF2bzPKufqIpgGE5bra2HOvaZJbD+ommAC7L8SAz11tbknWizPCgfHlZPz8VQETcAifAMnCamb5QXFBldqdUszwps/3DrOEeAvfANnDlnWDxlJldUc3wnmqmU5bG4+b/m5iZW8AZsAY8AsfADTCMiKmrSetfudofUL3nH1C98u+BvYi4bnrOzADKN1wBjoD9V1+tdeEEOIyIh1kPeDaAicz8BOxS1bUB+DOC+TSi+lVvCFw2vefXvSgAe7t8hS/OAYhzAOJ+ATeBuJOueKnYAAAAAElFTkSuQmCC';  

            var bitmap=null;  
            // H5 plus事件处理  
            function plusReady(){  
                bitmap = new plus.nativeObj.Bitmap("test");  
                bitmap.loadBase64Data(base64Str, function(){  
                    console.log("加载Base64图片数据成功");  
                    bitmap.save("_doc/test.jpg",{},function(i){  
                        console.log('保存图片成功:'+JSON.stringify(i));  
                    },function(e){  
                        console.log('保存图片失败:'+JSON.stringify(e));  
                    });  
                }, function(){  
                    console.log('加载Base64图片数据失败:'+JSON.stringify(e));  
                });  
            }  
            if(window.plus){  
                plusReady();  
            }else{  
                document.addEventListener("plusready",plusReady,false);  
            }  
        </script>  
    </body>  
</html>

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