我正在尝试将此JS代码转换为Java:
let url = e.image.url;
request(imageurl, async function(err, res, body) {
if (err !== null) return;
imghash
.hash(body)
.then(hash => {
console.log(hash);
})
});
它使用以下库:
https://www.npmjs.com/package/imghash https://www.npmjs.com/package/request
根据 imghash 的文档,当未指定位数时,默认值为8,但是我发现很奇怪,因为该代码中的示例哈希为ff3f2783818f8fff
,这不是128位吗?
到目前为止,我已经设法将图像转换为字节数组,并将该字节数组转换为十六进制哈希,但是我得到了一个巨大的哈希,而不是像这样的ff3f2783818f8fff
哈希。 >
是否有一种方法可以将图像转换为与该图像相似的哈希?
URL url = new URL(e.getImage().getUrl());
final HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setRequestProperty(
"User-Agent",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.65 Safari/537.31");
BufferedImage image = ImageIO.read(connection.getInputStream());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write( image, "jpg", baos );
baos.flush();
byte[] imageInByte = baos.toByteArray();
baos.close();
hex = getHexString(imageInByte);
public static String getHexString(byte[] b) throws Exception {
String result = "";
for (int i=0; i < b.length; i++) {
result +=
Integer.toString( ( b[i] & 0xff ) + 0x100, 16).substring( 1 );
}
return result;
}