简体   繁体   English

如何将图像缓冲区转换为字符串 base64?

[英]How can I convert image buffer to string base64?

在此处输入图像描述 I am uploading image using Filepond library to MongoDB using Mongoose.我正在使用 Filepond 库将图像上传到使用 ZCCADCDEDB567ABAE643E15DCF0974E503Z 的 MongoDB。 I want to show the image in the map's marker using pop-up feature.我想使用弹出功能在地图的标记中显示图像。 Image I am getting back in the format图片我正在恢复格式

{ location:
   { type: 'Point',
     coordinates: [ -86.34164, 39.81514 ],
     formattedAddress: 'Lucas Oil Raceway, Brownsburg, IN 46234, US' },
  _id: 5f10a1867435d3f048a14acc,
  storeId: '009',
  dateCompleted: 2020-07-01T04:00:00.000Z,
  description:
   ' setup datguard. backups, flashack logs  and monitoring',
  createdAt: 2020-07-16T18:50:46.997Z,
  storeImage:
   Binary {
     _bsontype: 'Binary',
     sub_type: 0,
     position: 3569671,
     buffer:
      <Buffer ff d8 ff e0 00 10 4a....
storeImageType: 'image/jpeg',
  __v: 0 }

I am able to show rest of the properties except image.我能够显示除图像之外的属性的 rest。 Is there a way i can convert this image buffer to string and then set to map properties.有没有办法可以将此图像缓冲区转换为字符串,然后设置为 map 属性。

stores = data.data.map(store => {
        return {
            type: 'Feature',
            geometry: {
                type: 'Point',
                coordinates: [
                    store.location.coordinates[0],
                    store.location.coordinates[1]
                ]
            },
            properties: {
                storeId: store.storeId,
                dateCompleted: store.dateCompleted,
                description: store.description,
                         
          image:`data:${store.storeImageType};charset=utf-8;base64,${store.storeImage.data.toString('base64')}`,

##with this i see image content as "data:image/jpeg;charset=utf;base64,233,4444,555,67 icon: 'shop' } }; }); ##with this i see image content as "data:image/jpeg;charset=utf;base64,233,4444,555,67 icon: 'shop' } }; });

function setMarkers(stores) {
    //add markers to the map
    stores.forEach(function (marker) {

        // create a HTML element for each feature
        var el = document.createElement('div-marker');
        el.className = 'marker';

        // make a marker for each feature and add to the map
        var mkr = new mapboxgl.Marker(el)
            .setLngLat(marker.geometry.coordinates)
            .setPopup(new mapboxgl.Popup({
                    offset: 25
                }) // add popups this is where i need to set image
                .setHTML("<img src='" + marker.properties.image + "' width='160' />"))
            .addTo(map);
        mkrs.push(mkr);
    });
}

All this code is in public javascript folder using NodeJS Express.所有这些代码都在使用 NodeJS Express 的公共 javascript 文件夹中。

From the format you posted, it seems like the image buffer is located in store.storeImage.buffer .从您发布的格式来看,图像缓冲区似乎位于store.storeImage.buffer中。 storeImage.data must be a getter that is converting it to something else - from the looks of it, probably an array? storeImage.data必须是一个将其转换为其他东西的getter - 从它的外观来看,可能是一个数组?

Try using store.storeImage.buffer.toString('base64') - passing 'base64' to toString is a feature of buffers specifically, other types don't handle it the same.尝试使用store.storeImage.buffer.toString('base64') - 将'base64'传递给toString是缓冲区的一个特性,其他类型的处理方式不同。

Edit: Apparently store.storeImage itself is the buffer.编辑:显然store.storeImage本身就是缓冲区。 So you should use simply store.storeImage.toString('base64')所以你应该简单地使用store.storeImage.toString('base64')

Edit: Apparently it describes itself as a buffer despite not actually offering a node.js Buffer object.编辑:尽管实际上并未提供 node.js 缓冲区 object,但显然它将自己描述为缓冲区。 I suggest converting the array to a buffer and then encoding as base64: Buffer.from(store.storeImage.data).toString('base64')我建议将数组转换为缓冲区,然后编码为 base64: Buffer.from(store.storeImage.data).toString('base64')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM