智能云图库-以图搜图解决办法

之前发现教程里百度的那个用不了了,然后360的平替也不行,之后看到https://www.codefather.cn/user/1855959217308680193 找的解决方法,非常感谢!。但是那段代码因为是原生的嘛,有点复杂,那段代码的核心逻辑就是模仿用户操作给它发请求,那么我就尝试自己弄了一个。来替代教程里的GetImagePageUrlApi方法来获取URL。这样的话我们只需要改这个文件和门面类的调用就好了!

代码如下:

java
复制代码
import cn.hutool.http.HttpRequest; import cn.hutool.http.HttpResponse; import cn.hutool.http.HttpUtil; import cn.hutool.json.JSONObject; import cn.hutool.json.JSONUtil; public class BaiduImageSearchUtil { /** * 通过图片 URL 上传到百度图片搜索 * @param imageUrl 图片的 URL 地址 * @return 百度返回的 JSON 结果 */ public static String uploadImageByUrl(String imageUrl) { // 下载图片 byte[] imageBytes = HttpUtil.downloadBytes(imageUrl); // 上传到百度 long uptime = System.currentTimeMillis(); String uploadUrl = "https://graph.baidu.com/upload?uptime=" + uptime; HttpResponse response = HttpRequest.post(uploadUrl) .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36") .header("Accept", "*/*") .header("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8") .header("Origin", "https://graph.baidu.com") .header("Referer", "https://graph.baidu.com/") .header("Sec-Fetch-Dest", "empty") .header("Sec-Fetch-Mode", "cors") .header("Sec-Fetch-Site", "same-origin") .header("Acs-Token", "") .form("image", imageBytes, "image.jpg") .form("tn", "pc") .form("from", "pc") .form("image_source", "PC_UPLOAD_URL") .timeout(30000) .execute(); JSONObject jsonObject = JSONUtil.parseObj(response.body()); String url = (String) jsonObject.get("data", JSONObject.class).get("url"); return url; } public static void main(String[] args) { String imageUrl = "https://cn.bing.com/th/id/OIP-C.IJZgTNx1vp9EML_1wV5p2gHaEo?w=233&h=180&c=7&r=0&o=7&dpr=1.3&pid=1.7&rm=3"; // JSONObject result = uploadImageByUrl(imageUrl); String result = uploadImageByUrl(imageUrl); System.out.println(result); } }

门面类:

java
复制代码
@Slf4j public class ImageSearchApiFacade { /** * 搜索图片 * * @param imageUrl * @return */ public static List<ImageSearchResult> searchImage(String imageUrl) { String imagePageUrl = BaiduImageSearchUtil.uploadImageByUrl(imageUrl); String imageFirstUrl = GetImageFirstUrlApi.getImageFirstUrl(imagePageUrl); List<ImageSearchResult> imageList = GetImageListApi.getImageList(imageFirstUrl); return imageList; } public static void main(String[] args) { // 测试以图搜图功能 String imageUrl = "https://cn.bing.com/th/id/OIP-C.IJZgTNx1vp9EML_1wV5p2gHaEo?w=233&h=180&c=7&r=0&o=7&dpr=1.3&pid=1.7&rm=3"; List<ImageSearchResult> resultList = searchImage(imageUrl); System.out.println("结果列表" + resultList); } }

image.png

获取全部图片url测试结果:

image.png

获取具体图片的url测试结果:

结果列表[ImageSearchResult(thumbUrl=http://mms2.baidu.com/it/u=569158274,4256921924&fm=253&app=138&f=JPEG?w=200&h=150, fromUrl=http://jingyan.baidu.com/article/454316abac8d47f7a7c03ac4.html), ImageSearchResult(thumbUrl=http://mms0.baidu.com/it/u=3279374365,677980737&fm=253&app=138&f=JPEG?w=371&h=247, fromUrl=http://hellorf.com/image/show/633962915), ImageSearchResult(thumbUrl=http://mms2.baidu.com/it/u=955193541,3698081520&fm=253&app=138&f=JPEG?w=330&h=247, fromUrl=http://www.hellorf.com/image/show/268563467)...

image.png

0个评论
点击登录,快来和大家讨论吧~
表情
图片
暂无评论
llllll
下载 APP