Skip to content

Instantly share code, notes, and snippets.

@marcusbuffett
Created March 28, 2021 18:57
Show Gist options
  • Save marcusbuffett/0bb4263881da9b5cd746f6eee4cf163d to your computer and use it in GitHub Desktop.
Save marcusbuffett/0bb4263881da9b5cd746f6eee4cf163d to your computer and use it in GitHub Desktop.
use image::Luma;
use qrcode::QrCode;
fn main() {
let bytes_limit = 2953;
let mut bytes = 1000;
let mut step = 100;
let hitchikers_text = std::fs::read_to_string("hitchhikers.txt").unwrap();
loop {
let head_bytes = hitchikers_text
.clone()
.into_bytes()
.iter()
.take(bytes)
.cloned()
.collect::<Vec<u8>>();
let compressed = lzma::compress(&head_bytes, 9).unwrap();
let compressed_length = compressed.len();
println!("{}", compressed_length);
println!("{}", &bytes_limit);
if compressed_length == bytes_limit {
println!("{} bytes was perfect!", &bytes);
break;
} else if compressed_length < bytes_limit && step == 1 {
println!(
"{} bytes is the right amount, any more and it would be too high.",
&bytes
);
break;
} else if compressed_length > bytes_limit {
println!(
"{} bytes was too much! Consuming {} fewer bytes",
&bytes, step
);
step -= 1;
bytes -= step;
} else {
println!(
"{} bytes was too little! Consuming {} more bytes",
&bytes, step
);
if step == 1 {
break;
}
bytes += step;
}
}
let head_bytes = hitchikers_text
.clone()
.into_bytes()
.iter()
.take(bytes)
.cloned()
.collect::<Vec<u8>>();
let compressed = lzma::compress(&head_bytes, 9).unwrap();
// dbg!(&compressed.len());
let string = qr_code.render().light_color(' ').dark_color('#').build();
// let qr_code =
// qrcode::QrCode::with_error_correction_level(&compressed, qrcode::types::EcLevel::L)
// .unwrap();
// let image = qr_code.render::<Luma<u8>>().build();
// image.save("qrcode_max.png").unwrap();
// let qr_code = qrcode::QrCode::with_error_correction_level(
// &hitchikers_text
// .clone()
// .into_bytes()
// .iter()
// .take(2953)
// .cloned()
// .collect::<Vec<u8>>(),
// qrcode::types::EcLevel::L,
// )
// .unwrap();
// let image = qr_code.render::<Luma<u8>>().build();
// image.save("qrcode_plaintext.png").unwrap();
// println!("{}", string);
// println!("{}", test_string.len());
// println!("{}", decompressed_str);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment