如何使用Cordova从设备上的base64字符串创建图像文件

本文概述

  • 要求
  • 实现
使用Javascript将base64转换为文件, 而Cordova比你想象的要容易得多。但是, 你不能直接使用cordova-plugin-file将字符串写入文件, 因为它不受支持。但是它接受二进制数据, 这就是我们保存图像的方式。要使用base64字符串创建图像文件, 我们将使用非常规方法将其转换为Blob。
要求 在处理文件时, 你需要在项目中安装cordova-file-plugin。要安装此插件, 请使用:
cordova plugin add cordova-plugin-file

如果你对该插件一无所知, 请在此处的官方存储库中了解有关其工作原理的更多信息。
实现 要将base64字符串转换为图像文件, 我们将需要以下两种方法。
/** * Convert a base64 string in a Blob according to the data and contentType. * * @param b64Data {String} Pure base64 string without contentType * @param contentType {String} the content type of the file i.e (image/jpeg - image/png - text/plain) * @param sliceSize {Int} SliceSize to process the byteCharacters * @see http://stackoverflow.com/questions/16245767/creating-a-blob-from-a-base64-string-in-javascript * @return Blob */function b64toBlob(b64Data, contentType, sliceSize) {contentType = contentType || ''; sliceSize = sliceSize || 512; var byteCharacters = atob(b64Data); var byteArrays = []; for (var offset = 0; offset < byteCharacters.length; offset += sliceSize) {var slice = byteCharacters.slice(offset, offset + sliceSize); var byteNumbers = new Array(slice.length); for (var i = 0; i < slice.length; i++) {byteNumbers[i] = slice.charCodeAt(i); }var byteArray = new Uint8Array(byteNumbers); byteArrays.push(byteArray); }var blob = new Blob(byteArrays, {type: contentType}); return blob; }/** * Create a Image file according to its database64 content only. * * @param folderpath {String} The folder where the file will be created * @param filename {String} The name of the file that will be created * @param content {Base64 String} Important : The content can't contain the following string (data:image/png[or any other format]; base64, ). Only the base64 string is expected. */function savebase64AsImageFile(folderpath, filename, content, contentType){// Convert the base64 string in a Blobvar DataBlob = b64toBlob(content, contentType); console.log("Starting to write the file :3"); window.resolveLocalFileSystemURL(folderpath, function(dir) {console.log("Access to the directory granted succesfully"); dir.getFile(filename, {create:true}, function(file) {console.log("File created succesfully."); file.createWriter(function(fileWriter) {console.log("Writing content to file"); fileWriter.write(DataBlob); }, function(){alert('Unable to save file in path '+ folderpath); }); }); }); }

cordova-file-plugin提供的cordova文件编写器不支持使用base64编写文件, 因此我们将使用一些技巧。 base64字符串将被处理并转换为可写Blob。
使用savebase64AsImageFile方法
【如何使用Cordova从设备上的base64字符串创建图像文件】片段中很好地说明了该方法, 要对其进行测试, 你可以在项目中测试以下代码, 它应该在根目录中创建以下图像。
如何使用Cordova从设备上的base64字符串创建图像文件

文章图片
// Remember to execute this after the onDeviceReady event// If your base64 string contains "data:image/png; base64, "" at the beginning, keep reading.var myBase64 = "iVBORw0KGgoAAAANSUhEUgAAANIAAADCCAMAAAALkWzrAAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAK7OHOkAAAA2UExURQa5/rDn/F7R/Ru//gC2/5Pf+3nY/P///wC4/wCy/8jt/Pn9/i7E/kTK/vrx7tv0/u36//Lm4jC6R7oAABSASURBVHja7V3ZuqsqDBYIKAgL+/4ve0gYBLWtU9dwvu3F3m1Xq/5mDknoun/Hv4OOAZrjT2NBAFpDZx3nXCnFubNWDOGjcAx/kDJaC66keYTjqxyPr/DWSMXtoP8SxQKcgStDUPqNYyRwXnLxJ2ANgTpW+sc2mOYIsB5SIaxfTR89OLYHTgXL8E7/VskK8mPlETyZDQOqIFm/kkD8OJ6CitnfBgp0FwjUnz8Cqdxv4j/QdhvQZFjQ17YTVthwOC6Zf0EqPvwSSg1asDWgiXEbtJ+OhjUd9F44aRa4fQalfgX76WFNIRNMDmEZ1gcBA6sSuUZLOC2jd4+H+3FQoNUS0KTQDdpC0+IaOBKLa4jvOxkpZYQefpTnXAE0xv+C6tKv4RRYiMqz/O0AykRFIX9QpGBgXwlJ7+kF697RZ4lKDDC/U0n7uZ8iVEWiiSTDC30AT+JAqBGqpNJ/hlAA8pFJZCb8Vx4GtKaazGbK6u8nkZilKOpkdxkRYmLpnEGffzPzaV5INEVEVg83HAD5tF/mW5lvCKq72KBoYPgtiIYhixMyn4BvNEay2CJGYuSv0ajSEaCLyzR+n0DBYBaI1DUxC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if cordova.file is not available use instead :// var folderpath = "file:///storage/emulated/0/"; var folderpath = cordova.file.externalRootDirectory; var filename = "ourcodeworld.png"; savebase64AsImageFile(folderpath, filename, myBase64, contentType);

根据你的情况和base64图像格式, 你可以使用2种类型的base64字符串:
// This type is common when using toDataUrl() on a canvas.var type1 = "data:image/png; base64, iVBORw0KGgoAAAANSUhEUgAAAkYAAAD................."; // The plain base64 content of a file, maybe retrieven from a server ?var type2 = "iVBORw0KGgoAAAANSUhEUgAAAkYAAAD.................";

保存base64类型1
由于此字符串已经包含需要该功能的内容类型, 因此我们只需要处理该字符串并以不同的变量检索内容和内容类型(image / png或image / jpeg等)。
/** Process the type1 base64 string **/var myBaseString = "data:image/png; base64, iVBORw0KGgoAAAANSUhEUgAAAkYAAA....."; // Split the base64 string in data and contentTypevar block = myBaseString.split("; "); // Get the content typevar dataType = block[0].split(":")[1]; // In this case "image/png"// get the real base64 content of the filevar realData = http://www.srcmini.com/block[1].split(", ")[1]; // In this case "iVBORw0KGg...."// The path where the file will be createdvar folderpath = "file:///storage/emulated/0/"; // The name of your file, note that you need to know if is .png, .jpeg etcvar filename = "myimage.png"; savebase64AsImageFile(folderpath, filename, realData, dataType);

保存base64类型2
使用这种格式, 你需要自己找出base64的内容类型, 因为我们只有数据。
// The base64 contentvar myBase64 = "iVBORw0KGgoAAAANSUhEUgAAANIAAAD......"; // To define the type of the Blob, you need to get this value by yourself (maybe according to the file extension)var contentType = "image/png"; // The path where the file will be savedvar folderpath = "file:///storage/emulated/0/"; // The name of your filevar filename = "myphoto.png"; savebase64AsImageFile(folderpath, filename, myBase64, contentType);

玩得开心

    推荐阅读