Coder Social home page Coder Social logo

Comments (6)

ayangweb avatar ayangweb commented on August 12, 2024 1

嗯嗯,hash算法有快慢的,如果是很大的图,就需要考虑这个hash算法的速度了

哦哦,这个我暂时没有注意到,您推荐的 xxhash_rust 这个算法库比较快是吧 😁,我可以换上试试

from clipboard-rs.

ayangweb avatar ayangweb commented on August 12, 2024 1

图片在不同的格式,不同大小,hash肯定也是不同的,本质上是图片内容的字节数组不一样 byte[]

感谢大佬提醒,这让我想到了用缩略图计算哈希,果然又能快出将近 60% 的时间 🤗

from clipboard-rs.

ChurchTao avatar ChurchTao commented on August 12, 2024

@ayangweb 我推荐你可以使用这个库,

https://github.com/DoumanAsh/xxhash-rust

我写个简单的例子

先在配置中增加依赖:
xxhash-rust = { version = "0.8.10", features = ["xxh3"] }

use clipboard_rs::{common::RustImage, Clipboard, ClipboardContext};
use xxhash_rust::xxh3::xxh3_64;

fn main() {
	let ctx = ClipboardContext::new().unwrap();

	let img = ctx.get_image();

	match img {
		Ok(img) => {
			let hash = xxh3_64(&img.to_png().unwrap().get_bytes());
			let hash_str = format!("{:x}", hash);
			println!("hash={}", hash);
			println!("hash_str={}", hash_str);
		}
		Err(err) => {
			println!("err={}", err);
		}
	}
}

from clipboard-rs.

ChurchTao avatar ChurchTao commented on August 12, 2024

图片在不同的格式,不同大小,hash肯定也是不同的,本质上是图片内容的字节数组不一样 byte[]

from clipboard-rs.

ayangweb avatar ayangweb commented on August 12, 2024

@ChurchTao 谢谢大佬的回复,我这边也是查阅了一下 rust 相关的知识,实现思路也是跟您的一致!

use std::{ hash::{Hash, Hasher}, };

let ctx = ClipboardContext::new().unwrap();

let image = ctx.get_image().unwrap();

let bytes = image.to_png().unwrap().get_bytes().to_vec();

let mut hasher = DefaultHasher::new();

bytes.hash(&mut hasher);

let hash = hasher.finish();

from clipboard-rs.

ChurchTao avatar ChurchTao commented on August 12, 2024

嗯嗯,hash算法有快慢的,如果是很大的图,就需要考虑这个hash算法的速度了

from clipboard-rs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.