detecting “we have no imagery” of google maps street view static images

后端 未结 6 1717
梦毁少年i
梦毁少年i 2021-02-13 17:43

I\'m generating street view static images like so:

https://maps.googleapis.com/maps/api/streetview?size=1080x400&location=%s&fov=90&heading=235&pitch=0&a

6条回答
  •  醉话见心
    2021-02-13 18:39

    One quick solution would be to load the image file using xmlrpc and check that its md5sum is 30234b543d5438e0a0614bf07f1ebd25, or that its size is 1717 bytes (it's unlikely that another image can have exactly the same size), but that's not very robust since I have seen Google change the position of the text in the image. Though it's a very good start for a prototype.

    You could go for image processing instead. Note that it's still not perfectly robust since Google could decide to change the looks of the image anytime. You'll have to decide whether it's worth it.

    Anyway, here is how I would do it using jQuery:

    • load the image and open a 2D context for direct pxiel access (see this question for how to do it)
    • analyse the image:
      • sample groups of 2×2 pixels at random locations; I recommend at least 30 groups
      • a group of 2×2 pixels is good if all the pixels have the same value and their R/G/B values do not differ by more than 10% (ie. they're grey)
      • count the ratio of good pixel groups in the image
    • if there are more than 70% good pixel groups, then we are pretty sure this is the “no imagery” version: replace it with another image of your choice.

    The reason I do not recommend testing directly for an RGB value is because JPEG decompression may have slightly different behaviours on different browsers.

提交回复
热议问题