简体   繁体   English

将原始图像数据转换为 html 中可用的数据

[英]Convert raw image data to something usable in html

I've seen a couple of posts on here similar to this, but I was unable to find a solution that works so I'm hoping a fresh conversation on the topic might offer some more clarity.我在这里看到了几篇与此类似的帖子,但我找不到有效的解决方案,所以我希望关于该主题的新对话可能会更清晰。

I'm working with the google places api, and when I try to get an image back I'm getting what appears to be raw image data.我正在使用 google place api,当我尝试取回图像时,我得到的似乎是原始图像数据。 I have no idea what to do with that and how to make it usable in html.我不知道如何处理它以及如何使其在 html 中可用。 I've tried encoding it in base64 and then applying to the image tag as I've read on certain posts, but it only shows a broken image.我尝试在 base64 中对其进行编码,然后在我阅读某些帖子时将其应用于图像标签,但它只显示损坏的图像。 On top of the main issue, I don't even seem to have any way of verifying I'm getting a legitimate image back from the api call until/unless I can render it properly in the DOM.除了主要问题之外,我似乎没有任何方法来验证我是否从 api 调用中获取了合法图像,直到/除非我可以在 DOM 中正确呈现它。

Here's the full code for the api call:这是 api 调用的完整代码:

function makeAPICall(url) {
  return fetch(url);
}    
makeAPICall(`http://localhost:5000/places/${searchString}`)
          .then(response => {
            return response.json();
          })
          .then(data => {
            responseObject = data.results;
            renderResult(responseObject[10]);
    
            return responseObject[10];
          })
          .then(responseObject => {
            makeAPICall(`http://localhost:5000/place/image/${responseObject.photos[0].photo_reference}`)
              .then(response => {
    
                return response;
              })
              .then(photoData => {
                console.log(photoData.url);
                const encodedImg = btoa(photoData.url);
    
                bizImage.src = `data:image/jpeg;base64,${encodedImg}`;
              });
          });

The specific bit of code that's trying to deal with the image:试图处理图像的特定代码位:

.then(photoData => {
      console.log(photoData.url);
      const encodedImg = btoa(photoData.url);
    
      bizImage.src = `data:image/jpeg;base64,${encodedImg}`;
});

A sample of the raw data being returned (the entire blob is too big to include in the post):返回的原始数据示例(整个 blob 太大,无法包含在帖子中):

����JFIF��*ExifII*1Google���ICC_PROFILE�mntrRGB XYZ �$acsp���-)�=ޯ�U�xB��ʃ9 descDybXYZ�bTRC�dmdd ��gXYZ hgTRC�lumi |meas �$bkpt �rXYZ �rTRC�tech �vued ��wtptpcprt�7chad�,descsRGB IEC61966-2-1 black scaledXYZ $����curv #(-27;@EJOTY^chmrw|������������������������� %+28>ELRY`gnu|����������������&/8AKT]gqz������������!-8COZfr~���������� -;HUcq~��������� +:IXgw��������'7HYj{�������+=Oat�������2FZn�������  % : O d y � � � � � �  ' = T j � � � � � �"9Qi������*C\u����� & @ Z t � � � � �.Id���� %A^z���� &Ca~����1Om����&Ed����#Cc����'Ij����4Vx���&Il����Ae����@e����

So after a lot of reading and research, I was able to get a better understanding of this API and realize that I was sort of going about it the wrong way.所以经过大量的阅读和研究,我能够更好地理解这个 API,并意识到我的做法有点错误。 There's a client-side service for places and maps apis that should be used for these kinds of things and actually returns usable data.有一个用于地点和地图 api 的客户端服务,应该用于这些类型的事情并实际返回可用数据。 Full disclosure, I feel the way google has organized their documentation in this regard is pretty terrible, but the info is in there.完全公开,我觉得谷歌在这方面组织文档的方式非常糟糕,但信息就在那里。

data:image/gif;base64,${encodedImg}

JFIF 是 JPEG 文件交换格式。

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

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