node 图片转base64

抓取网络图片需要加载http模块

//假定这是index.js文件

var http = require(‘http‘);

var url = ‘http://p0.meituan.net/tuanpic/3df525af5a3f7fe04077567d2a6caf794904.png‘;  //一张网络图片

http.get(url,function(res){
  var chunks = []; //用于保存网络请求不断加载传输的缓冲数据
  var size = 0;   //保存缓冲数据的总长度

  res.on(‘data‘,function(chunk){
    chunks.push(chunk);  //在进行网络请求时,会不断接收到数据(数据不是一次性获取到的),

                //node会把接收到的数据片段逐段的保存在缓冲区(Buffer),

                //这些数据片段会形成一个个缓冲对象(即Buffer对象),

                //而Buffer数据的拼接并不能像字符串那样拼接(因为一个中文字符占三个字节),

                //如果一个数据片段携带着一个中文的两个字节,下一个数据片段携带着最后一个字节,

                //直接字符串拼接会导致乱码,为避免乱码,所以将得到缓冲数据推入到chunks数组中,

                //利用下面的node.js内置的Buffer.concat()方法进行拼接

         
    size += chunk.length;  //累加缓冲数据的长度
  });

  

  res.on(‘end‘,function(err){

    var data = Buffer.concat(chunks, size);  //Buffer.concat将chunks数组中的缓冲数据拼接起来,返回一个新的Buffer对象赋值给data

    console.log(Buffer.isBuffer(data));    //可通过Buffer.isBuffer()方法判断变量是否为一个Buffer对象

    

    var base64Img = data.toString(‘base64‘);  //将Buffer对象转换为字符串并以base64编码格式显示

    console.log(base64Img);   //进入终端terminal,然后进入index.js所在的目录,

                   //在终端中输入node index.js

                   //打印出来的就是图片的base64编码格式,格式如下    

  });

});

抓取网络图片后转换为base64编码格式,格式如下:

iVBORw0KGgoAAAANSUhEUgAAAKQAAABsCAYAAADkDhmYAAAABmJLR0QA/wD/AP+gvaeTAAAQQUlEQVR42u2dd5gV1RmH34UVWIrIggJGkKJoAI2ixqixYW9oohgTxSTG2GskkURjN/aW2GNQY0eDqFERC7FLVCJFRLGAXURAUZosmz9+Z5zh7tw7M/dOu7vnfZ777N07Z2bOzPzmlO875zs1g8aPoQLaAdOB/kW2fwDcA1wEzKvkRGXQATgFOARYv0ia+cCGwOcp581ShFYV7r8UOL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jxf9BGjS2lWnkYAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAABJRU5ErkJggg==

时间: 2024-10-28 14:31:55

node 图片转base64的相关文章

JAVA 将图片转换为Base64编码

这里使用的jar包是commons-codec-1.10.jar; 示例代码 1 import java.io.FileInputStream; 2 import java.io.FileOutputStream; 3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.io.OutputStream; 6 7 import org.apache.commons.codec.binary.Base64;

JS 图片转Base64

JS 图片转Base64 有时候需要向HTML中插入一张图片,可苦于上线后找不到一个合适的网盘来存储这些图片,有没有一种办法能将图片转换成文字,然后直接插入HTML中呢,通过Base64编码就可以解决这个问题. 废话不多说直接上代码.不知道什么是Base64的请自行百度. 图片转Base64 示例代码 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>JS

.net C# 图片转Base64 Base64转图片

//图片 转为 base64编码的文本 private void button1_Click(object sender, EventArgs e) { OpenFileDialog dlg = new OpenFileDialog(); dlg.Multiselect = true; dlg.Title = "选择要转换的图片"; dlg.Filter = "Image files (*.jpg;*.bmp;*.gif;*.png)|*.jpg*.jpeg;*.gif;*.

android 将图片通过base64转换为String 将图片String转换为Bitmap

1.Bitmap转换为图片字符串 Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); //该方法用来压缩图片,第一个参数为图片格式,第二个参数为截取图片的保留率,如当前为90,则保留之前图片90%的区域 bitmap.compress(Bitmap

Base64编码 图片与base64编码互转

package com.education.util; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; import java.io.*; /** * Created by jn-dinggao on 2016/2/4. */ public class Base64Util { // 将图片文件转化为字节数组字符串,并对其进行Base64编码处理 public static String GetImageStr(Stri

本地与在线图片转Base64及图片预览

查看效果:http://sandbox.runjs.cn/show/tgvbo9nq 本地图片转Base64(从而可以预览图片): function localImgLoad() { var src = this.files[0]; var self = $(this); var read = new FileReader(); read.onload = function(e) { var html = "<img src=" + e.target.result + "

图片转base64字符串

图片转base64字符串 by 伍雪颖 - (NSString *)base64String:(UIImage *)image { NSData * data = [UIImagePNGRepresentation(image) base64EncodedDataWithOptions:NSDataBase64Encoding64CharacterLineLength]; return [NSString stringWithUTF8String:[data bytes]]; } NSData

图片和base64编码字符串 互相转换,图片和byte数组互相转换

图片和base64编码字符串 互相转换 import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; import java.io.*; /** * @author lishupeng * @create 2017-05-06 下午 2:56 **/ public class Base64Test { public static void main(String[] args) { String strImg = GetImageSt

将图片转换为base64 格式

1.页面上的图片,转换成base64格式,可以通过canvas 的 toDataURL 例子:给定图片的url 将图片转换为base64 var imageSrc = "../images/0.jpg"; // 图片的URL //@param image:Image 对象,ext:图片的格式(jpg)function getBase64Image(image,ext){ var canvas = document.createElement("canvas"); c